diff options
author | Stefan Haller <stefan@haller-berlin.de> | 2023-03-26 14:04:09 +0200 |
---|---|---|
committer | Stefan Haller <stefan@haller-berlin.de> | 2023-04-13 13:14:00 +0200 |
commit | 659d668e16c3aac63ca7c41bba06003b6b1f8023 (patch) | |
tree | c35e2369269851793d05b9d666ddb002c0b9068d /pkg/commands/git_commands/file.go | |
parent | 7bbcec965bb6606c661287f3a729a1de08debd81 (diff) |
Implement edit presets
Diffstat (limited to 'pkg/commands/git_commands/file.go')
-rw-r--r-- | pkg/commands/git_commands/file.go | 62 |
1 files changed, 61 insertions, 1 deletions
diff --git a/pkg/commands/git_commands/file.go b/pkg/commands/git_commands/file.go index 111733bb7..3ceef2d9b 100644 --- a/pkg/commands/git_commands/file.go +++ b/pkg/commands/git_commands/file.go @@ -3,8 +3,10 @@ package git_commands import ( "os" "strconv" + "strings" "github.com/go-errors/errors" + "github.com/jesseduffield/lazygit/pkg/config" "github.com/jesseduffield/lazygit/pkg/utils" ) @@ -27,7 +29,7 @@ func (self *FileCommands) Cat(fileName string) (string, error) { return string(buf), nil } -func (self *FileCommands) GetEditCmdStr(filename string, lineNumber int) (string, error) { +func (self *FileCommands) GetEditCmdStrLegacy(filename string, lineNumber int) (string, error) { editor := self.UserConfig.OS.EditCommand if editor == "" { @@ -72,3 +74,61 @@ func (self *FileCommands) GetEditCmdStr(filename string, lineNumber int) (string } return utils.ResolvePlaceholderString(editCmdTemplate, templateValues), nil } + +func (self *FileCommands) GetEditCmdStr(filename string) (string, bool) { + template, editInTerminal := config.GetEditTemplate(&self.UserConfig.OS, self.guessDefaultEditor) + + templateValues := map[string]string{ + "filename": self.cmd.Quote(filename), + } + + cmdStr := utils.ResolvePlaceholderString(template, templateValues) + return cmdStr, editInTerminal +} + +func (self *FileCommands) GetEditAtLineCmdStr(filename string, lineNumber int) (string, bool) { + template, editInTerminal := config.GetEditAtLineTemplate(&self.UserConfig.OS, self.guessDefaultEditor) + + templateValues := map[string]string{ + "filename": self.cmd.Quote(filename), + "line": strconv.Itoa(lineNumber), + } + + cmdStr := utils.ResolvePlaceholderString(template, templateValues) + return cmdStr, editInTerminal +} + +func (self *FileCommands) GetEditAtLineAndWaitCmdStr(filename string, lineNumber int) string { + template := config.GetEditAtLineAndWaitTemplate(&self.UserConfig.OS, self.guessDefaultEditor) + + templateValues := map[string]string{ + "filename": self.cmd.Quote(filename), + "line": strconv.Itoa(lineNumber), + } + + cmdStr := utils.ResolvePlaceholderString(template, templateValues) + return cmdStr +} + +func (self *FileCommands) guessDefaultEditor() string { + // Try to query a few places where editors get configured + editor := self.config.GetCoreEditor() + if editor == "" { + editor = self.os.Getenv("GIT_EDITOR") + } + if editor == "" { + editor = self.os.Getenv("VISUAL") + } + if editor == "" { + editor = self.os.Getenv("EDITOR") + } + + if editor != "" { + // At this point, it might be more than just the name of the editor; + // e.g. it might be "code -w" or "vim -u myvim.rc". So assume that + // everything up to the first space is the editor name. + editor = strings.Split(editor, " ")[0] + } + + return editor +} |