From dc6b69932c6cf5c87c540d332ea7707f6c000a2e Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 6 Dec 2024 14:52:16 -0800 Subject: [PATCH 1/2] Use nano or $EDITOR (if defined) to open the config file on Linux --- shell/AIShell.Kernel/Command/AgentCommand.cs | 36 ++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/shell/AIShell.Kernel/Command/AgentCommand.cs b/shell/AIShell.Kernel/Command/AgentCommand.cs index 7d993fd5..ffc742eb 100644 --- a/shell/AIShell.Kernel/Command/AgentCommand.cs +++ b/shell/AIShell.Kernel/Command/AgentCommand.cs @@ -163,9 +163,9 @@ private void ConfigAgentAction(string name, string editor) Process.Start(info); } - else + else if (OperatingSystem.IsMacOS()) { - // On macOS and Linux, we just depend on the default editor. + // On macOS, we just depend on the default editor. FileInfo fileInfo = new(settingFile); if (fileInfo.Exists) { @@ -189,6 +189,19 @@ private void ConfigAgentAction(string name, string editor) info = new(settingFile) { UseShellExecute = true }; Process.Start(info); } + else + { + (string exe, string[] args) = GetDefaultEditorForLinux(); + + info = new(exe); + foreach (string arg in args) + { + info.ArgumentList.Add(arg); + } + + info.ArgumentList.Add(settingFile); + Process.Start(info).WaitForExit(); + } } catch (Exception ex) { @@ -228,6 +241,25 @@ internal static void AgentNotFound(string name, Shell shell) string availableAgentNames = string.Join(", ", shell.Agents.Select(AgentName)); shell.Host.WriteErrorLine($"Cannot find an agent with the name '{name}'. Available agent(s): {availableAgentNames}."); } + + private static (string editor, string[] args) GetDefaultEditorForLinux() + { + string editor = Environment.GetEnvironmentVariable("EDITOR"); + if (string.IsNullOrWhiteSpace(editor)) + { + return ("nano", Array.Empty()); + } + + editor = editor.Trim(); + int index = editor.IndexOf(' '); + if (index is -1) + { + return (editor, Array.Empty()); + } + + string[] args = editor[(index + 1)..].Split(' ', StringSplitOptions.RemoveEmptyEntries); + return (editor[..index], args); + } } internal static partial class Interop From 0eeda150653ee9092f089cf7267220730754e228 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 6 Dec 2024 15:04:13 -0800 Subject: [PATCH 2/2] Update comment --- shell/AIShell.Kernel/Command/AgentCommand.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shell/AIShell.Kernel/Command/AgentCommand.cs b/shell/AIShell.Kernel/Command/AgentCommand.cs index ffc742eb..b7fa63e2 100644 --- a/shell/AIShell.Kernel/Command/AgentCommand.cs +++ b/shell/AIShell.Kernel/Command/AgentCommand.cs @@ -165,7 +165,7 @@ private void ConfigAgentAction(string name, string editor) } else if (OperatingSystem.IsMacOS()) { - // On macOS, we just depend on the default editor. + // On macOS, we just depend on shell execute to open file in the default editor. FileInfo fileInfo = new(settingFile); if (fileInfo.Exists) {