mirror of
https://github.com/softinio/Fishee.git
synced 2025-02-22 05:26:05 -08:00
swift format
This commit is contained in:
parent
f69cec36ac
commit
54edbfc36c
8 changed files with 407 additions and 395 deletions
|
@ -14,10 +14,8 @@
|
|||
// limitations under the License.
|
||||
//
|
||||
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
/// Gets the full file path to a given file.
|
||||
///
|
||||
/// - Parameters
|
||||
|
|
|
@ -14,7 +14,6 @@
|
|||
// limitations under the License.
|
||||
//
|
||||
|
||||
|
||||
import ArgumentParser
|
||||
import Foundation
|
||||
|
||||
|
@ -22,7 +21,9 @@ 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")
|
||||
@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.")
|
||||
|
@ -65,12 +66,14 @@ struct Fishee: ParsableCommand {
|
|||
|
||||
public func run() throws {
|
||||
let mergeFileLocation = mergeFile.flatMap { getPath($0) }
|
||||
let finalHistory: [FishHistoryEntry] = switch (fishHistoryLocation, mergeFileLocation) {
|
||||
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)
|
||||
return mergeFishHistory(
|
||||
currentHistory, toMergeHistory, removeDuplicates: removeDuplicates)
|
||||
}()
|
||||
case let (fishHistoryLocation?, nil):
|
||||
parseFishHistory(from: fishHistoryLocation.path) ?? []
|
||||
|
@ -80,8 +83,7 @@ struct Fishee: ParsableCommand {
|
|||
|
||||
if dryRun {
|
||||
finalHistory.forEach { print("\($0.writeEntry().joined(separator: "\n"))") }
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if let writePath = writeFileLocation?.path {
|
||||
let result = writeFishHistory(
|
||||
to: writePath,
|
||||
|
@ -90,8 +92,7 @@ struct Fishee: ParsableCommand {
|
|||
)
|
||||
if result {
|
||||
print("Succussfully updated \(writePath)")
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
print("Failed to update \(writePath)")
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,10 +14,8 @@
|
|||
// limitations under the License.
|
||||
//
|
||||
|
||||
|
||||
import Foundation
|
||||
|
||||
|
||||
/// Make a backup of the fish history.
|
||||
///
|
||||
/// ```
|
||||
|
@ -44,7 +42,8 @@ func backupHistory(_ path: String) -> Bool {
|
|||
let directory = fileURL.deletingLastPathComponent()
|
||||
|
||||
let newFileName = "\(fileNameWithoutExtension)_copy"
|
||||
let newFileURL = directory.appendingPathComponent(newFileName).appendingPathExtension(fileExtension)
|
||||
let newFileURL = directory.appendingPathComponent(newFileName).appendingPathExtension(
|
||||
fileExtension)
|
||||
|
||||
do {
|
||||
try? fileManager.removeItem(at: newFileURL)
|
||||
|
@ -57,7 +56,6 @@ func backupHistory(_ path: String) -> Bool {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
/// Write fish history to file.
|
||||
///
|
||||
/// - Parameters
|
||||
|
@ -88,8 +86,7 @@ func writeFishHistory(to path: String, history: [FishHistoryEntry], backup: Bool
|
|||
print("Error writing merged history: \(error)")
|
||||
return false
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
print("Nothing to write to \(path)")
|
||||
return false
|
||||
}
|
||||
|
@ -107,9 +104,13 @@ func parseFishHistory(from filePath: String) -> [FishHistoryEntry]? {
|
|||
return nil
|
||||
}
|
||||
|
||||
let lines = fileContents.split(separator: "\n").map { String($0).trimmingCharacters(in: .whitespaces) }
|
||||
let lines = fileContents.split(separator: "\n").map {
|
||||
String($0).trimmingCharacters(in: .whitespaces)
|
||||
}
|
||||
|
||||
let initialState: (entries: [FishHistoryEntry], currentCmd: String?, currentWhen: Int?, currentPaths: [String]) = ([], nil, nil, [])
|
||||
let initialState:
|
||||
(entries: [FishHistoryEntry], currentCmd: String?, currentWhen: Int?, currentPaths: [String]) =
|
||||
([], nil, nil, [])
|
||||
|
||||
let result = lines.reduce(into: initialState) { state, line in
|
||||
if line.starts(with: "- cmd:") {
|
||||
|
@ -147,12 +148,14 @@ func parseFishHistory(from filePath: String) -> [FishHistoryEntry]? {
|
|||
/// - removeDuplicates: if true, remove any duplicates found after merging the two lists.
|
||||
///
|
||||
/// - Returns: Single list of ``FishHistoryEntry`` entries.
|
||||
func mergeFishHistory(_ left: [FishHistoryEntry], _ right: [FishHistoryEntry], removeDuplicates: Bool = false) -> [FishHistoryEntry] {
|
||||
func mergeFishHistory(
|
||||
_ left: [FishHistoryEntry], _ right: [FishHistoryEntry], removeDuplicates: Bool = false
|
||||
) -> [FishHistoryEntry] {
|
||||
|
||||
let merged = left + right
|
||||
|
||||
if removeDuplicates {
|
||||
let finalList = merged.reduce(into: [String:FishHistoryEntry]()) { result, entry in
|
||||
let finalList = merged.reduce(into: [String: FishHistoryEntry]()) { result, entry in
|
||||
if result[entry.cmd] == nil {
|
||||
result[entry.cmd] = entry
|
||||
}
|
||||
|
|
|
@ -16,15 +16,17 @@
|
|||
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
@testable import Fishee
|
||||
|
||||
@Suite
|
||||
final class AlgebraTests {
|
||||
let historyItem = FishHistoryEntry(cmd: "cd Projects/Fishee/", when: 1727545693, paths: ["Projects/Fishee/"])
|
||||
let historyItem = FishHistoryEntry(
|
||||
cmd: "cd Projects/Fishee/", when: 1_727_545_693, paths: ["Projects/Fishee/"])
|
||||
|
||||
@Test func dateFromHistoryTest() {
|
||||
let gotDate = historyItem.getDate()
|
||||
#expect(gotDate == Date(timeIntervalSince1970: 1727545693))
|
||||
#expect(gotDate == Date(timeIntervalSince1970: 1_727_545_693))
|
||||
}
|
||||
|
||||
@Test func writeEntryTest() {
|
||||
|
|
|
@ -16,12 +16,13 @@
|
|||
|
||||
import Foundation
|
||||
import Testing
|
||||
@testable import Fishee
|
||||
|
||||
@testable import Fishee
|
||||
|
||||
@Suite(.serialized)
|
||||
final class FileHelpersTests {
|
||||
let filePath = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("myfile.txt")
|
||||
let filePath = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(
|
||||
"myfile.txt")
|
||||
|
||||
init() {
|
||||
try? "this is a test".write(
|
||||
|
@ -38,11 +39,12 @@ final class FileHelpersTests {
|
|||
@Test(arguments: [
|
||||
"$HOME/myfile.txt",
|
||||
"~/myfile.txt",
|
||||
"\(FileManager.default.homeDirectoryForCurrentUser.path)/myfile.txt"
|
||||
"\(FileManager.default.homeDirectoryForCurrentUser.path)/myfile.txt",
|
||||
])
|
||||
func getPathTest(testPath: String) {
|
||||
let path = getPath(testPath)
|
||||
let expected = URL(fileURLWithPath: "\(FileManager.default.homeDirectoryForCurrentUser.path)/myfile.txt")
|
||||
let expected = URL(
|
||||
fileURLWithPath: "\(FileManager.default.homeDirectoryForCurrentUser.path)/myfile.txt")
|
||||
#expect(path == expected)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,9 +14,9 @@
|
|||
// limitations under the License.
|
||||
//
|
||||
|
||||
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
@testable import Fishee
|
||||
|
||||
@Suite
|
||||
|
|
|
@ -16,15 +16,20 @@
|
|||
|
||||
import Foundation
|
||||
import Testing
|
||||
|
||||
@testable import Fishee
|
||||
|
||||
@Suite(.serialized)
|
||||
final class ParserTests {
|
||||
let fishHistoryFile = Bundle.module.path(forResource: "fish_history_test", ofType: "txt")
|
||||
let historyItem = FishHistoryEntry(cmd: "cd Projects/Fishee/", when: 1727545693, paths: ["Projects/Fishee/"])
|
||||
let historyItem2 = FishHistoryEntry(cmd: "swift package tools-version", when: 1727545709, paths: [])
|
||||
let filePathforWriteTest = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("myfile.txt")
|
||||
let filePathforFileBackupTest = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent("myfile_copy.txt")
|
||||
let historyItem = FishHistoryEntry(
|
||||
cmd: "cd Projects/Fishee/", when: 1_727_545_693, paths: ["Projects/Fishee/"])
|
||||
let historyItem2 = FishHistoryEntry(
|
||||
cmd: "swift package tools-version", when: 1_727_545_709, paths: [])
|
||||
let filePathforWriteTest = FileManager.default.homeDirectoryForCurrentUser.appendingPathComponent(
|
||||
"myfile.txt")
|
||||
let filePathforFileBackupTest = FileManager.default.homeDirectoryForCurrentUser
|
||||
.appendingPathComponent("myfile_copy.txt")
|
||||
|
||||
deinit {
|
||||
if FileManager.default.fileExists(atPath: filePathforWriteTest.path) {
|
||||
|
@ -88,7 +93,8 @@ final class ParserTests {
|
|||
}
|
||||
|
||||
@Test func mergeFishHistoryRemoveDuplicateTest() {
|
||||
let merged = mergeFishHistory([historyItem], [historyItem, historyItem2], removeDuplicates: true)
|
||||
let merged = mergeFishHistory(
|
||||
[historyItem], [historyItem, historyItem2], removeDuplicates: true)
|
||||
#expect(merged.count == 2)
|
||||
#expect(merged.contains(historyItem))
|
||||
#expect(merged.contains(historyItem2))
|
||||
|
|
Loading…
Reference in a new issue