diff --git a/shell/AIShell.Kernel/Command/AgentCommand.cs b/shell/AIShell.Kernel/Command/AgentCommand.cs index 7d993fd5..b7fa63e2 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 shell execute to open file in 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