mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-30 03:23:08 +03:00
Add config os.shellFunctionsFile
This commit is contained in:
@ -13,7 +13,9 @@ type ICmdObjBuilder interface {
|
||||
// NewFromArgs takes a slice of strings like []string{"git", "commit"} and returns a new command object.
|
||||
New(args []string) ICmdObj
|
||||
// NewShell takes a string like `git commit` and returns an executable shell command for it e.g. `sh -c 'git commit'`
|
||||
NewShell(commandStr string) ICmdObj
|
||||
// shellFunctionsFile is an optional file path that will be sourced before executing the command. Callers should pass
|
||||
// the value of UserConfig.OS.ShellFunctionsFile.
|
||||
NewShell(commandStr string, shellFunctionsFile string) ICmdObj
|
||||
// Quote wraps a string in quotes with any necessary escaping applied. The reason for bundling this up with the other methods in this interface is that we basically always need to make use of this when creating new command objects.
|
||||
Quote(str string) string
|
||||
}
|
||||
@ -42,7 +44,10 @@ func (self *CmdObjBuilder) NewWithEnviron(args []string, env []string) ICmdObj {
|
||||
}
|
||||
}
|
||||
|
||||
func (self *CmdObjBuilder) NewShell(commandStr string) ICmdObj {
|
||||
func (self *CmdObjBuilder) NewShell(commandStr string, shellFunctionsFile string) ICmdObj {
|
||||
if len(shellFunctionsFile) > 0 {
|
||||
commandStr = fmt.Sprintf("%ssource %s\n%s", self.platform.PrefixForShellFunctionsFile, shellFunctionsFile, commandStr)
|
||||
}
|
||||
quotedCommand := self.quotedCommandString(commandStr)
|
||||
cmdArgs := str.ToArgv(fmt.Sprintf("%s %s %s", self.platform.Shell, self.platform.ShellArg, quotedCommand))
|
||||
|
||||
|
@ -35,11 +35,12 @@ type OSCommand struct {
|
||||
|
||||
// Platform stores the os state
|
||||
type Platform struct {
|
||||
OS string
|
||||
Shell string
|
||||
ShellArg string
|
||||
OpenCommand string
|
||||
OpenLinkCommand string
|
||||
OS string
|
||||
Shell string
|
||||
ShellArg string
|
||||
PrefixForShellFunctionsFile string
|
||||
OpenCommand string
|
||||
OpenLinkCommand string
|
||||
}
|
||||
|
||||
// NewOSCommand os command runner
|
||||
@ -90,7 +91,7 @@ func (c *OSCommand) OpenFile(filename string) error {
|
||||
"filename": c.Quote(filename),
|
||||
}
|
||||
command := utils.ResolvePlaceholderString(commandTemplate, templateValues)
|
||||
return c.Cmd.NewShell(command).Run()
|
||||
return c.Cmd.NewShell(command, c.UserConfig().OS.ShellFunctionsFile).Run()
|
||||
}
|
||||
|
||||
func (c *OSCommand) OpenLink(link string) error {
|
||||
@ -107,7 +108,7 @@ func (c *OSCommand) OpenLink(link string) error {
|
||||
}
|
||||
|
||||
command := utils.ResolvePlaceholderString(commandTemplate, templateValues)
|
||||
return c.Cmd.NewShell(command).Run()
|
||||
return c.Cmd.NewShell(command, c.UserConfig().OS.ShellFunctionsFile).Run()
|
||||
}
|
||||
|
||||
// Quote wraps a message in platform-specific quotation marks
|
||||
@ -296,7 +297,7 @@ func (c *OSCommand) CopyToClipboard(str string) error {
|
||||
cmdStr := utils.ResolvePlaceholderString(c.UserConfig().OS.CopyToClipboardCmd, map[string]string{
|
||||
"text": c.Cmd.Quote(str),
|
||||
})
|
||||
return c.Cmd.NewShell(cmdStr).Run()
|
||||
return c.Cmd.NewShell(cmdStr, c.UserConfig().OS.ShellFunctionsFile).Run()
|
||||
}
|
||||
|
||||
return clipboard.WriteAll(str)
|
||||
@ -307,7 +308,7 @@ func (c *OSCommand) PasteFromClipboard() (string, error) {
|
||||
var err error
|
||||
if c.UserConfig().OS.CopyToClipboardCmd != "" {
|
||||
cmdStr := c.UserConfig().OS.ReadFromClipboardCmd
|
||||
s, err = c.Cmd.NewShell(cmdStr).RunWithOutput()
|
||||
s, err = c.Cmd.NewShell(cmdStr, c.UserConfig().OS.ShellFunctionsFile).RunWithOutput()
|
||||
} else {
|
||||
s, err = clipboard.ReadAll()
|
||||
}
|
||||
@ -357,5 +358,5 @@ func (c *OSCommand) UpdateWindowTitle() error {
|
||||
return getWdErr
|
||||
}
|
||||
argString := fmt.Sprint("title ", filepath.Base(path), " - Lazygit")
|
||||
return c.Cmd.NewShell(argString).Run()
|
||||
return c.Cmd.NewShell(argString, c.UserConfig().OS.ShellFunctionsFile).Run()
|
||||
}
|
||||
|
@ -4,15 +4,33 @@
|
||||
package oscommands
|
||||
|
||||
import (
|
||||
"os"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetPlatform() *Platform {
|
||||
shell := getUserShell()
|
||||
|
||||
prefixForShellFunctionsFile := ""
|
||||
if strings.HasSuffix(shell, "bash") {
|
||||
prefixForShellFunctionsFile = "shopt -s expand_aliases\n"
|
||||
}
|
||||
|
||||
return &Platform{
|
||||
OS: runtime.GOOS,
|
||||
Shell: "bash",
|
||||
ShellArg: "-c",
|
||||
OpenCommand: "open {{filename}}",
|
||||
OpenLinkCommand: "open {{link}}",
|
||||
OS: runtime.GOOS,
|
||||
Shell: shell,
|
||||
ShellArg: "-c",
|
||||
PrefixForShellFunctionsFile: prefixForShellFunctionsFile,
|
||||
OpenCommand: "open {{filename}}",
|
||||
OpenLinkCommand: "open {{link}}",
|
||||
}
|
||||
}
|
||||
|
||||
func getUserShell() string {
|
||||
if shell := os.Getenv("SHELL"); shell != "" {
|
||||
return shell
|
||||
}
|
||||
|
||||
return "bash"
|
||||
}
|
||||
|
Reference in New Issue
Block a user