-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocsCommand.cs
More file actions
152 lines (129 loc) · 5.32 KB
/
Copy pathDocsCommand.cs
File metadata and controls
152 lines (129 loc) · 5.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System.CommandLine;
using System.CommandLine.Invocation;
using Spectre.Console;
namespace HelpLine.Docs;
/// <summary>
/// Displays embedded Markdown documentation topics.
/// </summary>
public sealed class DocsCommand : Command
{
public DocsCommand(DocsTopicCatalog catalog, MarkdownHelpRenderer? renderer = null)
: base("docs", "Displays detailed documentation by topic")
{
Catalog = catalog ?? throw new ArgumentNullException(nameof(catalog));
Renderer = renderer ?? new MarkdownHelpRenderer();
TopicOption = new DocsTopicOption(Catalog);
AllOption = new Option<bool>("--all")
{
Description = "Displays all documentation topics"
};
Options.Add(TopicOption);
Options.Add(AllOption);
Action = new ShowDocsTopicAction(Catalog, Renderer, TopicOption, AllOption);
Subcommands.Add(new ListDocsTopicsCommand(Catalog));
}
/// <summary>
/// The topic catalog used by the command.
/// </summary>
internal DocsTopicCatalog Catalog { get; }
/// <summary>
/// The renderer used when writing Markdown to the console.
/// </summary>
internal MarkdownHelpRenderer Renderer { get; }
/// <summary>
/// The topic-selection option.
/// </summary>
internal DocsTopicOption TopicOption { get; }
/// <summary>
/// Displays all topics.
/// </summary>
internal Option<bool> AllOption { get; }
private static void RenderAllTopics(TextWriter output, DocsTopicCatalog catalog, MarkdownHelpRenderer renderer)
{
var first = true;
foreach (var topic in catalog.Topics)
{
if (!catalog.TryReadTopicText(topic, out var markdown))
{
continue;
}
if (!first)
{
output.WriteLine();
}
renderer.Render(markdown ?? string.Empty, output);
first = false;
}
}
private sealed class ShowDocsTopicAction(
DocsTopicCatalog catalog,
MarkdownHelpRenderer renderer,
DocsTopicOption topicOption,
Option<bool> allOption) : SynchronousCommandLineAction
{
private readonly DocsTopicCatalog _catalog = catalog;
private readonly MarkdownHelpRenderer _renderer = renderer;
private readonly DocsTopicOption _topicOption = topicOption;
private readonly Option<bool> _allOption = allOption;
private const string AllTopicsLabel = "[bold yellow]all topics[/]";
public override int Invoke(ParseResult parseResult)
{
var output = parseResult.InvocationConfiguration.Output;
var requestedTopic = parseResult.GetValue<string?>(_topicOption.Name);
var showAll = parseResult.GetValue(_allOption);
if (showAll)
{
RenderAllTopics(output, _catalog, _renderer);
return 0;
}
if (string.IsNullOrWhiteSpace(requestedTopic))
{
if (_catalog.Topics.Count == 1 && _catalog.TryReadTopicText(_catalog.Topics[0], out var singleTopicMarkdown))
{
_renderer.Render(singleTopicMarkdown ?? string.Empty, output);
return 0;
}
if (ReferenceEquals(output, Console.Out) && !Console.IsOutputRedirected)
{
var choices = new List<TopicChoice>
{
new(AllTopicsLabel, TopicName: null),
};
foreach (var t in _catalog.Topics)
{
var indent = new string(' ', Math.Max(0, (t.Level - 1) * 2));
choices.Add(new TopicChoice($"{indent}{t.ShortName}", t.Name));
}
var prompt = new SelectionPrompt<TopicChoice>();
prompt.Title = "[bold]Select a topic:[/]";
prompt.UseConverter(static c => c.Display);
prompt.AddChoices(choices);
var selected = AnsiConsole.Prompt(prompt);
if (selected.TopicName is null)
{
RenderAllTopics(output, _catalog, _renderer);
}
else if (_catalog.TryGetTopic(selected.TopicName, out var selectedTopic) &&
selectedTopic is not null &&
_catalog.TryReadTopicText(selectedTopic, out var selectedMarkdown))
{
_renderer.Render(selectedMarkdown ?? string.Empty, output);
}
}
else
{
ListDocsTopicsCommand.WriteTopicList(output, _catalog);
}
return 0;
}
if (_catalog.TryGetTopic(requestedTopic, out var topic) &&
topic is not null &&
_catalog.TryReadTopicText(topic, out var markdown))
{
_renderer.Render(markdown ?? string.Empty, output);
}
return 0;
}
private sealed record TopicChoice(string Display, string? TopicName);
}
}