Add comments to code

This commit is contained in:
Salar Rahmanian 2024-11-16 20:10:28 -08:00
parent 02f14b70bf
commit 4287ddfed9
5 changed files with 63 additions and 3 deletions

View file

@ -16,15 +16,18 @@
import Foundation
/// Data Structure/Schema representing an entry in a fish history file.
struct FishHistoryEntry: Equatable {
let cmd: String
let when: Int
let paths: [String]
/// Converts time to Date object
func getDate() -> Date {
Date(timeIntervalSince1970: TimeInterval(when))
}
/// Converts structure back to list of strings as expected in a fish history file.
func writeEntry() -> [String] {
var output: [String] = []

View file

@ -18,6 +18,12 @@
import Foundation
/// Gets the full file path to a given file.
///
/// - Parameters
/// - pathStr: The file path to a file.
///
/// - Returns: A Valid file URL or None if invalid.
func getPath(_ pathStr: String) -> URL? {
let userHomeDirectory = FileManager.default.homeDirectoryForCurrentUser.path
var filePath: String = pathStr

View file

@ -18,6 +18,18 @@
import Foundation
/// Make a backup of the fish history.
///
/// ```
///The copied files name will have _copy appended to the filename before the file extension.
///For example:
/// if the file was example.txt the copied file will be example_copy.txt
/// ```
///
/// - Parameters
/// - path: The file path to the fish history file.
///
/// - Returns: true if backup copy successful, false if not.
func backupHistory(_ path: String) -> Bool {
let fileManager = FileManager.default
@ -46,6 +58,14 @@ func backupHistory(_ path: String) -> Bool {
}
/// Write fish history to file.
///
/// - Parameters
/// - path: The file path to the fish history file.
/// - history: History list to write to file.
/// - backup: Should the current history file be backed up first boolean.
///
/// - Returns: true if writing to file copy successful, false if not.
func writeFishHistory(to path: String, history: [FishHistoryEntry], backup: Bool = true) -> Bool {
var output = ""
@ -75,6 +95,12 @@ func writeFishHistory(to path: String, history: [FishHistoryEntry], backup: Bool
}
}
/// Parse the fish history file.
///
/// - Parameters
/// - filePath: The file path to the fish history file.
///
/// - Returns: List of ``FishHistoryEntry`` entries from history file.
func parseFishHistory(from filePath: String) -> [FishHistoryEntry]? {
guard let fileContents = try? String(contentsOfFile: filePath) else {
print("Failed to open file.")
@ -113,6 +139,14 @@ func parseFishHistory(from filePath: String) -> [FishHistoryEntry]? {
return result.entries
}
/// Merge two given ``FishHistoryEntry`` lists into one list.
///
/// - Parameters
/// - left: The file path to the first fish history file.
/// - right: The file path to the second fish history file.
/// - 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] {
let merged = left + right
@ -127,5 +161,4 @@ func mergeFishHistory(_ left: [FishHistoryEntry], _ right: [FishHistoryEntry], r
} else {
return merged
}
}

0
Tests/FisheeTests.swift Normal file
View file

18
Tests/TestPlan.xctestplan Normal file
View file

@ -0,0 +1,18 @@
{
"configurations" : [
{
"id" : "1485A2F9-7AD1-4B2E-9F5B-CADD587FC5F6",
"name" : "Configuration 1",
"options" : {
}
}
],
"defaultOptions" : {
"testTimeoutsEnabled" : true
},
"testTargets" : [
],
"version" : 1
}