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.0 KB
Raw Blame
import PeelCore
import SwiftUI

/// A row showing one thing `brew doctor` found.
///
/// The first line of Homebrew's text is the summary, and the lines after it, paths or Homebrew's own sentences, are
/// listed under it. Then come Homebrew's advice and each command it suggests, with a Copy button beside each command.
struct HomebrewFindingRow: View {
    let finding: HomebrewFinding

    var body: some View {
        HStack(alignment: .top, spacing: 10) {
            Image(systemName: "exclamationmark.triangle.fill")
                .foregroundStyle(.orange)
                .frame(width: 20)
                .accessibilityHidden(true)
            VStack(alignment: .leading, spacing: 5) {
                Text(verbatim: summary)
                    .font(.callout.weight(.medium))
                    .textSelection(.enabled)
                ForEach(Array(details.enumerated()), id: \.offset) { _, line in
                    HomebrewFindingLine(line: line)
                }
                if let remedy {
                    Text(verbatim: remedy)
                        .font(.caption)
                        .foregroundStyle(.secondary)
                        .padding(.top, 1)
                }
                ForEach(Array(finding.commands.enumerated()), id: \.offset) { _, command in
                    HStack(spacing: 8) {
                        Text(verbatim: command)
                            .font(.caption.monospaced())
                            .lineLimit(1)
                            .truncationMode(.middle)
                            .textSelection(.enabled)
                            .padding(.horizontal, 8)
                            .padding(.vertical, 3)
                            .background(.quaternary, in: .rect(cornerRadius: 6))
                        CopyButton(text: command)
                            .buttonStyle(.bordered)
                            .controlSize(.small)
                    }
                }
            }
            Spacer(minLength: 0)
        }
        .padding(.vertical, 4)
    }

    private var lines: [String] {
        finding.text
            .split(separator: "\n", omittingEmptySubsequences: false)
            .map { $0.trimmingCharacters(in: .whitespaces) }
            .filter { !$0.isEmpty }
    }

    private var summary: String { lines.first ?? finding.text }

    private var details: [String] { Array(lines.dropFirst()) }

    /// Homebrew's advice without the backticks it puts around commands. The commands have rows of their own
    /// below, so the marks would only be clutter.
    private var remedy: String? {
        finding.remedy?.replacingOccurrences(of: "`", with: "")
    }
}

/// A line of Homebrew's text under a finding's summary, shown whole: it can be a sentence as well as a path.
struct HomebrewFindingLine: View {
    let line: String

    var body: some View {
        Text(verbatim: line)
            .font(.caption.monospaced())
            .foregroundStyle(.secondary)
            .fixedSize(horizontal: false, vertical: true)
            .textSelection(.enabled)
    }
}