Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static async Task Main(string[] args)
Console.WriteLine();

ConsoleKeyInfo key;
ConsoleKeyInfo promptKey;
string inputBuffer = "";
history.ResetIndex();

Expand Down Expand Up @@ -124,6 +125,85 @@ public static async Task Main(string[] args)
Console.Write(new string('\b', inputBuffer.Length) + new string(' ', inputBuffer.Length) + new string('\b', inputBuffer.Length));
Console.Write(inputBuffer);
}
} else if (inputBuffer.Length > 0) {
HashSet<string> suggestCommands = new();
HashSet<string> allCommands = CommandParser.CustomCommands.Keys
.Concat(CommandParser.SystemCommands)
.ToHashSet();

foreach (var suggestion in allCommands)
{
if (suggestion.StartsWith(inputBuffer))
{
suggestCommands.Add(suggestion);
}
}

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to merge, maybe for clean code reason make a keyboard handler

if (suggestCommands.Count > 15)
{
bool showFullList = false;
AnsiConsole.MarkupLine($"[[[yellow]*[/]]] - Do you want to list all [yellow]{suggestCommands.Count}[/] commands ? ([yellow]y[/]/[yellow]n[/]) >> ");
while (true)
{
promptKey = Console.ReadKey(intercept: true);
if (promptKey.Key == ConsoleKey.Y || promptKey.Key == ConsoleKey.N)
{
if (promptKey.Key == ConsoleKey.Y)
{
showFullList = true;
}
break;
}
}
if (showFullList)
{
var sortedCommands = suggestCommands.OrderBy(c => c.Length).ToArray();
int i = 0;

foreach (var cmd in sortedCommands)
{
if (i == 7)
{
AnsiConsole.MarkupLine("");
i = 0;
}
AnsiConsole.Markup($"{cmd} ");
i++;
}

inputBuffer = string.Empty;
AnsiConsole.MarkupLine("");
AnsiConsole.MarkupLine("[[[yellow]*[/]]] - Press enter to continue...");
}
}
else
{
var sortedCommands = suggestCommands.OrderBy(c => c.Length).ToArray();
List<string> commandsList = sortedCommands.ToList();

int currentIndex = 0;

if (!string.IsNullOrEmpty(inputBuffer))
{
for (int i = 0; i < commandsList.Count; i++)
{
if (commandsList[i].StartsWith(inputBuffer))
{
currentIndex = (i + 1) % commandsList.Count;
inputBuffer = commandsList[currentIndex];
break;
}
}
}

Console.SetCursorPosition(0, Console.CursorTop - 1);
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write(new string(' ', Console.WindowWidth));
Console.SetCursorPosition(0, Console.CursorTop);

AnsiConsole.Markup(context.GetPrompt());
Console.Write(inputBuffer);
}
}
}
else if (!char.IsControl(key.KeyChar))
Expand Down
14 changes: 7 additions & 7 deletions Shell/Commands/CommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
public class CommandParser
{

private readonly Dictionary<string, ICustomCommand> _commands = new();
private readonly HashSet<string> _systemCommands = new();
public static readonly Dictionary<string, ICustomCommand> CustomCommands = new();
public static readonly HashSet<string> SystemCommands = new();
private static readonly HashSet<string> InteractiveCommands = new()
{
"vim", "nano", "less", "more", "top", "htop", "man", "ssh", "apt"
Expand All @@ -33,11 +33,11 @@
{
foreach (var command in CommandRegistry.GetAll())
{
_commands[command.Name] = command;
CustomCommands[command.Name] = command;
AnsiConsole.MarkupLine($"\t[[[green]+[/]]] - Loaded custom command: [yellow]{command.Name}[/]");
}

var TotalCommands = _commands.Count + _systemCommands.Count;
var TotalCommands = CustomCommands.Count + SystemCommands.Count;

LoadSystemCommands();

Expand Down Expand Up @@ -69,7 +69,7 @@

foreach (var cmd in commands)
{
_systemCommands.Add(cmd);
SystemCommands.Add(cmd);

Check warning on line 72 in Shell/Commands/CommandParser.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'item' in 'bool HashSet<string>.Add(string item)'.
var safeCmd = EscapeMarkup(cmd);
//AnsiConsole.MarkupLine($"\t[[[green]+[/]]] Loaded system command: [yellow]{safeCmd}[/]");
}
Expand Down Expand Up @@ -143,7 +143,7 @@
}
}

if (_commands.TryGetValue(cmdName, out var command))
if (CustomCommands.TryGetValue(cmdName, out var command))
{
if (command is IMetadataCommand meta && meta.RequiresRoot && !(usedSudo || IsRootUser()))
{
Expand All @@ -155,7 +155,7 @@
return true;
}

if (_systemCommands.Contains(cmdName))
if (SystemCommands.Contains(cmdName))
{
var fullPath = ResolveSystemCommandPath(cmdName);
if (fullPath != null)
Expand Down
Loading