2022-02-06 06:54:26 +02:00
|
|
|
package helpers
|
2022-01-16 05:46:53 +02:00
|
|
|
|
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-01-30 01:23:39 +02:00
|
|
|
type FilesHelper struct {
|
2023-03-23 03:53:18 +02:00
|
|
|
c *HelperCommon
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
2023-03-23 03:53:18 +02:00
|
|
|
func NewFilesHelper(c *HelperCommon) *FilesHelper {
|
2022-01-30 01:23:39 +02:00
|
|
|
return &FilesHelper{
|
2023-03-23 03:53:18 +02:00
|
|
|
c: c,
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-31 13:11:34 +02:00
|
|
|
var _ IFilesHelper = &FilesHelper{}
|
2022-01-16 05:46:53 +02:00
|
|
|
|
2022-01-30 01:23:39 +02:00
|
|
|
func (self *FilesHelper) EditFile(filename string) error {
|
2023-08-09 13:34:51 +02:00
|
|
|
cmdStr, suspend := self.c.Git().File.GetEditCmdStr(filename)
|
|
|
|
return self.callEditor(cmdStr, suspend)
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
2022-01-30 01:23:39 +02:00
|
|
|
func (self *FilesHelper) EditFileAtLine(filename string, lineNumber int) error {
|
2023-08-09 13:34:51 +02:00
|
|
|
cmdStr, suspend := self.c.Git().File.GetEditAtLineCmdStr(filename, lineNumber)
|
|
|
|
return self.callEditor(cmdStr, suspend)
|
2023-03-26 14:04:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (self *FilesHelper) EditFileAtLineAndWait(filename string, lineNumber int) error {
|
2023-03-23 03:53:18 +02:00
|
|
|
cmdStr := self.c.Git().File.GetEditAtLineAndWaitCmdStr(filename, lineNumber)
|
2023-03-26 14:04:09 +02:00
|
|
|
|
2023-08-09 13:34:51 +02:00
|
|
|
// Always suspend, regardless of the value of the suspend config,
|
2023-03-26 14:04:09 +02:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2023-07-17 07:45:10 +02:00
|
|
|
func (self *FilesHelper) OpenDirInEditor(path string) error {
|
2023-08-09 13:34:51 +02:00
|
|
|
cmdStr, suspend := self.c.Git().File.GetOpenDirInEditorCmdStr(path)
|
2023-07-17 07:45:10 +02:00
|
|
|
|
2023-08-09 13:34:51 +02:00
|
|
|
return self.callEditor(cmdStr, suspend)
|
2023-07-17 07:45:10 +02:00
|
|
|
}
|
|
|
|
|
2023-08-09 13:34:51 +02:00
|
|
|
func (self *FilesHelper) callEditor(cmdStr string, suspend bool) error {
|
|
|
|
if suspend {
|
2023-03-26 14:04:09 +02:00
|
|
|
return self.c.RunSubprocessAndRefresh(
|
2023-03-23 03:53:18 +02:00
|
|
|
self.c.OS().Cmd.NewShell(cmdStr),
|
2023-03-26 14:04:09 +02:00
|
|
|
)
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
2023-03-23 03:53:18 +02:00
|
|
|
return self.c.OS().Cmd.NewShell(cmdStr).Run()
|
2022-01-16 05:46:53 +02:00
|
|
|
}
|
|
|
|
|
2022-01-30 01:23:39 +02:00
|
|
|
func (self *FilesHelper) OpenFile(filename string) error {
|
2022-01-16 05:46:53 +02:00
|
|
|
self.c.LogAction(self.c.Tr.Actions.OpenFile)
|
2023-03-23 03:53:18 +02:00
|
|
|
if err := self.c.OS().OpenFile(filename); err != nil {
|
2022-01-16 05:46:53 +02:00
|
|
|
return self.c.Error(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|