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.

13.9 KB
Raw Blame
import AppKit
import PeelCore
import SwiftUI

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

    static let tabTitles: [LocalizedStringResource] = ["Themes", "Terminal", "Shell", "Git", "SSH", "Tools"]
    static let tabBarWidth = TabBar.width(of: tabTitles.map { String(localized: $0) })

    var body: some View {
        TabView {
            Tab("Themes", systemImage: "paintpalette") {
                Form {
                    Section("In Use") {
                        TerminalThemeInUse()
                    }
                    Section {
                        TerminalThemeGrid()
                    }
                }
                .formStyle(.grouped)
            }
            Tab("Terminal", systemImage: "apple.terminal") {
                TerminalSettingsForm()
            }
            Tab("Shell", systemImage: "chevron.left.forwardslash.chevron.right") {
                ShellTab()
            }
            Tab("Git", systemImage: "arrow.triangle.branch") {
                GitTab()
            }
            Tab("SSH", systemImage: "key") {
                SSHTab()
            }
            Tab("Tools", systemImage: "shippingbox") {
                ToolsTab()
            }
        }
        .navigationTitle(Text(Tool.terminal.title))
        .task {
            await refresh()
        }
        .onDisappear {
            tweaks.forgetNotices()
            terminal.forgetRefusal()
            shell.forgetRefusal()
            ssh.forgetRefusal()
            git.forgetRefusal()
        }
        .onReceive(NotificationCenter.default.publisher(for: NSApplication.didBecomeActiveNotification)) { _ in
            Task { await refresh() }
        }
        .onChange(of: history.revision) {
            terminal.refresh()
        }
        .onReceive(
            NSWorkspace.shared.notificationCenter.publisher(for: NSWorkspace.didLaunchApplicationNotification)
        ) { notification in
            if Self.isTerminal(notification) { terminal.refresh() }
        }
        .onReceive(
            NSWorkspace.shared.notificationCenter.publisher(for: NSWorkspace.didTerminateApplicationNotification)
        ) { notification in
            if Self.isTerminal(notification) { terminal.refresh() }
        }
        .alert("Quit Terminal first?", isPresented: isWaitingForTerminal, presenting: terminal.waitingForTerminal) { action in
            Button("Quit Terminal") {
                Task { await terminal.quitTerminal(andThen: action) }
            }
            Button("Cancel", role: .cancel) {}
        } message: { _ in
            Text("While Terminal is open, it doesn’t see a change to its settings, and can undo it the next time it saves them.")
        }
        .alert("Terminal didn’t quit.", isPresented: Bindable(terminal).terminalDidNotQuit) {
            Button("OK", role: .cancel) {}
        } message: {
            Text("It may be asking whether to stop a command that is still running.")
        }
    }

    private func refresh() async {
        terminal.refresh()
        tweaks.refresh()
        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 static func isTerminal(_ notification: Notification) -> Bool {
        let app = notification.userInfo?[NSWorkspace.applicationUserInfoKey] as? NSRunningApplication
        return app?.bundleIdentifier == TerminalSettings.identifier
    }

    private var isWaitingForTerminal: Binding<Bool> {
        Binding(
            get: { terminal.waitingForTerminal != nil },
            set: { if !$0 { terminal.waitingForTerminal = nil } }
        )
    }
}

private struct TerminalThemeInUse: View {
    @Environment(TerminalLibrary.self) private var terminal

    var body: some View {
        ViewThatFits(in: .horizontal) {
            HStack(alignment: .center, spacing: 18) {
                preview
                    .frame(width: 300)
                words
            }
            VStack(alignment: .leading, spacing: 14) {
                preview
                words
            }
        }
        .padding(.vertical, 6)
    }

    @ViewBuilder
    private var preview: some View {
        if let theme = terminal.themeInUse {
            TerminalSession(theme: theme)
        } else {
            Image(systemName: "terminal")
                .font(.system(size: 40))
                .foregroundStyle(.secondary)
                .frame(maxWidth: .infinity, minHeight: 120)
                .background(.quaternary, in: .rect(cornerRadius: 10))
                .accessibilityHidden(true)
        }
    }

    private var words: some View {
        VStack(alignment: .leading, spacing: 6) {
            Text(verbatim: terminal.themeInUse?.name ?? terminal.profileInUse ?? String(localized: Tool.terminal.title))
                .font(.title3.weight(.semibold))
            Group {
                if let startup = terminal.opensWith {
                    Text("Every new window uses this theme, while Terminal opens with \(startup). Choose the theme again below to open with it too.")
                } else if terminal.themeInUse == nil {
                    Text("Choose a theme below, and Terminal opens with it.")
                } else {
                    Text("Terminal opens with this theme, and every new window uses it.")
                }
            }
            .foregroundStyle(.secondary)
            .fixedSize(horizontal: false, vertical: true)
            FlowLayout(spacing: 8) {
                Button(terminal.isTerminalOpen ? "Open a New Window" : "Open Terminal") {
                    terminal.openTerminal()
                }
                if terminal.canPutBack {
                    Button("Put Back") {
                        terminal.perform(.putBack)
                    }
                    .disabled(terminal.isManaged)
                    .transition(.opacity)
                }
            }
            .buttonStyle(.bordered)
            .disabled(terminal.isQuittingTerminal)
            .padding(.top, 4)
            if terminal.isManaged {
                StatusLabel(title: Text("Locked by a profile"), systemImage: "lock", tint: .accentColor)
                    .font(.caption)
                    .foregroundStyle(.secondary)
            } else if terminal.wasRefused {
                StatusLabel(title: Text("macOS refused it"), tint: .accentColor)
                    .font(.caption)
                    .foregroundStyle(.secondary)
            } else if terminal.areProfilesUnreadable {
                StatusLabel(title: Text("Terminal’s profiles couldn’t be read"), tint: .accentColor)
                    .font(.caption)
                    .foregroundStyle(.secondary)
            }
        }
        .motion(value: terminal.canPutBack)
    }
}

private struct TerminalThemeGrid: View {
    @Environment(TerminalLibrary.self) private var terminal

    var body: some View {
        LazyVGrid(columns: [GridItem(.adaptive(minimum: 116), spacing: 14)], spacing: 16) {
            ForEach(TerminalThemeCatalog.all) { theme in
                let isInUse = terminal.themeInUse == theme
                Button {
                    terminal.perform(.use(theme))
                } label: {
                    TerminalThemeCard(theme: theme, isInUse: isInUse)
                }
                .buttonStyle(.plain)
                .accessibilityLabel(Text(verbatim: theme.name))
                .accessibilityAddTraits(isInUse ? .isSelected : [])
            }
        }
        .disabled(terminal.isQuittingTerminal || terminal.isManaged)
        .padding(.vertical, 8)
    }
}

private struct TerminalSettingsForm: View {
    @Environment(TerminalLibrary.self) private var terminal

    var body: some View {
        Form {
            Section("When Terminal Opens") {
                if terminal.quietsLogin {
                    RemovalsHeldBanner()
                }
                QuietLoginRow()
                if let sessions = terminal.shellSessions {
                    ShellSessionsRow(sessions: sessions)
                }
                if terminal.showsWindowSwitch {
                    TweakRow(tweak: TweakCatalog.terminalWindows)
                }
            }
            Section {
                ForEach(TerminalOption.allCases, id: \.self) { option in
                    TerminalOptionRow(option: option)
                }
            } header: {
                Text("Theme in Use")
            } footer: {
                if terminal.isManaged {
                    StatusLabel(title: Text("Locked by a profile"), systemImage: "lock", tint: .accentColor)
                        .font(.caption)
                        .foregroundStyle(.secondary)
                } else if terminal.areProfilesUnreadable {
                    StatusLabel(title: Text("Terminal’s profiles couldn’t be read"), tint: .accentColor)
                        .font(.caption)
                        .foregroundStyle(.secondary)
                } else if terminal.options == nil {
                    Text("Choose one of Peel’s themes to change these settings.")
                        .font(.caption)
                        .foregroundStyle(.secondary)
                } else if terminal.wasRefused {
                    StatusLabel(title: Text("macOS refused it"), tint: .accentColor)
                        .font(.caption)
                        .foregroundStyle(.secondary)
                }
            }
        }
        .formStyle(.grouped)
    }
}

private struct TerminalOptionRow: View {
    @Environment(TerminalLibrary.self) private var terminal
    let option: TerminalOption

    var body: some View {
        TerminalSwitchRow(
            title: option.title,
            detail: option.detail,
            footnote: option.rawValue,
            isOn: Binding(
                get: { terminal.options?.contains(option) == true },
                set: { terminal.perform(.set(option, $0)) }
            ),
            isDisabled: terminal.options == nil || terminal.isManaged || terminal.isQuittingTerminal
        )
    }
}

extension TerminalOption {
    fileprivate var title: LocalizedStringResource {
        switch self {
        case .optionAsMeta: "Use Option as Meta key"
        case .noAlertSound: "No alert sound"
        }
    }

    fileprivate var detail: LocalizedStringResource {
        switch self {
        case .optionAsMeta: "The Option key works as the Meta key, which some text editors use for their commands. Option then no longer types the characters it types on your keyboard, such as @ and [ on a German keyboard."
        case .noAlertSound: "When a program rings Terminal’s bell, as with Control-G, Terminal plays no sound."
        }
    }
}

private struct QuietLoginRow: View {
    @Environment(TerminalLibrary.self) private var terminal
    @Environment(RemovalHistoryStore.self) private var history
    @Environment(RemovalOutcome.self) private var outcome
    @State private var asked: Bool?

    var body: some View {
        TerminalSwitchRow(
            title: "No “Last login” line",
            detail: "New Terminal windows start at the prompt, without the line that says when you last logged in. Logins to this Mac over SSH leave it out too.",
            footnote: HushLogin.url(in: .homeDirectory).abbreviatedPath,
            caption: "Takes effect in new windows",
            isOn: isOn,
            isDisabled: terminal.isMovingQuietLogin || terminal.quietsLogin && !canMoveToTrash
        )
    }

    private var canMoveToTrash: Bool {
        history.mayMove
    }

    private var isOn: Binding<Bool> {
        Binding(
            get: { asked ?? terminal.quietsLogin },
            set: { on in
                guard !on else {
                    terminal.turnOnQuietLogin()
                    return
                }
                asked = false
                Task {
                    let result = await history.moving(tool: .terminal, source: "Terminal", sourceKey: "tool") {
                        await terminal.moveQuietLoginToTrash { result, sizes in
                            await history.record(
                                result, tool: .terminal, source: "Terminal", sourceKey: "tool", sizes: sizes
                            )
                        }
                    }
                    asked = nil
                    outcome.report(result)
                }
            }
        )
    }
}

private struct ShellSessionsRow: View {
    let sessions: ShellSessions

    var body: some View {
        VStack(alignment: .leading, spacing: 2) {
            HStack(spacing: 5) {
                Text("No “Restored session” line")
                    .font(.body.weight(.semibold))
                    .lineLimit(1)
                    .minimumScaleFactor(0.8)
                InfoNote(
                    name: String(localized: "No “Restored session” line"),
                    detail: Text("When Terminal reopens a window, \(sessions.name) starts it with a “Restored session” line and gives it back its own command history. For that, every shell saves its session as it ends, with a “Saving session” line, and keeps it for two weeks. Peel doesn’t change your shell’s files."),
                    footnote: Text(verbatim: sessions.script)
                )
            }
            CopyableLines(caption: caption, lines: [sessions.turnOff])
        }
        .padding(.vertical, 4)
    }

    private var caption: Text {
        switch sessions {
        case .zsh(let startupFile): Text("Add this line to \(startupFile.abbreviatedPath)")
        case .bash: Text("Run this command in Terminal")
        }
    }
}