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.

Show each Terminal tab only once its settings are read

Țugui Dragoșb39a145d0979
diff --git a/Peel/Model/GitLibrary.swift b/Peel/Model/GitLibrary.swift
index fc7bde52..ed296b0e 100644
--- a/Peel/Model/GitLibrary.swift
+++ b/Peel/Model/GitLibrary.swift
@@ -4,14 +4,18 @@ import PeelCore
@Observable
final class GitLibrary {
+ struct State: Equatable {
+ var isInstalled: Bool
+ var settings: [String: String?]?
+ var known: Set<String>?
+ var signingKey: String?
+ }
+
private static let ledgerKey = "terminalGit"
private var ledger = GitLedger(stored: UserDefaults.standard.dictionary(forKey: ledgerKey) ?? [:])
- private(set) var git: GitConfig?
- private(set) var hasLooked = false
- private(set) var settings: [String: String?]?
- private(set) var known: Set<String>?
- private(set) var signingKey: String?
+ private var git: GitConfig?
+ private(set) var state: State?
private(set) var isWorking = false
private(set) var wasRefused = false
@@ -20,24 +24,28 @@ final class GitLibrary {
}
func refresh() async {
- if git == nil {
- git = await GitConfig.find()
- }
- hasLooked = true
- settings = await git?.settings()
+ let git = if let git { git } else { await GitConfig.find() }
+ let settings = await git?.settings()
+ var known = state?.known
if known == nil {
known = await git?.knownKeys()
}
- signingKey = GitSetting.signingKey(in: .homeDirectory)
+ self.git = git
+ state = State(
+ isInstalled: git != nil,
+ settings: settings,
+ known: known,
+ signingKey: GitSetting.signingKey(in: .homeDirectory)
+ )
}
func isOn(_ setting: GitSetting) -> Bool {
- settings.map(setting.isOn(in:)) ?? false
+ state?.settings.map(setting.isOn(in:)) ?? false
}
func isOffered(_ setting: GitSetting) -> Bool {
- guard settings != nil, known.map(setting.isKnown(by:)) == true else { return false }
- return setting != .signCommits || signingKey != nil || isOn(setting)
+ guard let state, state.settings != nil, state.known.map(setting.isKnown(by:)) == true else { return false }
+ return setting != .signCommits || state.signingKey != nil || isOn(setting)
}
func set(_ setting: GitSetting, to isOn: Bool) async {
@@ -45,7 +53,7 @@ final class GitLibrary {
isWorking = true
var ledger = ledger
let outcome = isOn
- ? await ledger.turnOn(setting, signingKey: signingKey, in: git)
+ ? await ledger.turnOn(setting, signingKey: state?.signingKey, in: git)
: await ledger.turnOff(setting, in: git)
finish(ledger, refused: outcome == .refused)
await refresh()
diff --git a/Peel/Model/SSHLibrary.swift b/Peel/Model/SSHLibrary.swift
index 5d7a8e57..26e54adb 100644
--- a/Peel/Model/SSHLibrary.swift
+++ b/Peel/Model/SSHLibrary.swift
@@ -4,11 +4,15 @@ import PeelCore
@Observable
final class SSHLibrary {
+ struct State: Equatable {
+ var settings: Set<SSHSetting>?
+ var isIncluded: Bool
+ var accepted: Set<SSHSetting>
+ }
+
let file = SSHFile.url(in: PeelFolder.url)
let config = URL.homeDirectory.appending(path: ".ssh/config", directoryHint: .notDirectory)
- private(set) var settings: Set<SSHSetting>?
- private(set) var isIncluded = false
- private(set) var accepted: Set<SSHSetting>?
+ private(set) var state: State?
private(set) var wasRefused = false
var lines: [String] {
@@ -16,23 +20,25 @@ final class SSHLibrary {
}
var hasSomethingOn: Bool {
- settings.map { !$0.isEmpty } ?? false
+ state?.settings.map { !$0.isEmpty } ?? false
}
func refresh() async {
- settings = SSHFile.read(file)
- isIncluded = SSHFile.isIncluded(file, from: config, home: .homeDirectory)
- if accepted == nil {
- accepted = await SSHFile.accepted()
- }
+ let accepted = if let state, !state.accepted.isEmpty { state.accepted } else { await SSHFile.accepted() }
+ state = State(
+ settings: SSHFile.read(file),
+ isIncluded: SSHFile.isIncluded(file, from: config, home: .homeDirectory),
+ accepted: accepted
+ )
}
func isOffered(_ setting: SSHSetting) -> Bool {
- settings != nil && accepted?.contains(setting) == true
+ guard let state, state.settings != nil else { return false }
+ return state.accepted.contains(setting)
}
func set(_ setting: SSHSetting, to isOn: Bool) {
- guard var settings else { return }
+ guard var settings = state?.settings else { return }
if isOn {
settings.insert(setting)
} else {
@@ -47,6 +53,6 @@ final class SSHLibrary {
private func write(_ settings: Set<SSHSetting>) {
wasRefused = !SSHFile.write(settings, to: file)
- self.settings = SSHFile.read(file)
+ state?.settings = SSHFile.read(file)
}
}
diff --git a/Peel/Model/ShellLibrary.swift b/Peel/Model/ShellLibrary.swift
index 78194202..44525bd6 100644
--- a/Peel/Model/ShellLibrary.swift
+++ b/Peel/Model/ShellLibrary.swift
@@ -4,44 +4,55 @@ import PeelCore
@Observable
final class ShellLibrary {
- let file = ShellFile.url(in: PeelFolder.url)
- private(set) var shell: ShellSessions?
- private(set) var choices: ShellFile.Choices?
- private(set) var isSourced = false
- private(set) var known: Set<String>?
- private(set) var wasRefused = false
+ struct State: Equatable {
+ var shell: ShellSessions?
+ var choices: ShellFile.Choices?
+ var isSourced: Bool
+ var known: Set<String>?
- var startupFile: URL? {
- shell?.zshrc
+ var startupFile: URL? {
+ shell?.zshrc
+ }
}
+ let file = ShellFile.url(in: PeelFolder.url)
+ private(set) var state: State?
+ private(set) var wasRefused = false
+
var line: String {
ShellFile.line(sourcing: file, home: .homeDirectory)
}
var hasSomethingOn: Bool {
- choices.map { !$0.settings.isEmpty || $0.prompt != .macOS } ?? false
+ state?.choices.map { !$0.settings.isEmpty || $0.prompt != .macOS } ?? false
}
func refresh() async {
- shell = ShellSessions.inTerminal(TerminalSettings())
- choices = ShellFile.read(file)
- isSourced = startupFile.map { ShellFile.isSourced(file, from: $0, home: .homeDirectory) } ?? false
+ var known = state?.known
if known == nil {
known = await ShellFile.knownToZsh()
}
+ let shell = ShellSessions.inTerminal(TerminalSettings())
+ state = State(
+ shell: shell,
+ choices: ShellFile.read(file),
+ isSourced: shell?.zshrc.map { ShellFile.isSourced(file, from: $0, home: .homeDirectory) } ?? false,
+ known: known
+ )
}
func isOffered(_ setting: ShellSetting) -> Bool {
- startupFile != nil && choices != nil && known.map(setting.isKnown(by:)) == true
+ guard let state, state.startupFile != nil, state.choices != nil else { return false }
+ return state.known.map(setting.isKnown(by:)) == true
}
func isOffered(_ prompt: PromptStyle) -> Bool {
- startupFile != nil && choices != nil && known.map(prompt.isKnown(by:)) == true
+ guard let state, state.startupFile != nil, state.choices != nil else { return false }
+ return state.known.map(prompt.isKnown(by:)) == true
}
func set(_ setting: ShellSetting, to isOn: Bool) {
- guard var choices else { return }
+ guard var choices = state?.choices else { return }
if isOn {
choices.settings.insert(setting)
} else {
@@ -51,7 +62,7 @@ final class ShellLibrary {
}
func choose(_ prompt: PromptStyle) {
- guard var choices else { return }
+ guard var choices = state?.choices else { return }
choices.prompt = prompt
write(choices)
}
@@ -62,6 +73,6 @@ final class ShellLibrary {
private func write(_ choices: ShellFile.Choices) {
wasRefused = !ShellFile.write(choices, to: file)
- self.choices = ShellFile.read(file)
+ state?.choices = ShellFile.read(file)
}
}
diff --git a/Peel/Model/TerminalToolLibrary.swift b/Peel/Model/TerminalToolLibrary.swift
index a0dba64f..84a4d26d 100644
--- a/Peel/Model/TerminalToolLibrary.swift
+++ b/Peel/Model/TerminalToolLibrary.swift
@@ -4,26 +4,45 @@ import PeelCore
@Observable
final class TerminalToolLibrary {
- private(set) var prefix: URL?
- private(set) var hasLooked = false
- private(set) var installed: Set<TerminalTool> = []
- private(set) var known: Set<String>?
- private(set) var packages: [HomebrewPackage] = []
+ struct State: Equatable {
+ var prefix: URL?
+ var installed: Set<TerminalTool>
+ }
+
+ struct Catalog {
+ let known: Set<String>
+ let packages: [HomebrewPackage]
+ }
+
+ private(set) var state: State?
+ private(set) var catalog: Catalog?
+ private var askingHomebrew: Task<Void, Never>?
func refresh() async {
- prefix = Homebrew.executableURL?.deletingLastPathComponent().deletingLastPathComponent()
- hasLooked = true
- installed = prefix.map { prefix in Set(TerminalTool.allCases.filter { $0.isInstalled(prefix: prefix) }) } ?? []
- guard prefix != nil, known == nil, await Homebrew.hasLocalDefinitions() else { return }
- let names = await Homebrew.formulaNames()
- guard !names.isEmpty else { return }
- packages = await Homebrew.formulae(
- named: Set(TerminalTool.allCases.flatMap(\.formulae)).filter(names.contains).sorted()
- )
- known = names
+ let prefix = Homebrew.executableURL?.deletingLastPathComponent().deletingLastPathComponent()
+ let installed = prefix.map { prefix in TerminalTool.allCases.filter { $0.isInstalled(prefix: prefix) } } ?? []
+ state = State(prefix: prefix, installed: Set(installed))
+ guard prefix != nil, catalog == nil else { return }
+ if askingHomebrew == nil {
+ askingHomebrew = Task {
+ catalog = await Self.askHomebrew()
+ askingHomebrew = nil
+ }
+ }
+ await askingHomebrew?.value
}
func availability(of tool: TerminalTool) -> TerminalTool.Availability {
- known.map { tool.availability(known: $0, packages: packages) } ?? .available
+ catalog.map { tool.availability(known: $0.known, packages: $0.packages) } ?? .available
+ }
+
+ private static func askHomebrew() async -> Catalog? {
+ guard await Homebrew.hasLocalDefinitions() else { return nil }
+ let names = await Homebrew.formulaNames()
+ guard !names.isEmpty else { return nil }
+ let packages = await Homebrew.formulae(
+ named: Set(TerminalTool.allCases.flatMap(\.formulae)).filter(names.contains).sorted()
+ )
+ return Catalog(known: names, packages: packages)
}
}
diff --git a/Peel/PeelApp.swift b/Peel/PeelApp.swift
index a1920dd3..02b9cb60 100644
--- a/Peel/PeelApp.swift
+++ b/Peel/PeelApp.swift
@@ -39,6 +39,10 @@ struct PeelApp: App {
@State private var installers: InstallerLibrary
@State private var tweaks = TweakLibrary()
@State private var terminal = TerminalLibrary()
+ @State private var shell = ShellLibrary()
+ @State private var git = GitLibrary()
+ @State private var ssh = SSHLibrary()
+ @State private var terminalTools = TerminalToolLibrary()
@State private var extensions = ExtensionLibrary()
@State private var cloud = CloudLibrary()
@State private var backgroundItems = BackgroundItemLibrary()
@@ -329,6 +333,10 @@ struct PeelApp: App {
.environment(installers)
.environment(tweaks)
.environment(terminal)
+ .environment(shell)
+ .environment(git)
+ .environment(ssh)
+ .environment(terminalTools)
.environment(extensions)
.environment(cloud)
.environment(backgroundItems)
diff --git a/Peel/Views/Terminal/GitTab.swift b/Peel/Views/Terminal/GitTab.swift
index 3ea46642..5d9b730b 100644
--- a/Peel/Views/Terminal/GitTab.swift
+++ b/Peel/Views/Terminal/GitTab.swift
@@ -1,4 +1,3 @@
-import AppKit
import PeelCore
import SwiftUI
@@ -8,36 +7,38 @@ struct GitTab: View {
var body: some View {
Form {
- if git.hasLooked, git.git == nil {
- Section {
- VStack(alignment: .leading, spacing: 4) {
- Text("Git isn’t installed")
- .font(.body.weight(.semibold))
- CopyableLines(
- caption: Text("Run this in Terminal to install Apple’s command line developer tools, which include Git."),
- lines: ["xcode-select --install"]
- )
+ if let state = git.state {
+ if !state.isInstalled {
+ Section {
+ VStack(alignment: .leading, spacing: 4) {
+ Text("Git isn’t installed")
+ .font(.body.weight(.semibold))
+ CopyableLines(
+ caption: Text("Run this in Terminal to install Apple’s command line developer tools, which include Git."),
+ lines: ["xcode-select --install"]
+ )
+ }
+ .padding(.vertical, 4)
+ }
+ } else if state.settings == nil {
+ Section {
+ Label("Git couldn’t read its settings", systemImage: "exclamationmark.triangle")
+ .foregroundStyle(Color.accentColor)
+ }
+ } else if git.wasRefused {
+ Section {
+ Label("Git refused it", systemImage: "exclamationmark.triangle")
+ .foregroundStyle(Color.accentColor)
}
- .padding(.vertical, 4)
- }
- } else if git.hasLooked, git.settings == nil {
- Section {
- Label("Git couldn’t read its settings", systemImage: "exclamationmark.triangle")
- .foregroundStyle(Color.accentColor)
- }
- } else if git.wasRefused {
- Section {
- Label("Git refused it", systemImage: "exclamationmark.triangle")
- .foregroundStyle(Color.accentColor)
}
- }
- ForEach(GitSetting.Group.allCases, id: \.self) { group in
- Section {
- ForEach(GitSetting.allCases.filter { $0.group == group }, id: \.self) { setting in
- GitSettingRow(setting: setting)
+ ForEach(GitSetting.Group.allCases, id: \.self) { group in
+ Section {
+ ForEach(GitSetting.allCases.filter { $0.group == group }, id: \.self) { setting in
+ GitSettingRow(setting: setting)
+ }
+ } header: {
+ Text(group.title)
}
- } header: {
- Text(group.title)
}
}
}
@@ -56,12 +57,6 @@ struct GitTab: View {
} message: {
Text("Settings Peel changed go back to what they were, and the rest go back to Git’s defaults.")
}
- .task {
- await git.refresh()
- }
- .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
- Task { await git.refresh() }
- }
}
}
@@ -73,7 +68,7 @@ private struct GitSettingRow: View {
TerminalSwitchRow(
title: setting.title,
detail: setting.detail,
- footnote: setting.values(signingKey: git.signingKey ?? "~/.ssh/id_ed25519.pub")
+ footnote: setting.values(signingKey: git.state?.signingKey ?? "~/.ssh/id_ed25519.pub")
.map { "git config --global \($0.key) \($0.value)" }
.joined(separator: "\n"),
caption: caption,
@@ -87,7 +82,7 @@ private struct GitSettingRow: View {
private var caption: LocalizedStringResource? {
guard setting == .signCommits else { return nil }
- guard let key = git.signingKey else { return "Needs an SSH key in ~/.ssh" }
+ guard let key = git.state?.signingKey else { return "Needs an SSH key in ~/.ssh" }
return "With \(URL(filePath: key).abbreviatedPath)"
}
}
diff --git a/Peel/Views/Terminal/SSHTab.swift b/Peel/Views/Terminal/SSHTab.swift
index 6901a3d9..72b078e2 100644
--- a/Peel/Views/Terminal/SSHTab.swift
+++ b/Peel/Views/Terminal/SSHTab.swift
@@ -1,4 +1,3 @@
-import AppKit
import PeelCore
import SwiftUI
@@ -8,34 +7,36 @@ struct SSHTab: View {
var body: some View {
Form {
- Section {
- SSHLinesRow()
- } footer: {
- if ssh.wasRefused {
- Label("macOS refused it", systemImage: "exclamationmark.triangle")
- .font(.caption)
- .foregroundStyle(Color.accentColor)
- } else {
- Text("What ~/.ssh/config sets earlier for a server stays in force, since ssh uses the first value it finds.")
- .font(.caption)
- .foregroundStyle(.secondary)
+ if let state = ssh.state {
+ Section {
+ SSHLinesRow(isIncluded: state.isIncluded)
+ } footer: {
+ if ssh.wasRefused {
+ Label("macOS refused it", systemImage: "exclamationmark.triangle")
+ .font(.caption)
+ .foregroundStyle(Color.accentColor)
+ } else {
+ Text("What ~/.ssh/config sets earlier for a server stays in force, since ssh uses the first value it finds.")
+ .font(.caption)
+ .foregroundStyle(.secondary)
+ }
}
- }
- Section {
- ForEach(SSHSetting.allCases, id: \.self) { setting in
- TerminalSwitchRow(
- title: setting.title,
- detail: setting.detail,
- footnote: setting.lines.joined(separator: "\n"),
- isOn: Binding(
- get: { ssh.settings?.contains(setting) == true },
- set: { ssh.set(setting, to: $0) }
- ),
- isDisabled: !ssh.isOffered(setting)
- )
+ Section {
+ ForEach(SSHSetting.allCases, id: \.self) { setting in
+ TerminalSwitchRow(
+ title: setting.title,
+ detail: setting.detail,
+ footnote: setting.lines.joined(separator: "\n"),
+ isOn: Binding(
+ get: { ssh.state?.settings?.contains(setting) == true },
+ set: { ssh.set(setting, to: $0) }
+ ),
+ isDisabled: !ssh.isOffered(setting)
+ )
+ }
+ } header: {
+ Text("Connections")
}
- } header: {
- Text("Connections")
}
}
.formStyle(.grouped)
@@ -53,22 +54,17 @@ struct SSHTab: View {
} message: {
Text("New connections use only what ~/.ssh/config sets.")
}
- .task {
- await ssh.refresh()
- }
- .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
- Task { await ssh.refresh() }
- }
}
}
private struct SSHLinesRow: View {
@Environment(SSHLibrary.self) private var ssh
+ let isIncluded: Bool
var body: some View {
VStack(alignment: .leading, spacing: 4) {
HStack(spacing: 5) {
- if ssh.isIncluded {
+ if isIncluded {
Label {
Text("ssh reads Peel’s settings")
} icon: {
@@ -85,7 +81,7 @@ private struct SSHLinesRow: View {
)
}
.font(.body.weight(.semibold))
- if ssh.isIncluded {
+ if isIncluded {
Text("From ~/.ssh/config")
.font(.caption)
.foregroundStyle(.secondary)
diff --git a/Peel/Views/Terminal/ShellTab.swift b/Peel/Views/Terminal/ShellTab.swift
index 66b5f5a4..5d99e9c7 100644
--- a/Peel/Views/Terminal/ShellTab.swift
+++ b/Peel/Views/Terminal/ShellTab.swift
@@ -1,4 +1,3 @@
-import AppKit
import PeelCore
import SwiftUI
@@ -8,47 +7,49 @@ struct ShellTab: View {
var body: some View {
Form {
- Section {
- ShellLineRow()
- } footer: {
- if shell.wasRefused {
- Label("macOS refused it", systemImage: "exclamationmark.triangle")
- .font(.caption)
- .foregroundStyle(Color.accentColor)
- } else if shell.startupFile != nil {
- Text("Takes effect in new windows")
- .font(.caption)
- .foregroundStyle(.secondary)
- }
- }
- Section {
- let width = PromptSample.width(of: PromptStyle.allCases)
- ForEach(PromptStyle.allCases, id: \.self) { style in
- PromptRow(style: style, sampleWidth: width)
+ if let state = shell.state {
+ Section {
+ ShellLineRow(state: state)
+ } footer: {
+ if shell.wasRefused {
+ Label("macOS refused it", systemImage: "exclamationmark.triangle")
+ .font(.caption)
+ .foregroundStyle(Color.accentColor)
+ } else if state.startupFile != nil {
+ Text("Takes effect in new windows")
+ .font(.caption)
+ .foregroundStyle(.secondary)
+ }
}
- } header: {
- Text("Prompt")
- } footer: {
- Text("The colors are the Terminal theme’s, and the arrow turns red after a command fails.")
- .font(.caption)
- .foregroundStyle(.secondary)
- }
- ForEach(ShellSetting.Group.allCases, id: \.self) { group in
Section {
- ForEach(ShellSetting.allCases.filter { $0.group == group }, id: \.self) { setting in
- TerminalSwitchRow(
- title: setting.title,
- detail: setting.detail,
- footnote: setting.lines.joined(separator: "\n"),
- isOn: Binding(
- get: { shell.choices?.settings.contains(setting) == true },
- set: { shell.set(setting, to: $0) }
- ),
- isDisabled: !shell.isOffered(setting)
- )
+ let width = PromptSample.width(of: PromptStyle.allCases)
+ ForEach(PromptStyle.allCases, id: \.self) { style in
+ PromptRow(style: style, sampleWidth: width)
}
} header: {
- Text(group.title)
+ Text("Prompt")
+ } footer: {
+ Text("The colors are the Terminal theme’s, and the arrow turns red after a command fails.")
+ .font(.caption)
+ .foregroundStyle(.secondary)
+ }
+ ForEach(ShellSetting.Group.allCases, id: \.self) { group in
+ Section {
+ ForEach(ShellSetting.allCases.filter { $0.group == group }, id: \.self) { setting in
+ TerminalSwitchRow(
+ title: setting.title,
+ detail: setting.detail,
+ footnote: setting.lines.joined(separator: "\n"),
+ isOn: Binding(
+ get: { shell.state?.choices?.settings.contains(setting) == true },
+ set: { shell.set(setting, to: $0) }
+ ),
+ isDisabled: !shell.isOffered(setting)
+ )
+ }
+ } header: {
+ Text(group.title)
+ }
}
}
}
@@ -67,23 +68,18 @@ struct ShellTab: View {
} message: {
Text("New windows start with the prompt and settings macOS sets.")
}
- .task {
- await shell.refresh()
- }
- .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
- Task { await shell.refresh() }
- }
}
}
private struct ShellLineRow: View {
@Environment(ShellLibrary.self) private var shell
+ let state: ShellLibrary.State
var body: some View {
VStack(alignment: .leading, spacing: 4) {
- if let startupFile = shell.startupFile {
+ if let startupFile = state.startupFile {
HStack(spacing: 5) {
- if shell.isSourced {
+ if state.isSourced {
Label {
Text("zsh reads Peel’s settings")
} icon: {
@@ -100,7 +96,7 @@ private struct ShellLineRow: View {
)
}
.font(.body.weight(.semibold))
- if shell.isSourced {
+ if state.isSourced {
Text("From \(startupFile.abbreviatedPath)")
.font(.caption)
.foregroundStyle(.secondary)
@@ -110,7 +106,7 @@ private struct ShellLineRow: View {
lines: [ShellFile.command(adding: shell.line, to: startupFile, home: .homeDirectory)]
)
}
- } else if shell.shell == .bash {
+ } else if state.shell == .bash {
Text("Terminal opens bash, so these zsh settings don’t apply.")
.foregroundStyle(.secondary)
} else {
@@ -129,7 +125,7 @@ private struct PromptRow: View {
let sampleWidth: CGFloat
var body: some View {
- let isChosen = shell.choices?.prompt == style
+ let isChosen = shell.state?.choices?.prompt == style
Button {
shell.choose(style)
} label: {
diff --git a/Peel/Views/Terminal/TerminalPage.swift b/Peel/Views/Terminal/TerminalPage.swift
index 97ba7534..344e9c35 100644
--- a/Peel/Views/Terminal/TerminalPage.swift
+++ b/Peel/Views/Terminal/TerminalPage.swift
@@ -6,11 +6,11 @@ struct TerminalPage: View {
@Environment(TerminalLibrary.self) private var terminal
@Environment(TweakLibrary.self) private var tweaks
@Environment(RemovalHistoryStore.self) private var history
+ @Environment(ShellLibrary.self) private var shell
+ @Environment(GitLibrary.self) private var git
+ @Environment(SSHLibrary.self) private var ssh
+ @Environment(TerminalToolLibrary.self) private var tools
@State private var showsWindowSwitch = false
- @State private var shell = ShellLibrary()
- @State private var git = GitLibrary()
- @State private var ssh = SSHLibrary()
- @State private var tools = TerminalToolLibrary()
static let tabTitles: [LocalizedStringResource] = ["Themes", "Terminal", "Shell", "Git", "SSH", "Tools"]
static let tabBarWidth = TabBar.width(of: tabTitles.map { String(localized: $0) })
@@ -44,23 +44,15 @@ struct TerminalPage: View {
ToolsTab()
}
}
- .environment(shell)
- .environment(git)
- .environment(ssh)
- .environment(tools)
.navigationTitle(Text(Tool.terminal.title))
.task {
- terminal.refresh()
- tweaks.refresh()
- showsWindowSwitch = TerminalWindows.switchMatters()
+ await refresh()
}
.onDisappear {
tweaks.forgetRefusals()
}
.onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
- terminal.refresh()
- tweaks.refresh()
- showsWindowSwitch = TerminalWindows.switchMatters()
+ Task { await refresh() }
}
.onChange(of: history.revision) {
terminal.refresh()
@@ -90,6 +82,17 @@ struct TerminalPage: View {
}
}
+ private func refresh() async {
+ terminal.refresh()
+ tweaks.refresh()
+ showsWindowSwitch = TerminalWindows.switchMatters()
+ async let shellRead: Void = shell.refresh()
+ async let gitRead: Void = git.refresh()
+ async let sshRead: Void = ssh.refresh()
+ async let toolsRead: Void = tools.refresh()
+ _ = await (shellRead, gitRead, sshRead, toolsRead)
+ }
+
private var isWaitingForTerminal: Binding<Bool> {
Binding(
get: { terminal.waitingForTerminal != nil },
diff --git a/Peel/Views/Terminal/ToolsTab.swift b/Peel/Views/Terminal/ToolsTab.swift
index 32a9a3f2..2acd01e9 100644
--- a/Peel/Views/Terminal/ToolsTab.swift
+++ b/Peel/Views/Terminal/ToolsTab.swift
@@ -1,4 +1,3 @@
-import AppKit
import PeelCore
import SwiftUI
@@ -7,55 +6,52 @@ struct ToolsTab: View {
var body: some View {
Form {
- if tools.hasLooked, tools.prefix == nil {
- Section {
- VStack(alignment: .leading, spacing: 4) {
- HStack(spacing: 5) {
- Text("Install Homebrew first")
- .font(.body.weight(.semibold))
- Link(destination: Homebrew.website) { Text(verbatim: "brew.sh") }
- .font(.caption)
+ if let state = tools.state {
+ if state.prefix == nil {
+ Section {
+ VStack(alignment: .leading, spacing: 4) {
+ HStack(spacing: 5) {
+ Text("Install Homebrew first")
+ .font(.body.weight(.semibold))
+ Link(destination: Homebrew.website) { Text(verbatim: "brew.sh") }
+ .font(.caption)
+ }
+ CopyableLines(
+ caption: Text("These tools install with Homebrew. Run this in Terminal: its script says what it will do and waits before doing it."),
+ lines: [Homebrew.installCommand]
+ )
}
- CopyableLines(
- caption: Text("These tools install with Homebrew. Run this in Terminal: its script says what it will do and waits before doing it."),
- lines: [Homebrew.installCommand]
- )
+ .padding(.vertical, 4)
}
- .padding(.vertical, 4)
}
- }
- ForEach(TerminalTool.Group.allCases, id: \.self) { group in
- Section {
- ForEach(TerminalTool.allCases.filter { $0.group == group }, id: \.self) { tool in
- TerminalToolRow(tool: tool)
- }
- } header: {
- Text(group.title)
- } footer: {
- if group == .shell {
- Text("Their lines go in ~/.zshrc in the order they are listed here.")
- .font(.caption)
- .foregroundStyle(.secondary)
+ ForEach(TerminalTool.Group.allCases, id: \.self) { group in
+ Section {
+ ForEach(TerminalTool.allCases.filter { $0.group == group }, id: \.self) { tool in
+ TerminalToolRow(tool: tool, state: state)
+ }
+ } header: {
+ Text(group.title)
+ } footer: {
+ if group == .shell {
+ Text("Their lines go in ~/.zshrc in the order they are listed here.")
+ .font(.caption)
+ .foregroundStyle(.secondary)
+ }
}
}
}
}
.formStyle(.grouped)
- .task {
- await tools.refresh()
- }
- .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
- Task { await tools.refresh() }
- }
}
}
private struct TerminalToolRow: View {
@Environment(TerminalToolLibrary.self) private var tools
let tool: TerminalTool
+ let state: TerminalToolLibrary.State
var body: some View {
- let isInstalled = tools.installed.contains(tool)
+ let isInstalled = state.installed.contains(tool)
VStack(alignment: .leading, spacing: 6) {
HStack(spacing: 5) {
Text(verbatim: tool.name)
@@ -97,7 +93,7 @@ private struct TerminalToolRow: View {
if !isInstalled {
CopyableLines(caption: Text("Install"), lines: [tool.installCommand])
}
- ForEach(tool.setup(prefix: tools.prefix ?? Self.defaultPrefix), id: \.lines) { setup in
+ ForEach(tool.setup(prefix: state.prefix ?? Self.defaultPrefix), id: \.lines) { setup in
CopyableLines(caption: Text(setup.place.caption), lines: setup.lines)
}
}