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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
package helpers
import (
"path/filepath"
)
type IFilesHelper interface {
EditFile(filename string) error
EditFileAtLine(filename string, lineNumber int) error
OpenFile(filename string) error
}
type FilesHelper struct {
c *HelperCommon
}
func NewFilesHelper(c *HelperCommon) *FilesHelper {
return &FilesHelper{
c: c,
}
}
var _ IFilesHelper = &FilesHelper{}
func (self *FilesHelper) EditFile(filename string) error {
absPath, err := filepath.Abs(filename)
if err != nil {
return err
}
cmdStr, suspend := self.c.Git().File.GetEditCmdStr(absPath)
return self.callEditor(cmdStr, suspend)
}
func (self *FilesHelper) EditFileAtLine(filename string, lineNumber int) error {
absPath, err := filepath.Abs(filename)
if err != nil {
return err
}
cmdStr, suspend := self.c.Git().File.GetEditAtLineCmdStr(absPath, lineNumber)
return self.callEditor(cmdStr, suspend)
}
func (self *FilesHelper) EditFileAtLineAndWait(filename string, lineNumber int) error {
absPath, err := filepath.Abs(filename)
if err != nil {
return err
}
cmdStr := self.c.Git().File.GetEditAtLineAndWaitCmdStr(absPath, lineNumber)
// Always suspend, regardless of the value of the suspend 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) OpenDirInEditor(path string) error {
absPath, err := filepath.Abs(path)
if err != nil {
return err
}
cmdStr, suspend := self.c.Git().File.GetOpenDirInEditorCmdStr(absPath)
return self.callEditor(cmdStr, suspend)
}
func (self *FilesHelper) callEditor(cmdStr string, suspend bool) error {
if suspend {
return self.c.RunSubprocessAndRefresh(
self.c.OS().Cmd.NewShell(cmdStr),
)
}
return self.c.OS().Cmd.NewShell(cmdStr).Run()
}
func (self *FilesHelper) OpenFile(filename string) error {
absPath, err := filepath.Abs(filename)
if err != nil {
return err
}
self.c.LogAction(self.c.Tr.Actions.OpenFile)
if err := self.c.OS().OpenFile(absPath); err != nil {
return self.c.Error(err)
}
return nil
}
|