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.

3.6 KB
Raw Blame
import PeelCore
import SwiftUI

struct GitTab: View {
    @Environment(GitLibrary.self) private var git
    @State private var isConfirmingTurnAllOff = false

    var body: some View {
        Form {
            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 {
                        StatusLabel(title: Text("Git couldn’t read its settings"), tint: .accentColor)
                    }
                } else if state.known == nil {
                    Section {
                        StatusLabel(title: Text("Git didn’t answer"), tint: .accentColor)
                    }
                } else if git.wasRefused {
                    Section {
                        StatusLabel(title: Text(verbatim: git.whyRefused.sentence), tint: .accentColor)
                    }
                }
                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)
                    }
                }
            }
        }
        .formStyle(.grouped)
        .edgeBar(.bottom) {
            TurnAllOffBar(
                explanation: "Peel changes these with `git config --global`. Turning one off puts back what was there before Peel, or leaves it to Git.",
                isEnabled: git.hasSomethingOn
            ) {
                isConfirmingTurnAllOff = true
            }
        }
        .alert("Turn off all Git settings?", isPresented: $isConfirmingTurnAllOff) {
            Button("Turn All Off") { git.turnAllOff() }
            Button("Cancel", role: .cancel) {}
        } message: {
            Text("Settings Peel changed go back to what they were, and the rest go back to Git’s defaults.")
        }
    }
}

private struct GitSettingRow: View {
    @Environment(GitLibrary.self) private var git
    let setting: GitSetting

    var body: some View {
        TerminalSwitchRow(
            title: setting.title,
            detail: setting.detail,
            footnote: setting.commands(signingKey: git.state?.signingKey ?? "~/.ssh/id_ed25519.pub")
                .joined(separator: "\n"),
            caption: caption,
            isOn: Binding(
                get: { git.isOn(setting) },
                set: { git.set(setting, to: $0) }
            ),
            isDisabled: !git.isOffered(setting)
        )
    }

    private var caption: LocalizedStringResource? {
        if git.isSetInIncludedFiles(setting) { return "Set in a file your Git settings include" }
        guard setting == .signCommits else { return nil }
        guard let key = git.state?.signingKey else { return "Needs an SSH key in ~/.ssh" }
        return GitSetting.isTheKeyItself(key)
            ? "With the key written in your Git settings"
            : "With \(URL(filePath: key).abbreviatedPath)"
    }
}