spectreconsole / spectre.console

A .NET library that makes it easier to create beautiful console applications.
https://spectreconsole.net
MIT License
9.45k stars 499 forks source link

Cannot convert from 'Spectre.Console.Table' to 'string' #971

Closed gjaryczewski closed 10 months ago

gjaryczewski commented 2 years ago

The following code returns the error:

using Spectre.Console;

namespace NewProject.Services;

public class ContextService : IContextService
{
    private readonly IRootService _rootService;

    public ContextService(IRootService rootService)
    {
        _rootService = rootService ?? throw new ArgumentNullException(nameof(rootService));
    }

    public void List()
    {
        var root = _rootService.GetPath();
        DirectoryInfo info = new DirectoryInfo(root);
        var directories = info.EnumerateDirectories();

        var table = new Table();
        table.Border = TableBorder.SimpleHeavy;
        table.AddColumn("Context");
        table.AddColumn("Path");

        foreach (var item in directories)
        {
            table.AddRow(item.Name, item.FullName);
        }

        AnsiConsole.Write(table); // Here is C:\Repos\NewProject\Services\ContextService.cs(36,27)
    }
}

The build output:

C:\Repos\NewProject\Services\ContextService.cs(36,27): error CS1503: Argument 1: cannot convert from 'Spectre.Console.Table' to 'string' [C:\Repos\NewProject\NewProject.csproj]

The build failed. Fix the build errors and run again.

Could anyone help me?

patriksvensson commented 2 years ago

What version of Spectre.Console are you using? .NET 6?

gjaryczewski commented 2 years ago

Thank you for so fast reply, it's awesome! Yes: .NET 6.0.203, Spectre.Console 0.41.0.

phil-scott-78 commented 2 years ago

One of the versions since 0.41.0 we switched around some syntax to make things consistent in their behavior. Back then Write only took a string and you'd need to use Render for any of the IRenderable elements.

I'd recommend updating to a new version as we've done a ton of improvements in the past year.

gjaryczewski commented 2 years ago

Great idea, but I couldn't use other examples from documentation with the recent versions... All right, let's forget about it. Thank you for your support.

patriksvensson commented 2 years ago

What examples didn't work? If that's the case then we want to address that.

patriksvensson commented 2 years ago

Need to figure out what part of the documentation is wrong with the latest version. @gjaryczewski Any help would be appreciated here.

gjaryczewski commented 2 years ago

:-) All right, I will repeat the exercise and find something useful during the weekend.

gjaryczewski commented 2 years ago

All right, so the Program.cs looks like this:

using Microsoft.Extensions.DependencyInjection;
using Spectre.Console;
using Spectre.Console.Cli;

public static class Program
{
    public static int Main(string[] args)
    {
        var app = new CommandApp();
        app.Configure(config =>
        {
            config.PropagateExceptions();
        });

        try
        {
            return app.Run(args);
        }
        catch (Exception ex)
        {
            AnsiConsole.WriteException(ex, ExceptionFormats.ShortenEverything);
            return -99;
        }
    }
}

The .csproj file looks like this:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net6.0</TargetFramework>
    <RootNamespace>test_spectre</RootNamespace>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
    <PackageReference Include="Spectre.Console" Version="0.45.0" />
  </ItemGroup>

</Project>

The output of the build process contains: C:\Users\gjary\Repos\test-spectre\Program.cs(3,23): error CS0234: The type or namespace name 'Cli' does not exist in the namespace 'Spectre.Console' (are you missing an assembly reference? [C:\Users\gjary\Repos\test-spectre\test-spectre.csproj].

gjaryczewski commented 2 years ago

According to the tutorial documentation, CommandApp is the entry point for a Spectre.Console.Cli command line application, this is also repeated in API Reference.

gjaryczewski commented 2 years ago

The previous code works with 0.44. Does not work with 0.45.

patriksvensson commented 2 years ago

We split the Spectre.Console.Cli stuff into its own NuGet package for 0.45: https://www.nuget.org/packages/spectre.console.cli

phil-scott-78 commented 2 years ago

This is a good catch though I'll update the docs tonight to include a reference to the new package