mirror of
https://github.com/swiftbar/SwiftBar.git
synced 2024-10-26 16:43:04 +03:00
Smarter shell quoting #366
Signed-off-by: Alex Mazanov <alexandr.mazanov@gmail.com>
This commit is contained in:
parent
10e6e8eed0
commit
18b1400c65
@ -149,7 +149,7 @@ struct MenuLineParameters: Codable {
|
||||
guard let param = params[key] else { continue }
|
||||
out.append(param.escaped())
|
||||
}
|
||||
return out.map { "'\($0)'" }
|
||||
return out.map { $0.quoteIfNeeded() }
|
||||
}
|
||||
|
||||
var terminal: Bool {
|
||||
|
@ -31,3 +31,20 @@ extension String {
|
||||
return encodedString
|
||||
}
|
||||
}
|
||||
|
||||
extension String {
|
||||
var isEnclosedInQuotes: Bool {
|
||||
let regex = #"^(?:'.*'|".*")$"#
|
||||
return range(of: regex, options: .regularExpression) != nil
|
||||
}
|
||||
|
||||
var needsShellQuoting: Bool {
|
||||
let specialCharacters = " \t\n\"'`$\\|&;()<>[]*?{}!^~#%"
|
||||
return rangeOfCharacter(from: CharacterSet(charactersIn: specialCharacters)) != nil
|
||||
}
|
||||
|
||||
func quoteIfNeeded() -> String {
|
||||
guard needsShellQuoting else { return self }
|
||||
return isEnclosedInQuotes ? self : "\'\(self)\'"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user