1
0
mirror of https://github.com/jesseduffield/lazygit.git synced 2024-12-14 11:23:09 +02:00
lazygit/pkg/gui/controllers/helpers/files_helper.go

63 lines
1.4 KiB
Go
Raw Normal View History

2022-02-06 06:54:26 +02:00
package helpers
import (
"github.com/jesseduffield/lazygit/pkg/commands"
"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
2022-01-29 10:09:20 +02:00
"github.com/jesseduffield/lazygit/pkg/gui/types"
)
2022-01-31 13:11:34 +02:00
type IFilesHelper interface {
EditFile(filename string) error
EditFileAtLine(filename string, lineNumber int) error
OpenFile(filename string) error
2022-02-06 04:42:17 +02:00
OpenFileAtLine(filename string, lineNumber int) error
2022-01-31 13:11:34 +02:00
}
2022-01-30 01:23:39 +02:00
type FilesHelper struct {
2022-02-06 06:54:26 +02:00
c *types.HelperCommon
git *commands.GitCommand
os *oscommands.OSCommand
}
2022-01-30 01:23:39 +02:00
func NewFilesHelper(
2022-02-06 06:54:26 +02:00
c *types.HelperCommon,
git *commands.GitCommand,
os *oscommands.OSCommand,
2022-01-30 01:23:39 +02:00
) *FilesHelper {
return &FilesHelper{
c: c,
git: git,
os: os,
}
}
2022-01-31 13:11:34 +02:00
var _ IFilesHelper = &FilesHelper{}
2022-01-30 01:23:39 +02:00
func (self *FilesHelper) EditFile(filename string) error {
return self.EditFileAtLine(filename, 1)
}
2022-01-30 01:23:39 +02:00
func (self *FilesHelper) EditFileAtLine(filename string, lineNumber int) error {
cmdStr, err := self.git.File.GetEditCmdStr(filename, lineNumber)
if err != nil {
return self.c.Error(err)
}
self.c.LogAction(self.c.Tr.Actions.EditFile)
return self.c.RunSubprocessAndRefresh(
self.os.Cmd.NewShell(cmdStr),
)
}
2022-01-30 01:23:39 +02:00
func (self *FilesHelper) OpenFile(filename string) error {
2022-02-06 04:42:17 +02:00
return self.OpenFileAtLine(filename, 1)
}
func (self *FilesHelper) OpenFileAtLine(filename string, lineNumber int) error {
self.c.LogAction(self.c.Tr.Actions.OpenFile)
2022-02-06 04:42:17 +02:00
if err := self.os.OpenFileAtLine(filename, lineNumber); err != nil {
return self.c.Error(err)
}
return nil
}