Skip to content

Commit 9e79716

Browse files
committed
[r] name
1 parent 89453e4 commit 9e79716

16 files changed

Lines changed: 211 additions & 199 deletions

.vscode/launch.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"type": "coreclr",
77
"request": "launch",
88
"preLaunchTask": "Build",
9-
"program": "${workspaceFolder}/src/EtcdTerminal.Console/bin/Debug/net10.0/etcd-terminal.dll",
10-
"cwd": "${workspaceFolder}/src/EtcdTerminal.Console/bin/Debug/net10.0/",
9+
"program": "${workspaceFolder}/src/EtcdTerminal.App/bin/Debug/net10.0/etcd-terminal.dll",
10+
"cwd": "${workspaceFolder}/src/EtcdTerminal.App/bin/Debug/net10.0/",
1111
"internalConsoleOptions": "openOnSessionStart",
1212
"env": {
1313
"ASPNETCORE_ENVIRONMENT": "Development"
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
using EtcdTerminal;
2+
using System.Text.RegularExpressions;
3+
4+
namespace EtcdTerminal.App.Engine;
5+
6+
public static class Menu
7+
{
8+
private const string _arrow = " ❯ ";
9+
10+
public static string? Show(string title, IEnumerable<string> choices, Func<string, string>? displayConverter = null)
11+
{
12+
var items = choices.ToList();
13+
var index = 0;
14+
15+
var plain = items.Select(c =>
16+
{
17+
var formatted = displayConverter?.Invoke(c) ?? c;
18+
19+
return StripMarkup(formatted);
20+
}).ToList();
21+
22+
var menuStart = Console.CursorTop;
23+
24+
Console.ResetColor();
25+
26+
if (!string.IsNullOrEmpty(title))
27+
{
28+
Console.WriteLine();
29+
Console.WriteLine(title);
30+
Console.WriteLine();
31+
}
32+
33+
var firstItemTop = Console.CursorTop;
34+
35+
for (var i = 0; i < items.Count; i++)
36+
{
37+
Console.Write(i == 0 ? _arrow : " ");
38+
39+
if (i == 0)
40+
Console.ForegroundColor = ConsoleColor.Yellow;
41+
42+
WriteTruncated(plain[i]);
43+
44+
if (i == 0)
45+
Console.ResetColor();
46+
47+
Console.WriteLine();
48+
}
49+
50+
Console.WriteLine();
51+
Console.ForegroundColor = ConsoleColor.DarkGray;
52+
Console.Write("(\u2191/\u2193 navigate, Enter confirm, Esc back)");
53+
Console.ResetColor();
54+
55+
while (true)
56+
{
57+
var key = Console.ReadKey(true);
58+
var oldIndex = index;
59+
60+
switch (key.Key)
61+
{
62+
case ConsoleKey.Escape:
63+
Console.CursorTop = menuStart;
64+
Console.CursorLeft = 0;
65+
Console.Write("\x1b[J");
66+
return null;
67+
case ConsoleKey.Enter:
68+
Console.CursorTop = menuStart;
69+
Console.CursorLeft = 0;
70+
Console.Write("\x1b[J");
71+
return items[index];
72+
case ConsoleKey.UpArrow:
73+
index = (index - 1 + items.Count) % items.Count;
74+
break;
75+
case ConsoleKey.DownArrow:
76+
index = (index + 1) % items.Count;
77+
break;
78+
default:
79+
continue;
80+
}
81+
82+
Console.CursorTop = firstItemTop + oldIndex;
83+
Console.CursorLeft = 0;
84+
Console.Write(" ");
85+
86+
WriteTruncated(plain[oldIndex]);
87+
88+
Console.CursorTop = firstItemTop + index;
89+
Console.CursorLeft = 0;
90+
Console.ForegroundColor = ConsoleColor.Yellow;
91+
Console.Write(_arrow);
92+
93+
WriteTruncated(plain[index]);
94+
95+
Console.ResetColor();
96+
}
97+
}
98+
99+
private static void WriteTruncated(string text)
100+
{
101+
var maxLen = Console.WindowWidth - 5;
102+
103+
if (text.Length > maxLen)
104+
{
105+
Console.Write(text.AsSpan(0, maxLen - 3));
106+
Console.Write("...");
107+
}
108+
else
109+
{
110+
Console.Write(text);
111+
}
112+
}
113+
114+
private static string StripMarkup(string text) => Regex.Replace(text, @"\[/?[^\]]*\]", "");
115+
}
Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
using EtcdTerminal;
12
using System.Text;
23
using Spectre.Console;
34

4-
namespace EtcdTerminal.Console.Engine;
5+
namespace EtcdTerminal.App.Engine;
56

67
public static class Prompt
78
{
@@ -34,25 +35,25 @@ public static class Prompt
3435

3536
while (true)
3637
{
37-
var key = System.Console.ReadKey(true);
38+
var key = Console.ReadKey(true);
3839

3940
switch (key.Key)
4041
{
4142
case ConsoleKey.Escape:
42-
System.Console.WriteLine();
43+
Console.WriteLine();
4344
return null;
4445
case ConsoleKey.Enter:
45-
System.Console.WriteLine("n");
46+
Console.WriteLine("n");
4647
return false;
4748
default:
4849
if (key.KeyChar is 'y' or 'Y')
4950
{
50-
System.Console.WriteLine("y");
51+
Console.WriteLine("y");
5152
return true;
5253
}
5354
if (key.KeyChar is 'n' or 'N')
5455
{
55-
System.Console.WriteLine("n");
56+
Console.WriteLine("n");
5657
return false;
5758
}
5859
break;
@@ -66,44 +67,44 @@ public static class Prompt
6667
{
6768
var input = new StringBuilder(prefill ?? "");
6869
var cursor = input.Length;
69-
var startCol = System.Console.CursorLeft;
70+
var startCol = Console.CursorLeft;
7071

7172
if (!string.IsNullOrEmpty(prefill))
72-
System.Console.Write(prefill);
73+
Console.Write(prefill);
7374

7475
while (true)
7576
{
76-
var key = System.Console.ReadKey(true);
77+
var key = Console.ReadKey(true);
7778

7879
switch (key.Key)
7980
{
8081
case ConsoleKey.Escape:
8182
ClearInput(startCol);
82-
System.Console.WriteLine();
83+
Console.WriteLine();
8384
return null;
8485
case ConsoleKey.Enter:
85-
System.Console.WriteLine();
86+
Console.WriteLine();
8687
return input.ToString();
8788
case ConsoleKey.LeftArrow:
8889
if (cursor > 0)
8990
{
9091
cursor--;
91-
System.Console.CursorLeft--;
92+
Console.CursorLeft--;
9293
}
9394
break;
9495
case ConsoleKey.RightArrow:
9596
if (cursor < input.Length)
9697
{
97-
System.Console.Write(input[cursor]);
98+
Console.Write(input[cursor]);
9899
cursor++;
99100
}
100101
break;
101102
case ConsoleKey.Home:
102-
System.Console.CursorLeft = startCol;
103+
Console.CursorLeft = startCol;
103104
cursor = 0;
104105
break;
105106
case ConsoleKey.End:
106-
System.Console.CursorLeft = startCol + input.Length;
107+
Console.CursorLeft = startCol + input.Length;
107108
cursor = input.Length;
108109
break;
109110
case ConsoleKey.Backspace:
@@ -135,18 +136,18 @@ public static class Prompt
135136

136137
private static void ClearInput(int startCol)
137138
{
138-
var endCol = System.Console.CursorLeft;
139+
var endCol = Console.CursorLeft;
139140

140-
System.Console.CursorLeft = startCol;
141-
System.Console.Write(new string(' ', Math.Max(0, endCol - startCol + 1)));
142-
System.Console.CursorLeft = startCol;
141+
Console.CursorLeft = startCol;
142+
Console.Write(new string(' ', Math.Max(0, endCol - startCol + 1)));
143+
Console.CursorLeft = startCol;
143144
}
144145

145146
private static void RedrawInput(int startCol, string text, int cursorPos)
146147
{
147-
System.Console.CursorLeft = startCol;
148-
System.Console.Write(text + ' ');
149-
System.Console.CursorLeft = startCol + cursorPos;
148+
Console.CursorLeft = startCol;
149+
Console.Write(text + ' ');
150+
Console.CursorLeft = startCol + cursorPos;
150151
}
151152

152153
private static string? ReadSecret()
@@ -155,28 +156,28 @@ private static void RedrawInput(int startCol, string text, int cursorPos)
155156

156157
while (true)
157158
{
158-
var key = System.Console.ReadKey(true);
159+
var key = Console.ReadKey(true);
159160

160161
switch (key.Key)
161162
{
162163
case ConsoleKey.Escape:
163-
System.Console.WriteLine();
164+
Console.WriteLine();
164165
return null;
165166
case ConsoleKey.Enter:
166-
System.Console.WriteLine();
167+
Console.WriteLine();
167168
return input.ToString();
168169
case ConsoleKey.Backspace:
169170
if (input.Length > 0)
170171
{
171172
input.Length--;
172-
System.Console.Write("\b \b");
173+
Console.Write("\b \b");
173174
}
174175
break;
175176
default:
176177
if (!char.IsControl(key.KeyChar))
177178
{
178179
input.Append(key.KeyChar);
179-
System.Console.Write('*');
180+
Console.Write('*');
180181
}
181182
break;
182183
}

src/EtcdTerminal.Console/EtcdTerminal.Console.csproj renamed to src/EtcdTerminal.App/EtcdTerminal.App.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<Version>0.1</Version>
88

99
<AssemblyName>etcd-terminal</AssemblyName>
10-
<RootNamespace>EtcdTerminal.Console</RootNamespace>
10+
<RootNamespace>EtcdTerminal.App</RootNamespace>
1111
<OutputType>Exe</OutputType>
1212
</PropertyGroup>
1313
<ItemGroup>

src/EtcdTerminal.Console/Modules/StatusBar.cs renamed to src/EtcdTerminal.App/Modules/StatusBar.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
using EtcdTerminal;
12
using EtcdTerminal.Models;
23
using Spectre.Console;
34

4-
namespace EtcdTerminal.Console.Modules;
5+
namespace EtcdTerminal.App.Modules;
56

67
public static class StatusBar
78
{
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
using EtcdTerminal.Console.Screens;
2-
using EtcdTerminal.Console.Setup;
1+
using EtcdTerminal;
2+
using EtcdTerminal.App.Screens;
3+
using EtcdTerminal.App.Setup;
34
using Simplify.DI;
45
using Spectre.Console;
56

src/EtcdTerminal.Console/Screens/InstanceSelectionScreen.cs renamed to src/EtcdTerminal.App/Screens/InstanceSelectionScreen.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
using EtcdTerminal;
12
using System.Reflection;
2-
using EtcdTerminal.Console.Engine;
3+
using EtcdTerminal.App.Engine;
34
using EtcdTerminal.Models;
45
using Simplify.System;
56
using Spectre.Console;
67

7-
namespace EtcdTerminal.Console.Screens;
8+
namespace EtcdTerminal.App.Screens;
89

910
public sealed class InstanceSelectionScreen(IConnectionConfigRepository _configRepo, IEtcdClient _etcdClient)
1011
{
@@ -54,7 +55,7 @@ await AnsiConsole.Status()
5455
AnsiConsole.WriteLine();
5556

5657
AnsiConsole.MarkupLine("[grey]Press any key to continue...[/]");
57-
System.Console.ReadKey(true);
58+
Console.ReadKey(true);
5859
}
5960
}
6061
}
@@ -114,7 +115,7 @@ private void AddInstanceInteractive()
114115
{
115116
AnsiConsole.MarkupLine("[red]Instance name cannot be empty.[/]");
116117
AnsiConsole.MarkupLine("[grey]Press any key to continue...[/]");
117-
System.Console.ReadKey(true);
118+
Console.ReadKey(true);
118119

119120
return;
120121
}
@@ -128,7 +129,7 @@ private void AddInstanceInteractive()
128129
{
129130
AnsiConsole.MarkupLine("[red]Invalid connection string. Must be a valid http or https URL.[/]");
130131
AnsiConsole.MarkupLine("[grey]Press any key to continue...[/]");
131-
System.Console.ReadKey(true);
132+
Console.ReadKey(true);
132133

133134
return;
134135
}
@@ -166,7 +167,7 @@ private void AddInstanceInteractive()
166167

167168
AnsiConsole.MarkupLine("[green]Instance added successfully![/]");
168169
AnsiConsole.MarkupLine("[grey]Press any key to continue...[/]");
169-
System.Console.ReadKey(true);
170+
Console.ReadKey(true);
170171
}
171172

172173
private void RemoveInstanceInteractive(IReadOnlyList<EtcdConnectionConfig> instances)
@@ -186,7 +187,7 @@ private void RemoveInstanceInteractive(IReadOnlyList<EtcdConnectionConfig> insta
186187
}
187188

188189
AnsiConsole.MarkupLine("[grey]Press any key to continue...[/]");
189-
System.Console.ReadKey(true);
190+
Console.ReadKey(true);
190191
}
191192

192193
private string GetVersion()

0 commit comments

Comments
 (0)