Uninstall Mac apps with every file they leave behind, and see why each belongs. Free up space, fine-tune macOS, and set up Terminal.
Swift cache-cleanercleanerclideveloper-toolsdisk-cleanupdisk-spaceduplicate-finderhomebrewliquid-glassmac-cleanermacosmacos-appmacos-golden-gatemacos-tahoemacos-tweaksswiftswiftuiterminal-themesuninstalluninstaller
Offer a shell setting or prompt only when this Mac's zsh knows it
Țugui Dragoș7d6b4c356cca
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/ZshRequirements.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/ZshRequirements.swiftnew file mode 100644index 00000000..08d04d0c--- /dev/null+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/ZshRequirements.swift@@ -0,0 +1,59 @@+import PeelPrivileged++/// What lines of zsh need from the zsh on this Mac: the options they set and the functions they load, read from the+/// lines themselves, so a setting is never offered to a zsh that would not take it.+enum ZshRequirements {+ static func of(_ lines: [String]) -> Set<String> {+ var needs: Set<String> = []+ for line in lines {+ let words = line.split(separator: " ").map(String.init)+ if words.first == "setopt" {+ needs.formUnion(words.dropFirst().map { "option:" + $0.lowercased().replacing("_", with: "") })+ }+ if let autoload = words.firstIndex(of: "autoload") {+ for word in words[(autoload + 1)...] where !word.hasPrefix("-") {+ guard word.allSatisfy({ $0.isLetter || $0.isNumber || $0 == "-" || $0 == "_" }) else { break }+ needs.insert("function:" + word)+ }+ }+ }+ return needs+ }++ static var catalog: Set<String> {+ ShellSetting.allCases.reduce(into: Set<String>()) { $0.formUnion($1.requirements) }+ .union(PromptStyle.allCases.reduce(into: Set<String>()) { $0.formUnion(of($1.lines)) })+ }++ /// The options and functions of the catalog that the zsh on this Mac knows, or nil when it did not answer.+ static func known() async -> Set<String>? {+ let functions = catalog.filter { $0.hasPrefix("function:") }.map { String($0.dropFirst("function:".count)) }.sorted()+ let script = "print -rl -- ${(k)options}; for name in \(functions.joined(separator: " ")); do "+ + "autoload +X -Uz $name 2>/dev/null && print -r -- \"function:$name\"; done; true"+ guard case .success(let output) = await Subprocess.run("/bin/zsh", ["-f", "-c", script], environment: [:], timeout: 10),+ output.status == 0 else { return nil }+ return Set(output.text.split(separator: "\n").map { $0.hasPrefix("function:") ? String($0) : "option:" + $0 })+ }+}++extension ShellSetting {+ var requirements: Set<String> {+ ZshRequirements.of(lines + (group == .completion ? [ShellFile.completionSystem] : []))+ }++ public func isKnown(by known: Set<String>) -> Bool {+ requirements.isSubset(of: known)+ }+}++extension PromptStyle {+ public func isKnown(by known: Set<String>) -> Bool {+ ZshRequirements.of(lines).isSubset(of: known)+ }+}++extension ShellFile {+ public static func knownToZsh() async -> Set<String>? {+ await ZshRequirements.known()+ }+}diff --git a/Packages/PeelCore/Tests/PeelCoreTests/ShellFileTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/ShellFileTests.swiftindex dc1006eb..76708108 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/ShellFileTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/ShellFileTests.swift@@ -126,3 +126,31 @@ struct ShellFileTests {#expect(!ShellFile.isSourced(shell.peelFile, from: startup, home: shell.home))}}++struct ZshRequirementsTests {+ @Test func theZshOnThisMacKnowsEverySettingAndEveryPrompt() async throws {+ let known = try #require(await ShellFile.knownToZsh())+ for setting in ShellSetting.allCases {+ #expect(setting.isKnown(by: known), "\(setting)")+ }+ for prompt in PromptStyle.allCases {+ #expect(prompt.isKnown(by: known), "\(prompt)")+ }+ }++ @Test func whatALineNeedsIsReadFromIt() {+ #expect(ZshRequirements.of(["setopt AUTO_PUSHD PUSHD_IGNORE_DUPS"]) == ["option:autopushd", "option:pushdignoredups"])+ #expect(ZshRequirements.of(["autoload -Uz vcs_info add-zsh-hook"]) == ["function:vcs_info", "function:add-zsh-hook"])+ #expect(ZshRequirements.of([ShellFile.completionSystem]) == ["function:compinit"])+ #expect(ZshRequirements.of(["HISTSIZE=50000", "zstyle ':completion:*' menu select"]).isEmpty)+ #expect(ShellSetting.completionMenu.requirements == ["function:compinit"])+ }++ @Test func aSettingOrPromptNeedingWhatZshDoesNotKnowIsNotOffered() async throws {+ let known = try #require(await ShellFile.knownToZsh())+ #expect(!ShellSetting.sharedHistory.isKnown(by: known.subtracting(["option:sharehistory"])))+ #expect(!ShellSetting.prefixSearch.isKnown(by: known.subtracting(["function:up-line-or-beginning-search"])))+ #expect(!PromptStyle.arrowAndBranch.isKnown(by: known.subtracting(["function:vcs_info"])))+ #expect(PromptStyle.macOS.isKnown(by: []))+ }+}