2022-02-06 15:54:26 +11:00
|
|
|
package helpers
|
2022-01-16 14:46:53 +11:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands"
|
|
|
|
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
|
2022-01-29 19:09:20 +11:00
|
|
|
"github.com/jesseduffield/lazygit/pkg/gui/types"
|
2022-01-16 14:46:53 +11:00
|
|
|
)
|
|
|
|
|
2022-01-31 22:11:34 +11:00
|
|
|
type IFilesHelper interface {
|
|
|
|
EditFile(filename string) error
|
|
|
|
EditFileAtLine(filename string, lineNumber int) error
|
|
|
|
OpenFile(filename string) error
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:23:39 +11:00
|
|
|
type FilesHelper struct {
|
2022-02-06 15:54:26 +11:00
|
|
|
c *types.HelperCommon
|
2022-01-16 14:46:53 +11:00
|
|
|
git *commands.GitCommand
|
|
|
|
os *oscommands.OSCommand
|
|
|
|
}
|
|
|
|
|
2022-01-30 10:23:39 +11:00
|
|
|
func NewFilesHelper(
|
2022-02-06 15:54:26 +11:00
|
|
|
c *types.HelperCommon,
|
2022-01-16 14:46:53 +11:00
|
|
|
git *commands.GitCommand,
|
|
|
|
os *oscommands.OSCommand,
|
2022-01-30 10:23:39 +11:00
|
|
|
) *FilesHelper {
|
|
|
|
return &FilesHelper{
|
2022-01-16 14:46:53 +11:00
|
|
|
c: c,
|
|
|
|
git: git,
|
|
|
|
os: os,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 22:11:34 +11:00
|
|
|
var _ IFilesHelper = &FilesHelper{}
|
2022-01-16 14:46:53 +11:00
|
|
|
|
2022-01-30 10:23:39 +11:00
|
|
|
func (self *FilesHelper) EditFile(filename string) error {
|
2023-03-26 14:04:09 +02:00
|
|
|
cmdStr, editInTerminal := self.git.File.GetEditCmdStr(filename)
|
|
|
|
return self.callEditor(cmdStr, editInTerminal)
|
2022-01-16 14:46:53 +11:00
|
|
|
}
|
|
|
|
|
2022-01-30 10:23:39 +11:00
|
|
|
func (self *FilesHelper) EditFileAtLine(filename string, lineNumber int) error {
|
2023-03-26 14:04:09 +02:00
|
|
|
cmdStr, editInTerminal := self.git.File.GetEditAtLineCmdStr(filename, lineNumber)
|
|
|
|
return self.callEditor(cmdStr, editInTerminal)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *FilesHelper) EditFileAtLineAndWait(filename string, lineNumber int) error {
|
|
|
|
cmdStr := self.git.File.GetEditAtLineAndWaitCmdStr(filename, lineNumber)
|
|
|
|
|
|
|
|
// Always suspend, regardless of the value of the editInTerminal config,
|
|
|
|
// since we want to prevent interacting with the UI until the editor
|
|
|
|
// returns, even if the editor doesn't use the terminal
|
|
|
|
return self.callEditor(cmdStr, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (self *FilesHelper) callEditor(cmdStr string, editInTerminal bool) error {
|
|
|
|
if editInTerminal {
|
|
|
|
return self.c.RunSubprocessAndRefresh(
|
|
|
|
self.os.Cmd.NewShell(cmdStr),
|
|
|
|
)
|
2022-01-16 14:46:53 +11:00
|
|
|
}
|
|
|
|
|
2023-03-26 14:04:09 +02:00
|
|
|
return self.os.Cmd.NewShell(cmdStr).Run()
|
2022-01-16 14:46:53 +11:00
|
|
|
}
|
|
|
|
|
2022-01-30 10:23:39 +11:00
|
|
|
func (self *FilesHelper) OpenFile(filename string) error {
|
2022-01-16 14:46:53 +11:00
|
|
|
self.c.LogAction(self.c.Tr.Actions.OpenFile)
|
2023-03-28 18:19:03 +02:00
|
|
|
if err := self.os.OpenFile(filename); err != nil {
|
2022-01-16 14:46:53 +11:00
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|