feat: Add CommandTool for user-approved shell command execution, integrating it with a custom spinner that can be paused during interaction.
This commit is contained in:
@@ -10,6 +10,8 @@ namespace AnchorCli.Tools;
|
||||
internal static class CommandTool
|
||||
{
|
||||
public static Action<string> Log { get; set; } = Console.WriteLine;
|
||||
public static Action? PauseSpinner { get; set; }
|
||||
public static Action? ResumeSpinner { get; set; }
|
||||
|
||||
#if WINDOWS
|
||||
[Description("Execute a PowerShell command after user approval. Prompts with [Y/n] before running. Note: For file editing and operations, use the built-in file tools (ReadFile, ReplaceLines, InsertAfter, DeleteRange, CreateFile, etc.) instead of shell commands.")]
|
||||
@@ -22,22 +24,34 @@ internal static class CommandTool
|
||||
Log($"Command request: {command}");
|
||||
|
||||
// Prompt for user approval
|
||||
AnsiConsole.WriteLine();
|
||||
AnsiConsole.Write(
|
||||
new Panel($"[yellow]{Markup.Escape(command)}[/]")
|
||||
.Header("[bold yellow] Run command? [/]")
|
||||
.BorderColor(Color.Yellow)
|
||||
.RoundedBorder()
|
||||
.Padding(1, 0));
|
||||
|
||||
// Drain any buffered keystrokes so stale input doesn't auto-answer
|
||||
while (Console.KeyAvailable)
|
||||
Console.ReadKey(intercept: true);
|
||||
|
||||
if (!AnsiConsole.Confirm("Execute?", defaultValue: true))
|
||||
PauseSpinner?.Invoke();
|
||||
try
|
||||
{
|
||||
AnsiConsole.MarkupLine("[dim grey] ✗ Cancelled by user[/]");
|
||||
return "ERROR: Command execution cancelled by user";
|
||||
AnsiConsole.WriteLine();
|
||||
AnsiConsole.Write(
|
||||
new Panel($"[yellow]{Markup.Escape(command)}[/]")
|
||||
.Header("[bold yellow] Run command? [/]")
|
||||
.BorderColor(Color.Yellow)
|
||||
.RoundedBorder()
|
||||
.Padding(1, 0));
|
||||
|
||||
// Drain any buffered keystrokes so stale input doesn't auto-answer
|
||||
try
|
||||
{
|
||||
while (Console.KeyAvailable)
|
||||
Console.ReadKey(intercept: true);
|
||||
}
|
||||
catch { }
|
||||
|
||||
if (!AnsiConsole.Confirm("Execute?", defaultValue: true))
|
||||
{
|
||||
AnsiConsole.MarkupLine("[dim grey] ✗ Cancelled by user[/]");
|
||||
return "ERROR: Command execution cancelled by user";
|
||||
}
|
||||
}
|
||||
finally
|
||||
{
|
||||
ResumeSpinner?.Invoke();
|
||||
}
|
||||
|
||||
// Execute the command
|
||||
|
||||
Reference in New Issue
Block a user