nwg-drawer
nwg-drawer copied to clipboard
Spaces in command are not being processed correctly.
Commands with spaces (in file names for example) won't run no matter what.
Suppose I want to run the following command
retroarch Chrono Trigger.smc
In which "Chrono Trigger.smc" is the name of the file. In standard bash we can express the middle space by two different ways (that I know of):
- Backslashs:
retroarch Chrono\ Trigger.smc
- Double Quotes:
retroarch "Chrono Trigger.smc"
Ideally they would both work in nwg-drawer
but it seems that none of these does.
I tried to go through the source code but couldn't find a clear path to fix it.
I wrote two functions to correct the behavior:
func parseString(s string) []string {
var r = regexp.MustCompile(`(".*"|(\\\s|[^\s])+)`)
var res = r.FindAllString(s, -1)
return res
}
func parseString2(command string) []string {
var elements []string
var confirmed []string
if !strings.Contains(command, "\"") {
elements = append(elements, strings.Split(command, " ")...)
} else {
for strings.Contains(command, "\"") {
quoteSplit := strings.SplitN(command, "\"", 2)
elements = append(elements, strings.Split(quoteSplit[0], " ")...)
quoteSplit = strings.SplitN(quoteSplit[1], "\"", 2)
elements = append(elements, quoteSplit[0])
command = quoteSplit[1]
}
}
for i := 0; i < len(elements); i++ {
if elements[i] != "" { // Ignore empty arguments
element := elements[i]
// If the argument end with "\ " then merge with next argument
for element[len(element)-1] == '\\' {
element = strings.Replace(element, "\\", "", 1)
element = element + " " + elements[i+1]
i++
}
confirmed = append(confirmed, element)
}
}
return confirmed
}
Both seem to work fine in my testing but checking edge cases may be necessary, I don't like the second one as it is a little convoluted but running benchmarks proved that it is much faster than the regex option.
I also encountered this issue in nwg-menu. This seems important!
I'll get back to it soon.