From 06e8bf49d212566a65b9192090197515225a9983 Mon Sep 17 00:00:00 2001 From: Dante De Ruwe Date: Tue, 19 Nov 2024 19:03:23 +0100 Subject: [PATCH] Clean up code --- src/Dotty.CLI/Commands/SimpleCommands.cs | 39 +++++--------------- src/Dotty.CLI/Helpers/Output.cs | 47 ------------------------ 2 files changed, 9 insertions(+), 77 deletions(-) diff --git a/src/Dotty.CLI/Commands/SimpleCommands.cs b/src/Dotty.CLI/Commands/SimpleCommands.cs index 083c8a1..dd47cff 100644 --- a/src/Dotty.CLI/Commands/SimpleCommands.cs +++ b/src/Dotty.CLI/Commands/SimpleCommands.cs @@ -1,8 +1,6 @@ using Cocona.Application; using Cocona.Help; -using Cocona.Help.DocumentModel; using Spectre.Console; -using Spectre.Console.Rendering; namespace Dotty.CLI.Commands; @@ -13,33 +11,14 @@ public void Register(ICoconaAppBuilder app) app.AddCommand("greet", ([Argument] string subject) => Panel($"Hello, {subject}!")); app.AddCommand(([FromService] ICoconaApplicationMetadataProvider metadataProvider, [FromService] ICoconaHelpMessageBuilder helpProvider) => - { - Panel(new Rows( - new FigletText("dotty").Color(Color.Aqua).Centered(), - new Markup($"[bold][aqua]{metadataProvider.GetVersion()}[/][/]").Centered(), - new Rule { Style = Style.Parse("gray") }, - new Text("\n"), - new Text(helpProvider.BuildAndRenderForCurrentCommand()) - )); - }) - .OptionLikeCommand(x => - { - x.Add("tree", () => Tree("Dotty CLI", t => t - .WithChild("greet") - .WithChild("convert", c => c - .WithChild("units") - .WithChild("tobase64") - .WithChild("frombase64") - ) - .WithChild("generate", g => g - .WithChild("random", r => r - .WithChild("guid") - .WithChild("number") - .WithChild("fromtemplate") - ) - .WithChild("timestamp") - ) - )); - }); + { + Panel(new Rows( + new FigletText("dotty").Color(Color.Aqua).Centered(), + new Markup($"[bold][aqua]{metadataProvider.GetVersion()}[/][/]").Centered(), + new Rule { Style = Style.Parse("gray") }, + new Text("\n"), + new Text(helpProvider.BuildAndRenderForCurrentCommand()) + )); + }); } } \ No newline at end of file diff --git a/src/Dotty.CLI/Helpers/Output.cs b/src/Dotty.CLI/Helpers/Output.cs index aa7bd90..27c7e3e 100644 --- a/src/Dotty.CLI/Helpers/Output.cs +++ b/src/Dotty.CLI/Helpers/Output.cs @@ -27,51 +27,4 @@ public static T Select(string title, params IEnumerable choices) where T : public static void WriteItalic(T value) => AnsiConsole.Markup($"[italic]{value}[/]"); public static void WriteBold(T value) => AnsiConsole.Markup($"[bold]{value}[/]"); - - public static void Tree(string root, Action configure) - { - var builder = new TreeBuilder(root); - configure(builder); - AnsiConsole.Write(builder.Build()); - } -} - -internal class TreeNodeBuilder(string name) -{ - private readonly TreeNode _node = new(new Text(name)); - - public TreeNodeBuilder WithChild(string child, Action? configure = null) - { - var builder = new TreeNodeBuilder(child); - configure?.Invoke(builder); - return WithChild(builder.Build()); - } - - public TreeNodeBuilder WithChild(TreeNode node) - { - _node.AddNode(node); - return this; - } - - public TreeNode Build() => _node; -} - -internal class TreeBuilder(string rootName) -{ - private readonly Tree _tree = new(rootName); - - public TreeBuilder WithChild(string child, Action? configure = null) - { - var builder = new TreeNodeBuilder(child); - configure?.Invoke(builder); - return WithChild(builder.Build()); - } - - public TreeBuilder WithChild(TreeNode node) - { - _tree.AddNode(node); - return this; - } - - public Tree Build() => _tree; } \ No newline at end of file