2022-01-08 14:00:36 +11:00
|
|
|
package git_commands
|
2020-09-29 20:03:39 +10:00
|
|
|
|
|
|
|
import (
|
2022-09-13 18:11:03 +08:00
|
|
|
"os"
|
2021-08-03 21:38:03 +09:00
|
|
|
"strconv"
|
2020-09-29 20:03:39 +10:00
|
|
|
|
|
|
|
"github.com/go-errors/errors"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/utils"
|
|
|
|
)
|
|
|
|
|
2022-01-02 10:34:33 +11:00
|
|
|
type FileCommands struct {
|
2022-01-18 21:26:21 +11:00
|
|
|
*GitCommon
|
2020-09-29 20:03:39 +10:00
|
|
|
}
|
|
|
|
|
2022-01-18 21:26:21 +11:00
|
|
|
func NewFileCommands(gitCommon *GitCommon) *FileCommands {
|
2022-01-02 10:34:33 +11:00
|
|
|
return &FileCommands{
|
2022-01-18 21:26:21 +11:00
|
|
|
GitCommon: gitCommon,
|
2020-09-29 20:03:39 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 10:34:33 +11:00
|
|
|
// Cat obtains the content of a file
|
|
|
|
func (self *FileCommands) Cat(fileName string) (string, error) {
|
2022-09-13 18:11:03 +08:00
|
|
|
buf, err := os.ReadFile(fileName)
|
2020-09-29 20:03:39 +10:00
|
|
|
if err != nil {
|
2022-01-02 10:34:33 +11:00
|
|
|
return "", nil
|
2020-09-29 20:03:39 +10:00
|
|
|
}
|
2022-01-02 10:34:33 +11:00
|
|
|
return string(buf), nil
|
2020-09-29 20:03:39 +10:00
|
|
|
}
|
2020-11-25 08:52:00 +11:00
|
|
|
|
2022-01-08 14:00:36 +11:00
|
|
|
func (self *FileCommands) GetEditCmdStr(filename string, lineNumber int) (string, error) {
|
|
|
|
editor := self.UserConfig.OS.EditCommand
|
2021-05-19 23:44:58 -07:00
|
|
|
|
|
|
|
if editor == "" {
|
2022-01-08 14:00:36 +11:00
|
|
|
editor = self.config.GetCoreEditor()
|
2021-05-19 23:44:58 -07:00
|
|
|
}
|
2021-04-01 20:27:06 +11:00
|
|
|
if editor == "" {
|
2022-01-08 14:00:36 +11:00
|
|
|
editor = self.os.Getenv("GIT_EDITOR")
|
2021-04-01 20:27:06 +11:00
|
|
|
}
|
2020-11-25 08:52:00 +11:00
|
|
|
if editor == "" {
|
2022-01-08 14:00:36 +11:00
|
|
|
editor = self.os.Getenv("VISUAL")
|
2020-11-25 08:52:00 +11:00
|
|
|
}
|
|
|
|
if editor == "" {
|
2022-01-08 14:00:36 +11:00
|
|
|
editor = self.os.Getenv("EDITOR")
|
2020-11-25 08:52:00 +11:00
|
|
|
}
|
|
|
|
if editor == "" {
|
2022-01-08 14:00:36 +11:00
|
|
|
if err := self.cmd.New("which vi").DontLog().Run(); err == nil {
|
2020-11-25 08:52:00 +11:00
|
|
|
editor = "vi"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if editor == "" {
|
2021-05-31 23:23:33 -07:00
|
|
|
return "", errors.New("No editor defined in config file, $GIT_EDITOR, $VISUAL, $EDITOR, or git config")
|
2021-04-10 11:40:42 +10:00
|
|
|
}
|
|
|
|
|
2021-08-03 21:38:03 +09:00
|
|
|
templateValues := map[string]string{
|
|
|
|
"editor": editor,
|
2022-01-08 14:00:36 +11:00
|
|
|
"filename": self.cmd.Quote(filename),
|
2021-08-03 21:38:03 +09:00
|
|
|
"line": strconv.Itoa(lineNumber),
|
|
|
|
}
|
|
|
|
|
2022-01-08 14:00:36 +11:00
|
|
|
editCmdTemplate := self.UserConfig.OS.EditCommandTemplate
|
2022-04-22 21:54:03 +09:00
|
|
|
if len(editCmdTemplate) == 0 {
|
2022-02-15 19:34:36 +01:00
|
|
|
switch editor {
|
2022-04-22 21:54:03 +09:00
|
|
|
case "emacs", "nano", "vi", "vim", "nvim":
|
|
|
|
editCmdTemplate = "{{editor}} +{{line}} -- {{filename}}"
|
2022-02-15 19:34:36 +01:00
|
|
|
case "subl":
|
2022-04-22 21:54:03 +09:00
|
|
|
editCmdTemplate = "{{editor}} -- {{filename}}:{{line}}"
|
2022-02-15 19:34:36 +01:00
|
|
|
case "code":
|
2022-04-22 21:54:03 +09:00
|
|
|
editCmdTemplate = "{{editor}} -r --goto -- {{filename}}:{{line}}"
|
|
|
|
default:
|
|
|
|
editCmdTemplate = "{{editor}} -- {{filename}}"
|
2022-02-15 19:34:36 +01:00
|
|
|
}
|
|
|
|
}
|
2021-08-04 18:43:34 +09:00
|
|
|
return utils.ResolvePlaceholderString(editCmdTemplate, templateValues), nil
|
2021-04-10 11:40:42 +10:00
|
|
|
}
|