summaryrefslogtreecommitdiffstats
path: root/pkg/gui/file_helper.go
blob: 2e32e168fe6c1a199aeec9c44ea327150c9963be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package gui

import (
	"github.com/jesseduffield/lazygit/pkg/commands"
	"github.com/jesseduffield/lazygit/pkg/commands/oscommands"
	"github.com/jesseduffield/lazygit/pkg/gui/controllers"
	"github.com/jesseduffield/lazygit/pkg/gui/types"
)

type FilesHelper struct {
	c   *types.ControllerCommon
	git *commands.GitCommand
	os  *oscommands.OSCommand
}

func NewFilesHelper(
	c *types.ControllerCommon,
	git *commands.GitCommand,
	os *oscommands.OSCommand,
) *FilesHelper {
	return &FilesHelper{
		c:   c,
		git: git,
		os:  os,
	}
}

var _ controllers.IFileHelper = &FilesHelper{}

func (self *FilesHelper) EditFile(filename string) error {
	return self.EditFileAtLine(filename, 1)
}

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),
	)
}

func (self *FilesHelper) OpenFile(filename string) error {
	self.c.LogAction(self.c.Tr.Actions.OpenFile)
	if err := self.os.OpenFile(filename); err != nil {
		return self.c.Error(err)
	}
	return nil
}