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
Give Terminal its own profile back where none was named before
Țugui Dragoșdbe641ab76b6
diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalSettings.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalSettings.swiftindex b21cc497..9f9ab24e 100644--- a/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalSettings.swift+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalSettings.swift@@ -48,4 +48,17 @@ public struct TerminalSettings: TerminalSettingsStoring, Sendable {CFPreferencesSetAppValue(role.rawValue as CFString, name as CFString?, Self.identifier as CFString)return CFPreferencesAppSynchronize(Self.identifier as CFString)}++ public var hasSetItselfUp: Bool {+ CFPreferencesAppSynchronize(Self.identifier as CFString)+ return CFPreferencesCopyAppValue("HasMigratedDefaults" as CFString, Self.identifier as CFString) as? Bool == true+ }++ public var initialProfileName: String? {+ guard let terminal = NSWorkspace.shared.urlForApplication(withBundleIdentifier: Self.identifier) else {+ return nil+ }+ let settings = terminal.appending(path: "Contents/Resources/Initial Settings/settings.plist")+ return BoundedRead.propertyList(at: settings)?["Default Settings"] as? String+ }}diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalSettingsStoring.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalSettingsStoring.swiftindex b59e4d15..daf8e3d2 100644--- a/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalSettingsStoring.swift+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalSettingsStoring.swift@@ -13,4 +13,9 @@ public protocol TerminalSettingsStoring {func setProfileName(_ name: String?, for role: TerminalProfileRole) -> Bool/// The command Terminal opens new windows with, when Shells open with names one.func shell() -> String?+ /// Whether Terminal has set itself up, which it does once, the first time it opens: it then names its initial+ /// profile for new windows and at startup itself.+ var hasSetItselfUp: Bool { get }+ /// The profile Terminal's own initial settings name, which it gives new windows and startup as it sets itself up.+ var initialProfileName: String? { get }}diff --git a/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalThemeLedger.swift b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalThemeLedger.swiftindex b2e18286..eaee9af4 100644--- a/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalThemeLedger.swift+++ b/Packages/PeelCore/Sources/PeelCore/Terminal/TerminalThemeLedger.swift@@ -107,8 +107,11 @@ public struct TerminalThemeLedger {// A role on another profile was changed by the person and stays, unless the theme is gone: Terminal itself// moves a role off a profile deleted there, which is no choice of a profile.let isGone = chosen.map { profiles[$0] == nil } ?? false+ // Terminal names its own initial profile for a role that names none only once, as it first opens, so a role+ // that named nothing before gets that profile back once Terminal has set itself up.+ let initial = terminal.initialProfileName.flatMap { terminal.hasSetItselfUp && profiles[$0] != nil ? $0 : nil }for role in TerminalProfileRole.allCases where isGone || terminal.profileName(for: role) == chosen {- guard terminal.setProfileName(before[role], for: role) else { return .refused }+ guard terminal.setProfileName(before[role] ?? initial, for: role) else { return .refused }}let inUse = Set(TerminalProfileRole.allCases.compactMap { terminal.profileName(for: $0) })let untouched = written.filter { name, fingerprint indiff --git a/Packages/PeelCore/Tests/PeelCoreTests/TerminalThemeLedgerTests.swift b/Packages/PeelCore/Tests/PeelCoreTests/TerminalThemeLedgerTests.swiftindex f8fa9b1d..8783e6c1 100644--- a/Packages/PeelCore/Tests/PeelCoreTests/TerminalThemeLedgerTests.swift+++ b/Packages/PeelCore/Tests/PeelCoreTests/TerminalThemeLedgerTests.swift@@ -10,6 +10,8 @@ private final class InMemoryTerminalSettings: TerminalSettingsStoring {var profilesAreUnreadable = falsevar storedProfiles: [String: [String: Any]] = ["Basic": ["name": "Basic"], "Pro": ["name": "Pro"]]var names: [TerminalProfileRole: String] = [.newWindows: "Basic", .startup: "Basic"]+ var hasSetItselfUp = true+ var initialProfileName: String? = "Clear Dark"func profiles() -> [String: [String: Any]]? {profilesAreUnreadable ? nil : storedProfiles@@ -38,6 +40,40 @@ struct TerminalThemeLedgerTests {private let theme = TerminalThemeCatalog.all[0]private let otherTheme = TerminalThemeCatalog.all[1]+ /// Terminal names its initial profile for both roles itself the first time it opens, and never again, so a role+ /// that named nothing before Peel's theme gets that profile back once Terminal has opened under the theme.+ @Test func givesARoleThatNamedNothingTerminalsOwnProfileOnceTerminalSetItselfUp() throws {+ let terminal = InMemoryTerminalSettings()+ terminal.names = [:]+ terminal.storedProfiles = [:]+ terminal.hasSetItselfUp = false+ var ledger = TerminalThemeLedger()+ #expect(try ledger.use(theme, in: terminal) == .changed)+ terminal.hasSetItselfUp = true+ terminal.storedProfiles["Clear Dark"] = ["name": "Clear Dark"]++ #expect(ledger.putBack(in: terminal) == .changed)++ #expect(terminal.names == [.newWindows: "Clear Dark", .startup: "Clear Dark"])+ }++ @Test func leavesARoleThatNamedNothingUnnamedWhileTerminalHasNotSetItselfUp() throws {+ let terminal = InMemoryTerminalSettings()+ terminal.names = [:]+ terminal.storedProfiles = [:]+ terminal.hasSetItselfUp = false+ var ledger = TerminalThemeLedger()+ #expect(try ledger.use(theme, in: terminal) == .changed)++ #expect(ledger.putBack(in: terminal) == .changed)++ #expect(terminal.names.isEmpty)+ }++ @Test func readsTheProfileTerminalsOwnSettingsStartWith() {+ #expect(TerminalSettings().initialProfileName == "Clear Dark")+ }+@Test func writesNothingOverProfilesItCannotRead() throws {let terminal = InMemoryTerminalSettings()var ledger = TerminalThemeLedger()diff --git a/PeelTests/TerminalLibrariesTests.swift b/PeelTests/TerminalLibrariesTests.swiftindex bd576b28..d9497180 100644--- a/PeelTests/TerminalLibrariesTests.swift+++ b/PeelTests/TerminalLibrariesTests.swift@@ -9,6 +9,8 @@ private final class FakeTerminalSettings: TerminalSettingsStoring {var storedProfiles: [String: [String: Any]]? = [:]var names: [TerminalProfileRole: String] = [:]var shellSetting: String?+ var hasSetItselfUp = true+ var initialProfileName: String? = "Clear Dark"func profiles() -> [String: [String: Any]]? { storedProfiles }