mirror of
https://github.com/jesseduffield/lazygit.git
synced 2025-07-28 16:02:01 +03:00
refactor: move fallback to platform struct
Co-authored-by: Jesse Duffield <jessedduffield@gmail.com>
This commit is contained in:
@ -17,11 +17,12 @@ import (
|
|||||||
|
|
||||||
// Platform stores the os state
|
// Platform stores the os state
|
||||||
type Platform struct {
|
type Platform struct {
|
||||||
os string
|
os string
|
||||||
shell string
|
shell string
|
||||||
shellArg string
|
shellArg string
|
||||||
escapedQuote string
|
escapedQuote string
|
||||||
openCommand string
|
openCommand string
|
||||||
|
fallbackEscapedQuote string
|
||||||
}
|
}
|
||||||
|
|
||||||
// OSCommand holds all the os commands
|
// OSCommand holds all the os commands
|
||||||
@ -140,14 +141,11 @@ func (c *OSCommand) PrepareSubProcess(cmdName string, commandArgs ...string) *ex
|
|||||||
// Quote wraps a message in platform-specific quotation marks
|
// Quote wraps a message in platform-specific quotation marks
|
||||||
func (c *OSCommand) Quote(message string) string {
|
func (c *OSCommand) Quote(message string) string {
|
||||||
message = strings.Replace(message, "`", "\\`", -1)
|
message = strings.Replace(message, "`", "\\`", -1)
|
||||||
if c.Platform.os == "linux" {
|
escapedQuote := c.Platform.escapedQuote
|
||||||
if strings.ContainsRune(message, '\'') {
|
if strings.Contains(message, c.Platform.escapedQuote) {
|
||||||
c.Platform.escapedQuote = `"`
|
escapedQuote = c.Platform.fallbackEscapedQuote
|
||||||
} else {
|
|
||||||
c.Platform.escapedQuote = `'`
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return c.Platform.escapedQuote + message + c.Platform.escapedQuote
|
return escapedQuote + message + escapedQuote
|
||||||
}
|
}
|
||||||
|
|
||||||
// Unquote removes wrapping quotations marks if they are present
|
// Unquote removes wrapping quotations marks if they are present
|
||||||
|
@ -8,10 +8,11 @@ import (
|
|||||||
|
|
||||||
func getPlatform() *Platform {
|
func getPlatform() *Platform {
|
||||||
return &Platform{
|
return &Platform{
|
||||||
os: runtime.GOOS,
|
os: runtime.GOOS,
|
||||||
shell: "bash",
|
shell: "bash",
|
||||||
shellArg: "-c",
|
shellArg: "-c",
|
||||||
escapedQuote: "\"",
|
escapedQuote: "'",
|
||||||
openCommand: "open {{filename}}",
|
openCommand: "open {{filename}}",
|
||||||
|
fallbackEscapedQuote: "\"",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -272,7 +272,7 @@ func TestOSCommandQuoteSingleQuote(t *testing.T) {
|
|||||||
|
|
||||||
actual := osCommand.Quote("hello 'test'")
|
actual := osCommand.Quote("hello 'test'")
|
||||||
|
|
||||||
expected := osCommand.Platform.escapedQuote + "hello 'test'" + osCommand.Platform.escapedQuote
|
expected := osCommand.Platform.fallbackEscapedQuote + "hello 'test'" + osCommand.Platform.fallbackEscapedQuote
|
||||||
|
|
||||||
assert.EqualValues(t, expected, actual)
|
assert.EqualValues(t, expected, actual)
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,10 @@ package commands
|
|||||||
|
|
||||||
func getPlatform() *Platform {
|
func getPlatform() *Platform {
|
||||||
return &Platform{
|
return &Platform{
|
||||||
os: "windows",
|
os: "windows",
|
||||||
shell: "cmd",
|
shell: "cmd",
|
||||||
shellArg: "/c",
|
shellArg: "/c",
|
||||||
escapedQuote: `\"`,
|
escapedQuote: `\"`,
|
||||||
|
fallbackEscapedQuote: "\\'",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user