Skip to content

Commit

Permalink
Clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
DanteDeRuwe committed Nov 19, 2024
1 parent f419479 commit 06e8bf4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 77 deletions.
39 changes: 9 additions & 30 deletions src/Dotty.CLI/Commands/SimpleCommands.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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())
));
});
}
}
47 changes: 0 additions & 47 deletions src/Dotty.CLI/Helpers/Output.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,51 +27,4 @@ public static T Select<T>(string title, params IEnumerable<T> choices) where T :
public static void WriteItalic<T>(T value) => AnsiConsole.Markup($"[italic]{value}[/]");

public static void WriteBold<T>(T value) => AnsiConsole.Markup($"[bold]{value}[/]");

public static void Tree(string root, Action<TreeBuilder> 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<TreeNodeBuilder>? 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<TreeNodeBuilder>? 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;
}

0 comments on commit 06e8bf4

Please sign in to comment.