Skip to content

Use nano or $EDITOR (if defined) to open the config file on Linux #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 6, 2024
Merged
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
36 changes: 34 additions & 2 deletions shell/AIShell.Kernel/Command/AgentCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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)
{
Expand Down Expand Up @@ -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<string>());
}

editor = editor.Trim();
int index = editor.IndexOf(' ');
if (index is -1)
{
return (editor, Array.Empty<string>());
}

string[] args = editor[(index + 1)..].Split(' ', StringSplitOptions.RemoveEmptyEntries);
return (editor[..index], args);
}
}

internal static partial class Interop
Expand Down
Loading