Skip to content

Commit 0ecf539

Browse files
committed
[r] extract string literals into named constants across screens
1 parent 92dc039 commit 0ecf539

7 files changed

Lines changed: 189 additions & 97 deletions

File tree

src/EtcdTerminal.App/Program.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
using EtcdTerminal;
2-
using EtcdTerminal.App.Screens;
2+
using EtcdTerminal.App.Screens;
33
using EtcdTerminal.App.Setup;
44
using Simplify.DI;
55
using Spectre.Console;
66

7+
const string SetBgCommand = "\x1b]11;#252629\x07";
8+
const string ResetBgCommand = "\x1b]111\x07";
9+
const string ShuttingDown = "[yellow]Shutting down...[/]";
10+
const string PressAnyKeyRestart = "\n[grey]Press any key to restart...[/]";
11+
712
Console.OutputEncoding = System.Text.Encoding.UTF8;
8-
Console.Write("\x1b]11;#252629\x07");
13+
Console.Write(SetBgCommand);
914

1015
Console.CancelKeyPress += (_, args) =>
1116
{
1217
args.Cancel = true;
13-
Console.Write("\x1b]111\x07");
18+
Console.Write(ResetBgCommand);
1419
Console.ResetColor();
1520
Console.WriteLine();
16-
AnsiConsole.MarkupLine("[yellow]Shutting down...[/]");
21+
AnsiConsole.MarkupLine(ShuttingDown);
1722
Environment.Exit(0);
1823
};
1924

@@ -38,7 +43,7 @@
3843
catch (Exception ex)
3944
{
4045
AnsiConsole.WriteException(ex);
41-
AnsiConsole.MarkupLine("\n[grey]Press any key to restart...[/]");
46+
AnsiConsole.MarkupLine(PressAnyKeyRestart);
4247
Console.ReadKey(true);
4348
}
4449
}

src/EtcdTerminal.App/Screens/InstanceSelectionScreen.cs

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,34 @@ namespace EtcdTerminal.App.Screens;
99

1010
public sealed class InstanceSelectionScreen(IConnectionConfigRepository _configRepo, IEtcdClient _etcdClient)
1111
{
12+
private const string InstanceAdded = "[green]Instance added successfully![/]";
13+
private const string InstanceRemoved = "[green]Instance removed successfully![/]";
14+
private const string ConnectedSuccess = "[green]Connected successfully![/]";
15+
private const string NameEmpty = "[red]Instance name cannot be empty.[/]";
16+
private const string InvalidConnStr = "[red]Invalid connection string. Must be a valid http or https URL.[/]";
17+
private const string ConsoleClientDesc = "[grey]Console client for etcd v3+[/]";
18+
private const string ManageConnections = "Manage Connections";
19+
private const string AddInstance = "Add Instance";
20+
private const string RemoveInstance = "Remove Instance";
21+
private const string Exit = "Exit";
22+
private const string SelectInstance = "Select etcd instance:";
23+
private const string SelectInstanceToRemove = "Select instance to remove:";
24+
private const string EnterInstanceName = "Enter instance name:";
25+
private const string EnterConnStr = "Enter connection string:";
26+
private const string DefaultConnStr = "http://localhost:2379";
27+
private const string UseSsl = "Use SSL?";
28+
private const string EnterUsername = "Enter username (optional, leave empty for none):";
29+
private const string EnterPassword = "Enter password:";
30+
private const string Connecting = "Connecting...";
31+
1232
public async Task<EtcdConnectionConfig?> ShowAsync()
1333
{
1434
while (true)
1535
{
1636
AnsiConsole.Clear();
1737
AnsiConsole.Write(new FigletText("etcd-terminal").Color(Color.OrangeRed1).Centered());
1838
AnsiConsole.MarkupLineInterpolated($"[grey]Version: {GetVersion()}[/]");
19-
AnsiConsole.MarkupLine("[grey]Console client for etcd v3+[/]");
39+
AnsiConsole.MarkupLine(ConsoleClientDesc);
2040

2141
var instances = _configRepo.LoadInstances();
2242

@@ -25,11 +45,11 @@ public sealed class InstanceSelectionScreen(IConnectionConfigRepository _configR
2545
if (choice is null)
2646
return null;
2747

28-
if (choice == "Manage Connections")
48+
if (choice == ManageConnections)
2949
{
3050
ManageConfigs(instances);
3151
}
32-
else if (choice == "Exit")
52+
else if (choice == Exit)
3353
{
3454
return null;
3555
}
@@ -40,12 +60,12 @@ public sealed class InstanceSelectionScreen(IConnectionConfigRepository _configR
4060
try
4161
{
4262
await AnsiConsole.Status()
43-
.StartAsync("Connecting...", async ctx =>
63+
.StartAsync(Connecting, async ctx =>
4464
{
4565
await _etcdClient.ConnectAsync(selected);
4666
});
4767

48-
AnsiConsole.MarkupLine("[green]Connected successfully![/]");
68+
AnsiConsole.MarkupLine(ConnectedSuccess);
4969

5070
return selected;
5171
}
@@ -65,10 +85,10 @@ await AnsiConsole.Status()
6585
{
6686
var choices = new List<string>();
6787
choices.AddRange(instances.Select(i => i.Name));
68-
choices.Add("Manage Connections");
69-
choices.Add("Exit");
88+
choices.Add(ManageConnections);
89+
choices.Add(Exit);
7090

71-
return Menu.Show("Select etcd instance:", choices, c =>
91+
return Menu.Show(SelectInstance, choices, c =>
7292
{
7393
var instance = instances.FirstOrDefault(i => i.Name == c);
7494

@@ -82,29 +102,29 @@ private void ManageConfigs(IReadOnlyList<EtcdConnectionConfig> instances)
82102
{
83103
AnsiConsole.Clear();
84104

85-
var manageChoices = new List<string> { "Add Instance" };
105+
var manageChoices = new List<string> { AddInstance };
86106

87107
if (instances.Count > 0)
88-
manageChoices.Add("Remove Instance");
108+
manageChoices.Add(RemoveInstance);
89109

90-
var action = Menu.Show("Manage Connections", manageChoices);
110+
var action = Menu.Show(ManageConnections, manageChoices);
91111

92112
if (action is null)
93113
return;
94114

95-
if (action == "Add Instance")
115+
if (action == AddInstance)
96116
{
97117
AddInstanceInteractive();
98118
}
99-
else if (action == "Remove Instance")
119+
else if (action == RemoveInstance)
100120
{
101121
RemoveInstanceInteractive(instances);
102122
}
103123
}
104124

105125
private void AddInstanceInteractive()
106126
{
107-
var name = Prompt.Ask("Enter instance name:");
127+
var name = Prompt.Ask(EnterInstanceName);
108128

109129
if (name is null)
110130
return;
@@ -113,33 +133,33 @@ private void AddInstanceInteractive()
113133

114134
if (string.IsNullOrWhiteSpace(name))
115135
{
116-
AnsiConsole.MarkupLine("[red]Instance name cannot be empty.[/]");
136+
AnsiConsole.MarkupLine(NameEmpty);
117137
AnsiConsole.MarkupLine(Prompt.PressAnyKeyMarkup);
118138
Console.ReadKey(true);
119139

120140
return;
121141
}
122142

123-
var connectionString = Prompt.Ask("Enter connection string:", "http://localhost:2379");
143+
var connectionString = Prompt.Ask(EnterConnStr, DefaultConnStr);
124144

125145
if (connectionString is null)
126146
return;
127147

128148
if (!Uri.TryCreate(connectionString, UriKind.Absolute, out var uri) || uri.Scheme is not ("http" or "https"))
129149
{
130-
AnsiConsole.MarkupLine("[red]Invalid connection string. Must be a valid http or https URL.[/]");
150+
AnsiConsole.MarkupLine(InvalidConnStr);
131151
AnsiConsole.MarkupLine(Prompt.PressAnyKeyMarkup);
132152
Console.ReadKey(true);
133153

134154
return;
135155
}
136156

137-
var useSsl = Prompt.Confirm("Use SSL?");
157+
var useSsl = Prompt.Confirm(UseSsl);
138158

139159
if (useSsl is null)
140160
return;
141161

142-
var username = Prompt.Ask("Enter username (optional, leave empty for none):");
162+
var username = Prompt.Ask(EnterUsername);
143163

144164
if (username is null)
145165
return;
@@ -148,7 +168,7 @@ private void AddInstanceInteractive()
148168

149169
if (!string.IsNullOrEmpty(username))
150170
{
151-
password = Prompt.Secret("Enter password:");
171+
password = Prompt.Secret(EnterPassword);
152172

153173
if (password is null)
154174
return;
@@ -165,14 +185,14 @@ private void AddInstanceInteractive()
165185

166186
_configRepo.AddInstance(config);
167187

168-
AnsiConsole.MarkupLine("[green]Instance added successfully![/]");
188+
AnsiConsole.MarkupLine(InstanceAdded);
169189
AnsiConsole.MarkupLine(Prompt.PressAnyKeyMarkup);
170190
Console.ReadKey(true);
171191
}
172192

173193
private void RemoveInstanceInteractive(IReadOnlyList<EtcdConnectionConfig> instances)
174194
{
175-
var nameToRemove = Menu.Show("Select instance to remove:", instances.Select(i => i.Name));
195+
var nameToRemove = Menu.Show(SelectInstanceToRemove, instances.Select(i => i.Name));
176196

177197
if (nameToRemove is null)
178198
return;
@@ -183,7 +203,7 @@ private void RemoveInstanceInteractive(IReadOnlyList<EtcdConnectionConfig> insta
183203
{
184204
_configRepo.RemoveInstance(nameToRemove);
185205

186-
AnsiConsole.MarkupLine("[green]Instance removed successfully![/]");
206+
AnsiConsole.MarkupLine(InstanceRemoved);
187207
}
188208

189209
AnsiConsole.MarkupLine(Prompt.PressAnyKeyMarkup);

src/EtcdTerminal.App/Screens/KeyBrowseScreen.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ public sealed class KeyBrowseScreen(IEtcdClient _etcdClient)
1414
private const int SearchBarRow = 4;
1515
private const int EditValueMaxLength = 200;
1616

17+
private const string SearchPlaceholder = "[grey]🔍 Type to search keys...[/]";
18+
private const string NoKeysFound = " [grey]No keys found.[/]";
19+
private const string EditAction = "[bold yellow][[E]][/] [white]Edit[/] ";
20+
private const string DeleteAction = "[bold yellow][[D]][/] [white]Delete[/] ";
21+
private const string CancelAction = "[bold yellow][[Esc]][/] [white]Cancel[/]";
22+
private const string KeyUpdated = "[green]Key updated successfully![/]";
23+
private const string CouldNotUpdateKey = "[red]Could not update key.[/]";
24+
private const string KeyDeleted = "[green]Key deleted successfully![/]";
25+
private const string KeyCouldNotBeDeleted = "[red]Key could not be deleted.[/]";
26+
private const string EnterNewValue = "Enter new value:";
27+
private const string AreYouSure = "Are you sure?";
28+
1729
private static int KeyColumnWidth => (Console.WindowWidth - LinePadding - PrefixWidth - 1) / 2;
1830
private static int ValueColumnWidth => Console.WindowWidth - LinePadding - PrefixWidth - 1 - KeyColumnWidth;
1931

@@ -210,7 +222,7 @@ private void Render()
210222
private void RenderSearchBar()
211223
{
212224
if (_searchQuery.Length == 0)
213-
AnsiConsole.Markup("[grey]🔍 Type to search keys...[/]");
225+
AnsiConsole.Markup(SearchPlaceholder);
214226
else
215227
AnsiConsole.Markup($"[yellow]🔍[/] [white]{Markup.Escape(_searchQuery)}[/]");
216228
}
@@ -221,7 +233,7 @@ private void RenderKeyList()
221233

222234
if (_filteredKeys.Count == 0)
223235
{
224-
AnsiConsole.MarkupLine(" [grey]No keys found.[/]");
236+
AnsiConsole.MarkupLine(NoKeysFound);
225237

226238
return;
227239
}
@@ -266,9 +278,9 @@ private void RenderActionBar()
266278

267279
AnsiConsole.MarkupLine($" [grey]Selected:[/] [cyan]{Markup.Escape(_selectedKey.Key)}[/]");
268280
AnsiConsole.Markup(" ");
269-
AnsiConsole.Markup("[bold yellow][[E]][/] [white]Edit[/] ");
270-
AnsiConsole.Markup("[bold yellow][[D]][/] [white]Delete[/] ");
271-
AnsiConsole.Markup("[bold yellow][[Esc]][/] [white]Cancel[/]");
281+
AnsiConsole.Markup(EditAction);
282+
AnsiConsole.Markup(DeleteAction);
283+
AnsiConsole.Markup(CancelAction);
272284
Console.WriteLine();
273285
}
274286

@@ -283,7 +295,7 @@ private async Task EditKeyAsync()
283295
AnsiConsole.MarkupLine($"Current value: [green]{Markup.Escape(TruncateText(_selectedKey.Value, EditValueMaxLength))}[/]");
284296
Console.WriteLine();
285297

286-
var newValue = Prompt.Ask("Enter new value:", _selectedKey.Value);
298+
var newValue = Prompt.Ask(EnterNewValue, _selectedKey.Value);
287299

288300
if (newValue is null)
289301
return;
@@ -295,12 +307,12 @@ private async Task EditKeyAsync()
295307

296308
if (result)
297309
{
298-
AnsiConsole.MarkupLine("[green]Key updated successfully![/]");
310+
AnsiConsole.MarkupLine(KeyUpdated);
299311
await ReloadAsync();
300312
}
301313
else
302314
{
303-
AnsiConsole.MarkupLine("[red]Could not update key.[/]");
315+
AnsiConsole.MarkupLine(CouldNotUpdateKey);
304316
}
305317

306318
Console.WriteLine();
@@ -318,7 +330,7 @@ private async Task DeleteKeyAsync()
318330
AnsiConsole.MarkupLine($"Delete key: [red]{Markup.Escape(_selectedKey.Key)}[/]");
319331
Console.WriteLine();
320332

321-
var confirm = Prompt.Confirm("Are you sure?");
333+
var confirm = Prompt.Confirm(AreYouSure);
322334

323335
if (confirm is not true)
324336
return;
@@ -330,12 +342,12 @@ private async Task DeleteKeyAsync()
330342

331343
if (result)
332344
{
333-
AnsiConsole.MarkupLine("[green]Key deleted successfully![/]");
345+
AnsiConsole.MarkupLine(KeyDeleted);
334346
await ReloadAsync();
335347
}
336348
else
337349
{
338-
AnsiConsole.MarkupLine("[red]Key could not be deleted.[/]");
350+
AnsiConsole.MarkupLine(KeyCouldNotBeDeleted);
339351
}
340352

341353
Console.WriteLine();

src/EtcdTerminal.App/Screens/KeyCreateScreen.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,32 @@ namespace EtcdTerminal.App.Screens;
88

99
public sealed class KeyCreateScreen(IEtcdClient _etcdClient)
1010
{
11+
private const string EnterKeyPrompt = "Enter key:";
12+
private const string EnterValuePrompt = "Enter value:";
13+
private const string KeyCreated = "[green]Key created successfully![/]";
14+
private const string KeyCreateFailed = "[red]Key already exists or could not be created.[/]";
15+
1116
public async Task ShowAsync(EtcdConnectionConfig config)
1217
{
1318
AnsiConsole.Clear();
1419
StatusBar.Render(config);
1520

16-
var key = Prompt.Ask("Enter key:");
21+
var key = Prompt.Ask(EnterKeyPrompt);
1722

1823
if (key is null)
1924
return;
2025

21-
var value = Prompt.Ask("Enter value:");
26+
var value = Prompt.Ask(EnterValuePrompt);
2227

2328
if (value is null)
2429
return;
2530

2631
var result = await _etcdClient.CreateKeyAsync(key, value);
2732

2833
if (result)
29-
AnsiConsole.MarkupLine("[green]Key created successfully![/]");
34+
AnsiConsole.MarkupLine(KeyCreated);
3035
else
31-
AnsiConsole.MarkupLine("[red]Key already exists or could not be created.[/]");
36+
AnsiConsole.MarkupLine(KeyCreateFailed);
3237

3338
AnsiConsole.MarkupLine(Prompt.PressAnyKeyMarkup);
3439
Console.ReadKey(true);

src/EtcdTerminal.App/Screens/PermissionViewScreen.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,23 @@ namespace EtcdTerminal.App.Screens;
88

99
public sealed class PermissionViewScreen(IEtcdClient _etcdClient)
1010
{
11+
private const string LoadingPermissions = "Loading permissions...";
12+
private const string NoUsersOrRoles = "[yellow]No users or roles found.[/]";
13+
1114
public async Task ShowAsync(EtcdConnectionConfig config)
1215
{
1316
AnsiConsole.Clear();
1417
StatusBar.Render(config);
1518

1619
await AnsiConsole.Status()
17-
.StartAsync("Loading permissions...", async ctx =>
20+
.StartAsync(LoadingPermissions, async ctx =>
1821
{
1922
var users = await _etcdClient.GetUsersAsync();
2023
var roles = await _etcdClient.GetRolesAsync();
2124

2225
if (users.Count == 0 && roles.Count == 0)
2326
{
24-
AnsiConsole.MarkupLine("[yellow]No users or roles found.[/]");
27+
AnsiConsole.MarkupLine(NoUsersOrRoles);
2528

2629
return;
2730
}

0 commit comments

Comments
 (0)