Skip to content

Commit 7f35f66

Browse files
authored
Use nano or $EDITOR (if defined) to open the config file on Linux (#318)
1 parent 2aefb25 commit 7f35f66

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

shell/AIShell.Kernel/Command/AgentCommand.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ private void ConfigAgentAction(string name, string editor)
163163

164164
Process.Start(info);
165165
}
166-
else
166+
else if (OperatingSystem.IsMacOS())
167167
{
168-
// On macOS and Linux, we just depend on the default editor.
168+
// On macOS, we just depend on shell execute to open file in the default editor.
169169
FileInfo fileInfo = new(settingFile);
170170
if (fileInfo.Exists)
171171
{
@@ -189,6 +189,19 @@ private void ConfigAgentAction(string name, string editor)
189189
info = new(settingFile) { UseShellExecute = true };
190190
Process.Start(info);
191191
}
192+
else
193+
{
194+
(string exe, string[] args) = GetDefaultEditorForLinux();
195+
196+
info = new(exe);
197+
foreach (string arg in args)
198+
{
199+
info.ArgumentList.Add(arg);
200+
}
201+
202+
info.ArgumentList.Add(settingFile);
203+
Process.Start(info).WaitForExit();
204+
}
192205
}
193206
catch (Exception ex)
194207
{
@@ -228,6 +241,25 @@ internal static void AgentNotFound(string name, Shell shell)
228241
string availableAgentNames = string.Join(", ", shell.Agents.Select(AgentName));
229242
shell.Host.WriteErrorLine($"Cannot find an agent with the name '{name}'. Available agent(s): {availableAgentNames}.");
230243
}
244+
245+
private static (string editor, string[] args) GetDefaultEditorForLinux()
246+
{
247+
string editor = Environment.GetEnvironmentVariable("EDITOR");
248+
if (string.IsNullOrWhiteSpace(editor))
249+
{
250+
return ("nano", Array.Empty<string>());
251+
}
252+
253+
editor = editor.Trim();
254+
int index = editor.IndexOf(' ');
255+
if (index is -1)
256+
{
257+
return (editor, Array.Empty<string>());
258+
}
259+
260+
string[] args = editor[(index + 1)..].Split(' ', StringSplitOptions.RemoveEmptyEntries);
261+
return (editor[..index], args);
262+
}
231263
}
232264

233265
internal static partial class Interop

0 commit comments

Comments
 (0)