chore(swabble): apply swiftformat

This commit is contained in:
Peter Steinberger
2025-12-09 15:36:41 +01:00
parent 336c9d6caa
commit 318457cb2c
13 changed files with 74 additions and 75 deletions

View File

@@ -17,11 +17,11 @@ public actor HookRunner {
public init(config: SwabbleConfig) {
self.config = config
self.hostname = Host.current().localizedName ?? "host"
hostname = Host.current().localizedName ?? "host"
}
public func shouldRun() -> Bool {
guard self.config.hook.cooldownSeconds > 0 else { return true }
guard config.hook.cooldownSeconds > 0 else { return true }
if let lastRun, Date().timeIntervalSince(lastRun) < config.hook.cooldownSeconds {
return false
}
@@ -29,23 +29,23 @@ public actor HookRunner {
}
public func run(job: HookJob) async throws {
guard self.shouldRun() else { return }
guard !self.config.hook.command.isEmpty else { throw NSError(
guard shouldRun() else { return }
guard !config.hook.command.isEmpty else { throw NSError(
domain: "Hook",
code: 1,
userInfo: [NSLocalizedDescriptionKey: "hook command not set"]) }
let prefix = self.config.hook.prefix.replacingOccurrences(of: "${hostname}", with: self.hostname)
let prefix = config.hook.prefix.replacingOccurrences(of: "${hostname}", with: hostname)
let payload = prefix + job.text
let process = Process()
process.executableURL = URL(fileURLWithPath: self.config.hook.command)
process.arguments = self.config.hook.args + [payload]
process.executableURL = URL(fileURLWithPath: config.hook.command)
process.arguments = config.hook.args + [payload]
var env = ProcessInfo.processInfo.environment
env["SWABBLE_TEXT"] = job.text
env["SWABBLE_PREFIX"] = prefix
for (k, v) in self.config.hook.env {
for (k, v) in config.hook.env {
env[k] = v
}
process.environment = env
@@ -70,6 +70,6 @@ public actor HookRunner {
try await group.next()
group.cancelAll()
}
self.lastRun = Date()
lastRun = Date()
}
}

View File

@@ -35,18 +35,18 @@ public actor SpeechPipeline {
transcriptionOptions: etiquette ? [.etiquetteReplacements] : [],
reportingOptions: [.volatileResults],
attributeOptions: [])
self.transcriber = transcriberModule
transcriber = transcriberModule
guard let analyzerFormat = await SpeechAnalyzer.bestAvailableAudioFormat(compatibleWith: [transcriberModule])
else {
throw SpeechPipelineError.analyzerFormatUnavailable
}
self.analyzer = SpeechAnalyzer(modules: [transcriberModule])
analyzer = SpeechAnalyzer(modules: [transcriberModule])
let (stream, continuation) = AsyncStream<AnalyzerInput>.makeStream()
self.inputContinuation = continuation
inputContinuation = continuation
let inputNode = self.engine.inputNode
let inputNode = engine.inputNode
let inputFormat = inputNode.outputFormat(forBus: 0)
inputNode.removeTap(onBus: 0)
inputNode.installTap(onBus: 0, bufferSize: 2048, format: inputFormat) { [weak self] buffer, _ in
@@ -55,11 +55,11 @@ public actor SpeechPipeline {
Task { await self.handleBuffer(boxed.buffer, targetFormat: analyzerFormat) }
}
self.engine.prepare()
try self.engine.start()
try await self.analyzer?.start(inputSequence: stream)
engine.prepare()
try engine.start()
try await analyzer?.start(inputSequence: stream)
guard let transcriberForStream = self.transcriber else {
guard let transcriberForStream = transcriber else {
throw SpeechPipelineError.transcriberUnavailable
}
@@ -82,18 +82,18 @@ public actor SpeechPipeline {
}
public func stop() async {
self.resultTask?.cancel()
self.inputContinuation?.finish()
self.engine.inputNode.removeTap(onBus: 0)
self.engine.stop()
try? await self.analyzer?.finalizeAndFinishThroughEndOfInput()
resultTask?.cancel()
inputContinuation?.finish()
engine.inputNode.removeTap(onBus: 0)
engine.stop()
try? await analyzer?.finalizeAndFinishThroughEndOfInput()
}
private func handleBuffer(_ buffer: AVAudioPCMBuffer, targetFormat: AVAudioFormat) async {
do {
let converted = try converter.convert(buffer, to: targetFormat)
let input = AnalyzerInput(buffer: converted)
self.inputContinuation?.yield(input)
inputContinuation?.yield(input)
} catch {
// drop on conversion failure
}

View File

@@ -27,11 +27,11 @@ public struct Logger: Sendable {
print("[\(level.rawValue.uppercased())] \(ts) | \(message)")
}
public func trace(_ msg: String) { self.log(.trace, msg) }
public func debug(_ msg: String) { self.log(.debug, msg) }
public func info(_ msg: String) { self.log(.info, msg) }
public func warn(_ msg: String) { self.log(.warn, msg) }
public func error(_ msg: String) { self.log(.error, msg) }
public func trace(_ msg: String) { log(.trace, msg) }
public func debug(_ msg: String) { log(.debug, msg) }
public func info(_ msg: String) { log(.info, msg) }
public func warn(_ msg: String) { log(.warn, msg) }
public func error(_ msg: String) { log(.error, msg) }
}
extension LogLevel {

View File

@@ -11,24 +11,24 @@ public actor TranscriptsStore {
let dir = FileManager.default.homeDirectoryForCurrentUser
.appendingPathComponent("Library/Application Support/swabble", isDirectory: true)
try? FileManager.default.createDirectory(at: dir, withIntermediateDirectories: true)
self.fileURL = dir.appendingPathComponent("transcripts.log")
fileURL = dir.appendingPathComponent("transcripts.log")
if let data = try? Data(contentsOf: fileURL),
let text = String(data: data, encoding: .utf8)
{
self.entries = text.split(separator: "\n").map(String.init).suffix(self.limit)
entries = text.split(separator: "\n").map(String.init).suffix(limit)
}
}
public func append(text: String) {
self.entries.append(text)
if self.entries.count > self.limit {
self.entries.removeFirst(self.entries.count - self.limit)
entries.append(text)
if entries.count > limit {
entries.removeFirst(entries.count - limit)
}
let body = self.entries.joined(separator: "\n")
try? body.write(to: self.fileURL, atomically: false, encoding: .utf8)
let body = entries.joined(separator: "\n")
try? body.write(to: fileURL, atomically: false, encoding: .utf8)
}
public func latest() -> [String] { self.entries }
public func latest() -> [String] { entries }
}
extension String {