mirror of
https://github.com/nsnail/spectre.console.git
synced 2025-04-16 00:42:51 +08:00
Backward direction of autocomplete with Tab + Shift
This commit is contained in:
parent
a60910c15f
commit
1082ac1efc
@ -33,7 +33,10 @@ public static partial class AnsiConsoleExtensions
|
||||
|
||||
if (key.Key == ConsoleKey.Tab && autocomplete.Count > 0)
|
||||
{
|
||||
var replace = AutoComplete(autocomplete, text);
|
||||
var autoCompleteDirection = key.Modifiers.HasFlag(ConsoleModifiers.Shift)
|
||||
? AutoCompleteDirection.Backward
|
||||
: AutoCompleteDirection.Forward;
|
||||
var replace = AutoComplete(autocomplete, text, autoCompleteDirection);
|
||||
if (!string.IsNullOrEmpty(replace))
|
||||
{
|
||||
// Render the suggestion
|
||||
@ -63,7 +66,7 @@ public static partial class AnsiConsoleExtensions
|
||||
}
|
||||
}
|
||||
|
||||
private static string AutoComplete(List<string> autocomplete, string text)
|
||||
private static string AutoComplete(List<string> autocomplete, string text, AutoCompleteDirection autoCompleteDirection)
|
||||
{
|
||||
var found = autocomplete.Find(i => i == text);
|
||||
var replace = string.Empty;
|
||||
@ -85,15 +88,38 @@ public static partial class AnsiConsoleExtensions
|
||||
else
|
||||
{
|
||||
// Get the next match
|
||||
var index = autocomplete.IndexOf(found) + 1;
|
||||
if (index >= autocomplete.Count)
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
|
||||
replace = autocomplete[index];
|
||||
replace = GetAutocompleteValue(autoCompleteDirection, autocomplete, found);
|
||||
}
|
||||
|
||||
return replace;
|
||||
}
|
||||
|
||||
private static string GetAutocompleteValue(AutoCompleteDirection autoCompleteDirection, IList<string> autocomplete, string found)
|
||||
{
|
||||
var foundAutocompleteIndex = autocomplete.IndexOf(found);
|
||||
var index = autoCompleteDirection switch
|
||||
{
|
||||
AutoCompleteDirection.Forward => foundAutocompleteIndex + 1,
|
||||
AutoCompleteDirection.Backward => foundAutocompleteIndex - 1,
|
||||
_ => throw new ArgumentOutOfRangeException(nameof(autoCompleteDirection), autoCompleteDirection, null),
|
||||
};
|
||||
|
||||
if (index >= autocomplete.Count)
|
||||
{
|
||||
index = 0;
|
||||
}
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
index = autocomplete.Count - 1;
|
||||
}
|
||||
|
||||
return autocomplete[index];
|
||||
}
|
||||
|
||||
private enum AutoCompleteDirection
|
||||
{
|
||||
Forward,
|
||||
Backward,
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user