Skip to content

Commit 92dc039

Browse files
committed
[r] make key list layout responsive to console width
1 parent a60edb3 commit 92dc039

1 file changed

Lines changed: 20 additions & 19 deletions

File tree

src/EtcdTerminal.App/Screens/KeyBrowseScreen.cs

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@ namespace EtcdTerminal.App.Screens;
99
public sealed class KeyBrowseScreen(IEtcdClient _etcdClient)
1010
{
1111
private const int PageSize = 10;
12+
private const int LinePadding = 2;
13+
private const int PrefixWidth = 4;
14+
private const int SearchBarRow = 4;
15+
private const int EditValueMaxLength = 200;
16+
17+
private static int KeyColumnWidth => (Console.WindowWidth - LinePadding - PrefixWidth - 1) / 2;
18+
private static int ValueColumnWidth => Console.WindowWidth - LinePadding - PrefixWidth - 1 - KeyColumnWidth;
1219

1320
private List<EtcdKeyValue> _allKeys = [];
1421
private List<EtcdKeyValue> _filteredKeys = [];
@@ -18,6 +25,7 @@ public sealed class KeyBrowseScreen(IEtcdClient _etcdClient)
1825
private bool _showActions;
1926
private EtcdKeyValue? _selectedKey;
2027
private EtcdConnectionConfig _config = default!;
28+
private int _searchEndCol;
2129

2230
public async Task ShowAsync(EtcdConnectionConfig config)
2331
{
@@ -97,7 +105,6 @@ private async Task MainLoopAsync()
97105
while (true)
98106
{
99107
Render();
100-
SetCursorAfterSearch();
101108

102109
var keyInfo = Console.ReadKey(true);
103110

@@ -184,8 +191,9 @@ private void Render()
184191
AnsiConsole.Clear();
185192
StatusBar.Render(_config);
186193

187-
Console.Write(" ");
194+
Console.Write(new string(' ', LinePadding));
188195
RenderSearchBar();
196+
_searchEndCol = Console.CursorLeft;
189197

190198
Console.WriteLine();
191199
RenderKeyList();
@@ -194,6 +202,9 @@ private void Render()
194202
RenderPagination();
195203

196204
RenderActionBar();
205+
206+
Console.CursorTop = SearchBarRow;
207+
Console.CursorLeft = _searchEndCol;
197208
}
198209

199210
private void RenderSearchBar()
@@ -215,15 +226,18 @@ private void RenderKeyList()
215226
return;
216227
}
217228

229+
var keyWidth = KeyColumnWidth;
230+
var valueWidth = ValueColumnWidth;
231+
218232
for (var i = 0; i < pageKeys.Count; i++)
219233
{
220234
var kv = pageKeys[i];
221235
var isSelected = i == _selectedIndex;
222236

223237
var prefix = isSelected ? " ▶ " : " ";
224-
var key = TruncateText(kv.Key, 60);
225-
var value = TruncateText(kv.Value, 60);
226-
var line = $"{prefix}{key,-62} {value}";
238+
var key = TruncateText(kv.Key, keyWidth);
239+
var value = TruncateText(kv.Value, valueWidth);
240+
var line = $"{prefix}{key.PadRight(keyWidth)} {value}";
227241

228242
if (isSelected)
229243
AnsiConsole.MarkupLine($" [cyan]{Markup.Escape(line)}[/]");
@@ -266,7 +280,7 @@ private async Task EditKeyAsync()
266280
AnsiConsole.Clear();
267281
StatusBar.Render(_config);
268282
AnsiConsole.MarkupLine($"Editing key: [cyan]{Markup.Escape(_selectedKey.Key)}[/]");
269-
AnsiConsole.MarkupLine($"Current value: [green]{Markup.Escape(TruncateText(_selectedKey.Value, 200))}[/]");
283+
AnsiConsole.MarkupLine($"Current value: [green]{Markup.Escape(TruncateText(_selectedKey.Value, EditValueMaxLength))}[/]");
270284
Console.WriteLine();
271285

272286
var newValue = Prompt.Ask("Enter new value:", _selectedKey.Value);
@@ -381,19 +395,6 @@ private int GetPageItemCount() =>
381395
private int GetTotalPages() =>
382396
_filteredKeys.Count == 0 ? 1 : (int)Math.Ceiling((double)_filteredKeys.Count / PageSize);
383397

384-
private void SetCursorAfterSearch()
385-
{
386-
var col = 2;
387-
388-
if (_searchQuery.Length == 0)
389-
col += 27;
390-
else
391-
col += 3 + _searchQuery.Length;
392-
393-
Console.CursorTop = 4;
394-
Console.CursorLeft = col;
395-
}
396-
397398
private static string TruncateText(string text, int maxLength) =>
398399
text.Length <= maxLength ? text : text[..maxLength] + "...";
399400
}

0 commit comments

Comments
 (0)