Commit 0f8054a1 authored by shrabbit's avatar shrabbit
Browse files

Add View playground and improve error handling in RunProgram and PlaygroundRunner

parent d0ae5dc1
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
6
 No newline at end of file
+22 −13
Original line number Diff line number Diff line
namespace TestV;
using Spectre.Console;

namespace TestV;

public class PlaygroundRunner<TP> : IPlayground
where TP : class, IPlayground, new()
@@ -20,6 +22,8 @@ where TP : class, IPlayground, new()
    }

    public async Task Run(string[] args)
    {
        try
        {
            if (_tp is null)
            {
@@ -36,4 +40,9 @@ where TP : class, IPlayground, new()

            await _tp.Run(args);
        }
        catch (Exception e)
        {
            AnsiConsole.WriteException(e);
        }
    }
}
 No newline at end of file
+11 −4
Original line number Diff line number Diff line
@@ -17,9 +17,16 @@ public class RunProgram : IPlayground
            return;
        }

        try
        {
            var exec = args[0];
            var runArgs = args.Length > 1 ? args[1..] : [];

            await Process.Start(exec, runArgs).WaitForExitAsync();
        }
        catch (Exception e)
        {
            AnsiConsole.MarkupLine($"[red]错误:{e.Message}[/]");
        }
    }
}
 No newline at end of file
+62 −0
Original line number Diff line number Diff line
using Spectre.Console;

namespace TestV.Playgrounds;

public class View : IPlayground
{
    public string LaunchCmd { get; } = "view";
    public string? Name { get; } = "View Picture";
    public string? Description { get; } = "提供一个图像位置,在控制台输出该图片(不提供路径则输出默认图像)";
    public async Task Run(string[] args)
    {
        if (args.Length == 0)
        {
            ReadDemos();
            return;
        }
        
        var image = new CanvasImage(args[0]);
        AnsiConsole.Write(image);
    }

    void ReadDemos()
    {
        var basePath = "Assets";
        var maxPath = Path.Combine(basePath, "view-demo-max");
        var fileTemplate = Path.Combine(basePath, "view-{0}");
        
        if (!File.Exists(maxPath))
        {
            AnsiConsole.MarkupLine("[red]错误:参数太少且找不到示例图像。[/]");
            return;
        }

        var max = int.Parse(File.ReadAllText(maxPath));
        for (var i = 0; i < max; i++)
        {
            AnsiConsole.MarkupLine($"[cyan]Demo {i + 1} / {max}[/]");
            var image = new CanvasImage(TryFile(string.Format(fileTemplate, i)));
            
            AnsiConsole.Write(image);
        }
    }

    string TryFile(string filename)
    {
        Span<string> list = ["bmp", "jpg", "png"];
        var result = string.Empty;

        foreach (var ext in list)
        {
            var name = filename + $".{ext}";
            AnsiConsole.MarkupLine($"[DarkOrange]{name}[/]");
            
            if (!File.Exists(name)) continue;
            
            result = name;
            break;
        }

        return result;
    }
}
 No newline at end of file
+1 −0
Original line number Diff line number Diff line
@@ -149,6 +149,7 @@ class Program
                .Add<StorageTest>()
                .Add<MakeSpace>()
                .Add<RunProgram>()
                .Add<View>()
                .Add<Exp1>();
            
            play.Startup(args);
Loading