From 999e170f1d17b652dad231f1cbde6ab4bbeae8c7 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Wed, 25 Nov 2020 08:52:00 +1100 Subject: standardise how we read from the config --- pkg/commands/files.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'pkg/commands/files.go') diff --git a/pkg/commands/files.go b/pkg/commands/files.go index 05aa89978..93d1ccf03 100644 --- a/pkg/commands/files.go +++ b/pkg/commands/files.go @@ -2,6 +2,7 @@ package commands import ( "fmt" + "os/exec" "path/filepath" "strings" "time" @@ -9,6 +10,7 @@ import ( "github.com/go-errors/errors" "github.com/jesseduffield/lazygit/pkg/commands/models" "github.com/jesseduffield/lazygit/pkg/utils" + "github.com/mgutz/str" ) // CatFile obtains the content of a file @@ -262,3 +264,28 @@ func (c *GitCommand) ResetAndClean() error { return c.RemoveUntrackedFiles() } + +// EditFile opens a file in a subprocess using whatever editor is available, +// falling back to core.editor, VISUAL, EDITOR, then vi +func (c *GitCommand) EditFile(filename string) (*exec.Cmd, error) { + editor, _ := c.getGlobalGitConfig("core.editor") + + if editor == "" { + editor = c.OSCommand.Getenv("VISUAL") + } + if editor == "" { + editor = c.OSCommand.Getenv("EDITOR") + } + if editor == "" { + if err := c.OSCommand.RunCommand("which vi"); err == nil { + editor = "vi" + } + } + if editor == "" { + return nil, errors.New("No editor defined in $VISUAL, $EDITOR, or git config") + } + + splitCmd := str.ToArgv(fmt.Sprintf("%s %s", editor, c.OSCommand.Quote(filename))) + + return c.OSCommand.PrepareSubProcess(splitCmd[0], splitCmd[1:]...), nil +} -- cgit v1.2.3