swift format

This commit is contained in:
Salar Rahmanian 2025-02-09 10:34:15 -08:00
parent f69cec36ac
commit 54edbfc36c
8 changed files with 407 additions and 395 deletions

View file

@ -14,7 +14,6 @@
// limitations under the License.
//
import ArgumentParser
import Foundation
@ -22,81 +21,83 @@ let DEFAULT_FISH_HISTORY_LOCATION: String = "~/.local/share/fish/fish_history"
@main
struct Fishee: ParsableCommand {
@Option(name: [.short, .customLong("history-file")], help: "Location of your fish history file. Will default to ~/.local/share/fish/fish_history")
var fishHistoryLocationStr: String?
@Option(name: .shortAndLong, help: "File path to file to merge with history file.")
var mergeFile: String?
@Option(
name: [.short, .customLong("output-file")],
help: "File to write to. Default: same as current history file."
)
var writeFileStr: String?
@Flag(
name: .shortAndLong,
help: "Dry run. Will only print to the console without actually modifying the history file."
)
var dryRun: Bool = false
@Flag(
name: .shortAndLong,
help: "Remove duplicates from combined history. Default: false"
)
var removeDuplicates: Bool = false
@Flag(
name: .shortAndLong,
inversion: .prefixedNo,
help: "Backup fish history file given before writing."
)
var backup: Bool = true
var fishHistoryLocation: URL? {
let pathStr = fishHistoryLocationStr ?? DEFAULT_FISH_HISTORY_LOCATION
return getPath(pathStr)
}
var writeFileLocation: URL? {
let pathStr = writeFileStr ?? DEFAULT_FISH_HISTORY_LOCATION
return getPath(pathStr)
}
public func run() throws {
let mergeFileLocation = mergeFile.flatMap { getPath($0) }
let finalHistory: [FishHistoryEntry] = switch (fishHistoryLocation, mergeFileLocation) {
case let (fishHistoryLocation?, mergeFileLocation?):
{
let currentHistory = parseFishHistory(from: fishHistoryLocation.path) ?? []
let toMergeHistory = parseFishHistory(from: mergeFileLocation.path) ?? []
return mergeFishHistory(currentHistory, toMergeHistory, removeDuplicates: removeDuplicates)
}()
case let (fishHistoryLocation?, nil):
parseFishHistory(from: fishHistoryLocation.path) ?? []
default:
[]
@Option(
name: [.short, .customLong("history-file")],
help: "Location of your fish history file. Will default to ~/.local/share/fish/fish_history")
var fishHistoryLocationStr: String?
@Option(name: .shortAndLong, help: "File path to file to merge with history file.")
var mergeFile: String?
@Option(
name: [.short, .customLong("output-file")],
help: "File to write to. Default: same as current history file."
)
var writeFileStr: String?
@Flag(
name: .shortAndLong,
help: "Dry run. Will only print to the console without actually modifying the history file."
)
var dryRun: Bool = false
@Flag(
name: .shortAndLong,
help: "Remove duplicates from combined history. Default: false"
)
var removeDuplicates: Bool = false
@Flag(
name: .shortAndLong,
inversion: .prefixedNo,
help: "Backup fish history file given before writing."
)
var backup: Bool = true
var fishHistoryLocation: URL? {
let pathStr = fishHistoryLocationStr ?? DEFAULT_FISH_HISTORY_LOCATION
return getPath(pathStr)
}
var writeFileLocation: URL? {
let pathStr = writeFileStr ?? DEFAULT_FISH_HISTORY_LOCATION
return getPath(pathStr)
}
public func run() throws {
let mergeFileLocation = mergeFile.flatMap { getPath($0) }
let finalHistory: [FishHistoryEntry] =
switch (fishHistoryLocation, mergeFileLocation) {
case let (fishHistoryLocation?, mergeFileLocation?):
{
let currentHistory = parseFishHistory(from: fishHistoryLocation.path) ?? []
let toMergeHistory = parseFishHistory(from: mergeFileLocation.path) ?? []
return mergeFishHistory(
currentHistory, toMergeHistory, removeDuplicates: removeDuplicates)
}()
case let (fishHistoryLocation?, nil):
parseFishHistory(from: fishHistoryLocation.path) ?? []
default:
[]
}
if dryRun {
finalHistory.forEach { print("\($0.writeEntry().joined(separator: "\n"))") }
} else {
if let writePath = writeFileLocation?.path {
let result = writeFishHistory(
to: writePath,
history: finalHistory,
backup: backup
)
if result {
print("Succussfully updated \(writePath)")
} else {
print("Failed to update \(writePath)")
}
if dryRun {
finalHistory.forEach { print("\($0.writeEntry().joined(separator: "\n"))") }
}
else {
if let writePath = writeFileLocation?.path {
let result = writeFishHistory(
to: writePath,
history: finalHistory,
backup: backup
)
if result {
print("Succussfully updated \(writePath)")
}
else {
print("Failed to update \(writePath)")
}
}
}
return
}
}
return
}
}