Skip to content

armgit / peel

Public Imported from GitHub
0

Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.

peel quotes every name a shell would read as more than itself

Țugui Dragoșae3242e816b2
diff --git a/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift b/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift
index 650e23a8..907c775e 100644
--- a/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift
+++ b/Packages/PeelCore/Sources/PeelCommandLine/Support/Output.swift
@@ -51,10 +51,12 @@ enum Output {
PlainText.of(text)
}
- /// Returns `name` so the reader can paste it into a command: in single quotes when it contains a space.
+ /// Returns `name` so the reader can paste it into a command: in single quotes unless every character is a
+ /// letter, a digit, or one of `._/+-@%:=,`, which no shell reads as anything but itself.
static func quoted(_ name: String) -> String {
let name = plain(name)
- return name.contains(" ") ? "'\(name.replacingOccurrences(of: "'", with: "'\\''"))'" : name
+ let isPlain = !name.isEmpty && name.allSatisfy { $0.isLetter || $0.isNumber || "._/+-@%:=,".contains($0) }
+ return isPlain ? name : "'\(name.replacingOccurrences(of: "'", with: "'\\''"))'"
}
/// Writes `text` to standard output unchanged, for output that already ends in a newline. When the write fails
diff --git a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift
index adcf628b..1603f253 100644
--- a/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift
+++ b/Packages/PeelCore/Tests/PeelCoreTests/CommandLineTests.swift
@@ -363,6 +363,11 @@ struct CommandLineTests {
#expect(Output.quoted("Notes Pro") == "'Notes Pro'")
#expect(Output.quoted("Editor") == "Editor")
#expect(Output.quoted("Sam's Apps") == "'Sam'\\''s Apps'")
+ let breaking = ["Q&A", "Tools;Kit", "A|B", "(Beta)", "Cost$", "Back`tick", "Star*", "What?", "Wow!", "No#1"]
+ for name in breaking + ["Sam's"] {
+ #expect(Output.quoted(name).hasPrefix("'"), "\(name) would break the command it is pasted into")
+ }
+ #expect(Output.quoted("Café-2.0_x86/64+@%:=,") == "Café-2.0_x86/64+@%:=,")
}
// MARK: peel uninstall