58 lines
2.3 KiB
Swift
58 lines
2.3 KiB
Swift
import Foundation
|
|
|
|
/// A pointer at code the agent understands: `path`, `path:42` or `path:42-58`.
|
|
public struct AgentReference: Sendable, Equatable, CustomStringConvertible {
|
|
public var path: String
|
|
/// First line, 1-based; nil for a whole-file (or folder) reference.
|
|
public var startLine: Int?
|
|
/// Last line; equal to `startLine` for a single line.
|
|
public var endLine: Int?
|
|
|
|
public init(path: String, startLine: Int? = nil, endLine: Int? = nil) {
|
|
self.path = path
|
|
self.startLine = startLine
|
|
self.endLine = endLine ?? startLine
|
|
}
|
|
|
|
/// A reference to a line range.
|
|
public init(path: String, lines: ClosedRange<Int>) {
|
|
self.init(path: path, startLine: lines.lowerBound, endLine: lines.upperBound)
|
|
}
|
|
|
|
public var description: String {
|
|
guard let start = startLine else { return path }
|
|
guard let end = endLine, end != start else { return "\(path):\(start)" }
|
|
return "\(path):\(start)-\(end)"
|
|
}
|
|
}
|
|
|
|
public enum PassOn {
|
|
/// The exact text a "Pass on …" action inserts into the agent.
|
|
///
|
|
/// With code: `note ref` then the code in a fenced block. Without: `note => ref`,
|
|
/// or just `ref` when the note is empty.
|
|
public static func compose(note: String, reference: String, code: String? = nil) -> String {
|
|
let n = note.trimmingCharacters(in: .whitespacesAndNewlines)
|
|
if let code {
|
|
let head = (n.isEmpty ? "" : n + " ") + reference
|
|
return head + "\n```\n" + code + "\n```"
|
|
}
|
|
return n.isEmpty ? reference : "\(n) => \(reference)"
|
|
}
|
|
}
|
|
|
|
public enum BracketedPaste {
|
|
public static let start = "\u{1b}[200~"
|
|
public static let end = "\u{1b}[201~"
|
|
|
|
/// Wraps text so the CLI treats it as pasted and does not submit it.
|
|
///
|
|
/// ESC and the 8-bit CSI are stripped from the payload: a file or note holding `ESC[201~`
|
|
/// would otherwise end the paste early and let the rest run as typed input.
|
|
public static func wrap(_ text: String, composerHasText: Bool) -> String {
|
|
var payload = String.UnicodeScalarView()
|
|
payload.append(contentsOf: text.unicodeScalars.lazy.filter { $0 != "\u{1b}" && $0 != "\u{9b}" })
|
|
return start + (composerHasText ? "\n" : "") + String(payload) + end
|
|
}
|
|
}
|