mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-04-14 03:25:50 +00:00
We already had an established Ghostty.Shell namespace (previously a struct; now a more idiomatic enum), and locating these functions next to each other makes it clearer how they relate to one another.
30 lines
1.1 KiB
Swift
30 lines
1.1 KiB
Swift
extension Ghostty {
|
|
enum Shell {
|
|
// Characters to escape in the shell.
|
|
private static let escapeCharacters = "\\ ()[]{}<>\"'`!#$&;|*?\t"
|
|
|
|
/// Escape shell-sensitive characters in a string by prefixing each with a
|
|
/// backslash. Suitable for inserting paths/URLs into a live terminal buffer.
|
|
static func escape(_ str: String) -> String {
|
|
var result = str
|
|
for char in escapeCharacters {
|
|
result = result.replacingOccurrences(
|
|
of: String(char),
|
|
with: "\\\(char)"
|
|
)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
private static let quoteUnsafe = /[^\w@%+=:,.\/-]/
|
|
|
|
/// Returns a shell-quoted version of the string, like Python's shlex.quote.
|
|
/// Suitable for building shell command lines that will be executed.
|
|
static func quote(_ str: String) -> String {
|
|
guard str.isEmpty || str.contains(Self.quoteUnsafe) else { return str }
|
|
return "'" + str.replacingOccurrences(of: "'", with: #"'"'"'"#) + "'"
|
|
}
|
|
}
|
|
}
|