adds claude-generated commit messages and a recent commits panel to the git view - adds log tailing in the explorer, branch merging and a clean up button
This commit is contained in:
@@ -5,6 +5,8 @@ public enum TerminalKind: String, Sendable, Equatable, CaseIterable {
|
||||
case agent
|
||||
/// The bottom pane: a plain login shell.
|
||||
case shell
|
||||
/// The Explorer's log block: `tail -f` on one file.
|
||||
case tail
|
||||
}
|
||||
|
||||
/// Executable, arguments and environment for one terminal pane.
|
||||
@@ -21,7 +23,7 @@ public struct TerminalLaunch: Sendable, Equatable {
|
||||
self.workingDirectory = workingDirectory
|
||||
}
|
||||
|
||||
/// The launch for a pane. The agent runs as `$SHELL -i -c <command>`: `-i` still sources the rc
|
||||
/// The launch for the agent or shell pane. The agent runs as `$SHELL -i -c <command>`: `-i` still sources the rc
|
||||
/// files (nvm etc.) but leaves no prompt line or echoed command behind.
|
||||
public static func make(kind: TerminalKind, config: HelderConfig, projectRoot: String?, environment base: [String: String]) -> TerminalLaunch {
|
||||
let shell = config.terminal.shell ?? base["SHELL"] ?? "/bin/zsh"
|
||||
@@ -30,6 +32,12 @@ public struct TerminalLaunch: Sendable, Equatable {
|
||||
return TerminalLaunch(executable: shell, arguments: args, environment: environment(from: base), workingDirectory: cwd)
|
||||
}
|
||||
|
||||
/// `tail -f` on a project-relative path, run directly without a shell.
|
||||
public static func tail(path: String, projectRoot: String?, environment base: [String: String]) -> TerminalLaunch {
|
||||
TerminalLaunch(executable: "/usr/bin/tail", arguments: ["-f", path], environment: environment(from: base),
|
||||
workingDirectory: projectRoot ?? base["HOME"] ?? "/")
|
||||
}
|
||||
|
||||
/// The child environment. A GUI launch inherits no locale and no COLORTERM, which turns
|
||||
/// multibyte output into mojibake and shifts the CLI's 24-bit colours; npm's prefix
|
||||
/// variables make nvm refuse to load. `TERM_PROGRAM` tells the agent it runs in Helder.
|
||||
|
||||
@@ -9,8 +9,8 @@ public enum ContextTarget: Sendable, Equatable {
|
||||
case directory(path: String)
|
||||
/// A file row in the Git view; a deleted file has nothing to open.
|
||||
case gitRow(path: String, isDeleted: Bool)
|
||||
/// The caret beside the Git view's Commit button.
|
||||
case commitMenu
|
||||
/// A row in the Git view's Branches panel; `current` is the checked-out branch.
|
||||
case branch(name: String, remote: Bool, current: String, isDirty: Bool)
|
||||
/// The empty space under the tree: the project folder itself.
|
||||
case projectRoot
|
||||
case terminal(TerminalKind, hasSelection: Bool)
|
||||
@@ -19,7 +19,7 @@ public enum ContextTarget: Sendable, Equatable {
|
||||
switch self {
|
||||
case let .editor(reference, _, _): reference.path
|
||||
case let .file(path), let .directory(path), let .gitRow(path, _): path
|
||||
case .projectRoot, .terminal, .commitMenu: ""
|
||||
case .projectRoot, .terminal, .branch: ""
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,8 @@ public enum ContextAction: Sendable, Equatable {
|
||||
case openFile(String)
|
||||
/// Reverts unstaged changes: modified and deleted files come back from the index, new files are removed.
|
||||
case gitDiscard([String])
|
||||
case commit(andPush: Bool)
|
||||
/// The Git view's Clean up: every unstaged change reverted to the index, every untracked file and folder removed.
|
||||
case gitCleanUp
|
||||
/// Opens the Diff of the open file at this line.
|
||||
case showDiff(String, line: Int)
|
||||
case rename(String, isDirectory: Bool)
|
||||
@@ -52,16 +53,20 @@ public enum ContextAction: Sendable, Equatable {
|
||||
case bouwen(String)
|
||||
/// Reads a Markdown file's prose aloud.
|
||||
case readAloudFile(String)
|
||||
/// Runs `tail -f` on a `.log` file in the Explorer's tail block, replacing any running tail.
|
||||
case tailLog(String)
|
||||
case readAloudText(String)
|
||||
/// Sends the editor selection to Plak as it is, code and all.
|
||||
case plak(String)
|
||||
/// Sends the pane's selection to Plak; the text is fetched when the item runs.
|
||||
case plakTerminalSelection(TerminalKind)
|
||||
/// Merges this branch into the current one.
|
||||
case mergeBranch(String)
|
||||
|
||||
/// Destructive items read in amber-deep.
|
||||
public var isDestructive: Bool {
|
||||
switch self {
|
||||
case .gitDiscard, .delete: true
|
||||
case .gitDiscard, .gitCleanUp, .delete: true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
@@ -69,7 +74,7 @@ public enum ContextAction: Sendable, Equatable {
|
||||
/// The menu's main item, set in bold.
|
||||
public var isEmphasized: Bool {
|
||||
switch self {
|
||||
case .openFile, .commit(andPush: true): true
|
||||
case .openFile: true
|
||||
default: false
|
||||
}
|
||||
}
|
||||
@@ -78,7 +83,15 @@ public enum ContextAction: Sendable, Equatable {
|
||||
/// A menu entry, or a separator between groups.
|
||||
public enum ContextMenuEntry: Sendable, Equatable {
|
||||
case action(ContextAction, label: String)
|
||||
/// Shown dimmed with its `hint` as tooltip; the keyboard skips it and a click does nothing.
|
||||
case disabled(ContextAction, label: String, hint: String)
|
||||
case separator
|
||||
|
||||
/// The keyboard can land on it.
|
||||
public var isSelectable: Bool {
|
||||
if case .action = self { return true }
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
public enum ContextActions {
|
||||
@@ -86,8 +99,9 @@ public enum ContextActions {
|
||||
public static func header(for target: ContextTarget, projectName: String) -> String {
|
||||
switch target {
|
||||
case let .file(path), let .gitRow(path, _): path
|
||||
case let .branch(name, _, _, _): name
|
||||
case .projectRoot: projectName + "/"
|
||||
case .editor, .directory, .terminal, .commitMenu: ""
|
||||
case .editor, .directory, .terminal: ""
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,7 +124,7 @@ public enum ContextActions {
|
||||
.action(.revealInFinder(""), label: "Show in Finder"),
|
||||
]
|
||||
case let .file(path):
|
||||
return fileHead(path) + readAloudFile(path) + [
|
||||
return fileHead(path) + readAloudFile(path) + tailLog(path) + [
|
||||
.separator,
|
||||
.action(.rename(path, isDirectory: false), label: "Rename file"),
|
||||
.action(.revealInFinder(path), label: "Show in Finder"),
|
||||
@@ -136,19 +150,20 @@ public enum ContextActions {
|
||||
.separator, .action(.plakTerminalSelection(kind), label: "Plak")] : []
|
||||
let waitWhat: [ContextMenuEntry] = hasSelection && kind == .agent
|
||||
? [.separator, .action(.waitWhat(kind), label: "Wait what?")] : []
|
||||
return (hasSelection ? [.action(.copyTerminalSelection(kind), label: "Copy")] : [])
|
||||
+ [.action(.pasteIntoTerminal(kind), label: "Paste")] + readAloud + waitWhat
|
||||
let paste: [ContextMenuEntry] = kind == .tail ? [] : [.action(.pasteIntoTerminal(kind), label: "Paste")]
|
||||
return (hasSelection ? [.action(.copyTerminalSelection(kind), label: "Copy")] : []) + paste + readAloud + waitWhat
|
||||
case let .gitRow(path, isDeleted):
|
||||
let open: [ContextMenuEntry] = isDeleted ? [] : [.action(.openFile(path), label: "Open in editor"), .separator]
|
||||
return open + [
|
||||
.action(.passOnReference(path, code: nil), label: "Pass on reference"),
|
||||
.action(.copyReference(path), label: "Copy reference"),
|
||||
]
|
||||
case .commitMenu:
|
||||
return [
|
||||
.action(.commit(andPush: false), label: "Commit"),
|
||||
.action(.commit(andPush: true), label: "Commit and push"),
|
||||
]
|
||||
case let .branch(name, remote, current, isDirty):
|
||||
if !remote && name == current { return [] }
|
||||
let label = "Merge into \(current)"
|
||||
return [isDirty
|
||||
? .disabled(.mergeBranch(name), label: label, hint: "Commit or discard your changes first")
|
||||
: .action(.mergeBranch(name), label: label)]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -163,6 +178,10 @@ public enum ContextActions {
|
||||
FileType.isMarkdown(path) ? [.separator, .action(.readAloudFile(path), label: "Read aloud")] : []
|
||||
}
|
||||
|
||||
private static func tailLog(_ path: String) -> [ContextMenuEntry] {
|
||||
(path as NSString).pathExtension.lowercased() == "log" ? [.separator, .action(.tailLog(path), label: "Tail log")] : []
|
||||
}
|
||||
|
||||
/// Selected Markdown is read as prose; a selection with no prose left, such as one code block, is read as it is.
|
||||
private static func readAloudSelection(_ code: String, path: String) -> [ContextMenuEntry] {
|
||||
let prose = FileType.isMarkdown(path) ? MarkdownSpeech.plainText(MarkdownParser.parse(code)) : ""
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
import Foundation
|
||||
|
||||
/// The staged changes as `git diff --cached` prints them: the `--stat` summary and the full patch.
|
||||
public struct StagedDiff: Sendable, Equatable {
|
||||
public var stat: String
|
||||
public var patch: String
|
||||
|
||||
public init(stat: String, patch: String) {
|
||||
self.stat = stat
|
||||
self.patch = patch
|
||||
}
|
||||
|
||||
public var isEmpty: Bool {
|
||||
patch.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
|
||||
}
|
||||
}
|
||||
|
||||
/// The prompt that asks the Claude CLI for one commit message, and the cleaning of its answer.
|
||||
public enum CommitMessagePrompt {
|
||||
public static let maxPatchCharacters = 60_000
|
||||
|
||||
public static let examples = [
|
||||
"Adds a new button on the header to navigate to profile",
|
||||
"Fixed security issue that made it possible to hack a file upload",
|
||||
"Makes sure the register form does not accept R&B fans - Enforces password rules on login form",
|
||||
]
|
||||
|
||||
/// The full prompt; a patch over `limit` characters is cut and the prompt says so, the stat always stays whole.
|
||||
public static func make(_ diff: StagedDiff, limit: Int = maxPatchCharacters) -> String {
|
||||
let isCut = diff.patch.count > limit
|
||||
let patch = isCut ? String(diff.patch.prefix(limit)) : diff.patch
|
||||
var lines = [
|
||||
"Write ONE git commit message for the staged changes below.",
|
||||
"",
|
||||
"Rules:",
|
||||
"- One single sentence on one line. No body, no quotes, no prefix such as \"feat:\". A trailing period is not needed.",
|
||||
"- English. Start with a verb in the third person or in the past tense.",
|
||||
"- When two separate things changed, join them with \" - \" in that one line.",
|
||||
"- Reply with the message only, nothing else.",
|
||||
"",
|
||||
"Examples:",
|
||||
"- \(examples[0])",
|
||||
"- \(examples[1])",
|
||||
"- \(examples[2]) (two things changed)",
|
||||
"",
|
||||
"Changed files (git diff --cached --stat):",
|
||||
diff.stat.trimmingCharacters(in: .newlines),
|
||||
"",
|
||||
"Diff (git diff --cached):",
|
||||
]
|
||||
if isCut {
|
||||
lines.append("(The diff is cut after \(limit) characters; the file list above is complete.)")
|
||||
}
|
||||
lines.append(patch.trimmingCharacters(in: .newlines))
|
||||
return lines.joined(separator: "\n")
|
||||
}
|
||||
|
||||
/// The first non-empty line, Markdown fence lines skipped, without surrounding quotes or backticks and with single spaces;
|
||||
/// nil when nothing is left.
|
||||
public static func clean(_ output: String) -> String? {
|
||||
let wrappers: Set<Character> = ["\"", "'", "`", "“", "”", "‘", "’"]
|
||||
guard var line = output.split(whereSeparator: \.isNewline)
|
||||
.map({ $0.trimmingCharacters(in: .whitespaces) })
|
||||
.first(where: { !$0.isEmpty && !$0.hasPrefix("```") }) else { return nil }
|
||||
while let first = line.first, let last = line.last, line.count >= 2, wrappers.contains(first), wrappers.contains(last) {
|
||||
line = String(line.dropFirst().dropLast()).trimmingCharacters(in: .whitespaces)
|
||||
}
|
||||
line = line.replacingOccurrences(of: "\\s+", with: " ", options: .regularExpression)
|
||||
return line.isEmpty ? nil : line
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import Foundation
|
||||
|
||||
/// One commit of HEAD's recent history, as the Git view's commit list shows it.
|
||||
public struct GitCommitSummary: Sendable, Equatable {
|
||||
public var hash: String
|
||||
public var subject: String
|
||||
public var author: String
|
||||
public var date: Date
|
||||
|
||||
public init(hash: String, subject: String, author: String, date: Date) {
|
||||
self.hash = hash
|
||||
self.subject = subject
|
||||
self.author = author
|
||||
self.date = date
|
||||
}
|
||||
}
|
||||
|
||||
public enum GitCommitLogParsing {
|
||||
public static let limit = 30
|
||||
/// NUL between fields and a record separator after each commit, so no subject can split a record.
|
||||
public static let arguments = ["log", "-n", "\(limit)", "--no-color", "--format=%h%x00%s%x00%an%x00%ct%x1e"]
|
||||
|
||||
/// Reads the records `arguments` prints, newest first; a malformed record is skipped.
|
||||
public static func commits(_ raw: String) -> [GitCommitSummary] {
|
||||
raw.split(separator: "\u{1e}").compactMap { record in
|
||||
let text = record.trimmingCharacters(in: .newlines)
|
||||
let fields = text.split(separator: "\0", omittingEmptySubsequences: false)
|
||||
guard fields.count == 4, !fields[0].isEmpty, let seconds = TimeInterval(fields[3]) else { return nil }
|
||||
return GitCommitSummary(hash: String(fields[0]), subject: String(fields[1]), author: String(fields[2]),
|
||||
date: Date(timeIntervalSince1970: seconds))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum RelativeTime {
|
||||
/// "just now", "5 min ago", "3 h ago", "2 d ago", "3 w ago", "4 mo ago", "2 y ago"; a future date reads "just now".
|
||||
public static func text(_ date: Date, now: Date) -> String {
|
||||
let seconds = Int(now.timeIntervalSince(date))
|
||||
let minute = 60, hour = 3_600, day = 86_400
|
||||
switch seconds {
|
||||
case ..<minute: return "just now"
|
||||
case ..<hour: return "\(seconds / minute) min ago"
|
||||
case ..<day: return "\(seconds / hour) h ago"
|
||||
case ..<(7 * day): return "\(seconds / day) d ago"
|
||||
case ..<(30 * day): return "\(seconds / (7 * day)) w ago"
|
||||
case ..<(365 * day): return "\(seconds / (30 * day)) mo ago"
|
||||
default: return "\(seconds / (365 * day)) y ago"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user