Skip to content

Commit

Permalink
Update comments with LLM
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed Sep 23, 2023
1 parent 3aa5721 commit 91ec7dd
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 119 deletions.
10 changes: 5 additions & 5 deletions FileSugarExampleMacOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ class AppDelegate: NSObject, NSApplicationDelegate {
* Creates the view
*/
lazy var view: NSView = {
let contentRect = window.contentRect(forFrameRect: window.frame)/*size of win sans titlebar*/
let view: View = .init(frame: contentRect)
window.contentView = view
view.layer?.backgroundColor = NSColor.white.cgColor
return view
let contentRect = window.contentRect(forFrameRect: window.frame) // Get the size of the window without the title bar
let view: View = .init(frame: contentRect) // Create a new view with the same size as the window
window.contentView = view // Set the window's content view to the new view
view.layer?.backgroundColor = NSColor.white.cgColor // Set the background color of the view to white
return view // Return the new view
}()
func applicationDidFinishLaunching(_ aNotification: Notification) {
_ = view
Expand Down
2 changes: 2 additions & 0 deletions FileSugarExampleMacOS/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ extension View {
* Testing getting content from folder
*/
func test1() {
// Call the content method of the FileParser class with the expanded tilde path
let result = FileParser.content(dirPath: NSString(string: "~/Desktop/").expandingTildeInPath)
// Print the count of the result array
Swift.print("result.count: \(String(describing: result?.count))")
result?.forEach {
Swift.print("$0: \($0)") // prints all the files on the desktop folder
Expand Down
17 changes: 6 additions & 11 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
MIT License

Copyright (c) 2018 André J
(c) 2023 eonist

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
- The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
SOFTWARE.
27 changes: 14 additions & 13 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
// swift-tools-version:5.6
import PackageDescription

// Define the package with a name and a list of products and dependencies
let package = Package(
name: "FileSugar",
name: "FileSugar", // The name of the package
products: [
.library(
name: "FileSugar",
targets: ["FileSugar"])
.library( // Define a library product
name: "FileSugar", // The name of the library product
targets: ["FileSugar"]) // The targets that make up the product
],
dependencies: [
dependencies: [ // The list of dependencies for the package
],
targets: [
.target(
name: "FileSugar",
dependencies: []),
.testTarget(
name: "FileSugarTests",
dependencies: ["FileSugar"])
targets: [ // The list of targets for the package
.target( // Define a target
name: "FileSugar", // The name of the target
dependencies: []), // The dependencies of the target
.testTarget( // Define a test target
name: "FileSugarTests", // The name of the test target
dependencies: ["FileSugar"]) // The dependencies of the test target
]
)
)
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
[![Tests](https://github.com/eonist/FileSugar/actions/workflows/Tests.yml/badge.svg)](https://github.com/eonist/FileSugar/actions/workflows/Tests.yml)
[![codebeat badge](https://codebeat.co/badges/b4b79239-d1f9-4c9a-8c46-d6a1b9dcb559)](https://codebeat.co/projects/github-com-eonist-filesugar-master)

### Description
FileSugar is a Swift package that provides a simple API for working with files. It allows you to easily open, save, delete, and create files.

## Table of Contents
- [Description](#description)
- [Features](#features)
- [Installation](#installation)
- [Usage](#usage)

## Features

- Open, save, delete, and create files
Expand Down
10 changes: 8 additions & 2 deletions Sources/FileSugar/FileAsserter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ public final class FileAsserter {
* - Returns: A boolean value indicating whether the file or folder exists
*/
public static func exists(path: String) -> Bool {
FileManager().fileExists(atPath: path)
// Create a new instance of the FileManager class
FileManager()
// Check if a file exists at the given path
.fileExists(atPath: path)
}

/**
Expand All @@ -20,6 +23,9 @@ public final class FileAsserter {
* - Returns: A boolean value indicating whether the directory has files
*/
public static func hasContent(filePath: String) -> Bool {
FileParser.content(dirPath: filePath)?.isEmpty ?? false
// Call the content method of the FileParser class with the given directory path
FileParser.content(dirPath: filePath)?
// Check if the returned content is empty
.isEmpty ?? false
}
}
1 change: 0 additions & 1 deletion Sources/FileSugar/FileModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public final class FileModifier {
return false // return false if the file move operation failed
}
}

/**
* Copies a file to another location
* - Remark: Paths must be created with: URL(fileURLWithPath: directory) and then .path
Expand Down
2 changes: 0 additions & 2 deletions Sources/FileSugar/FileUtils.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#if os(OSX)
import Cocoa

/**
* A utility class for working with files on macOS
*/
Expand All @@ -14,7 +13,6 @@ class FileUtils {
static func showFileInFinder(_ filePath: String) {
// Expand the tilde in the file path to the user's home directory
let expandedFilePath: String = filePath.tildePath

// Use the shared NSWorkspace instance to select the file or folder in Finder
NSWorkspace.shared.selectFile(nil, inFileViewerRootedAtPath: expandedFilePath)
}
Expand Down
5 changes: 4 additions & 1 deletion Sources/FileSugar/common/String+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ extension String {
/**
* From user agnostic to absolute URL
*/
internal var tildePath: String { NSString(string: self).expandingTildeInPath }
internal var tildePath: String {
// Convert the string to an NSString and expand the tilde in the path
NSString(string: self).expandingTildeInPath
}
}
12 changes: 4 additions & 8 deletions Sources/FileSugar/path/FilePathAsserter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ public final class FilePathAsserter {
* - Returns: True if the path is absolute, false if it is relative
*/
public static func isAbsolute(_ path: String, pathSeperator: String = "/") -> Bool {
// Check if the path starts with the path separator
return path.hasPrefix(pathSeperator)
path.hasPrefix(pathSeperator) // Check if the path starts with the path separator
}

/**
Expand All @@ -18,10 +17,8 @@ public final class FilePathAsserter {
* - Returns: True if the path contains a relative path, false otherwise
*/
public static func isBacklash(_ path: String) -> Bool {
// Check if the path starts with "../"
return path.hasPrefix("../")
path.hasPrefix("../") // Check if the path starts with "../"
}

/**
* Tests if a path is a file path (i.e. starts with a path separator or "../")
* - Parameter path: The path to test
Expand All @@ -30,7 +27,7 @@ public final class FilePathAsserter {
*/
public static func isFilePath(_ path: String, pathSeperator: String = "/") -> Bool {
// Check if the path starts with the path separator or "../"
return path.hasPrefix(pathSeperator) || path.hasPrefix(".." + pathSeperator)
path.hasPrefix(pathSeperator) || path.hasPrefix(".." + pathSeperator)
}

/**
Expand All @@ -39,7 +36,6 @@ public final class FilePathAsserter {
* - Returns: True if the path is a tilde path, false otherwise
*/
public static func isTildePath(_ path: String) -> Bool {
// Check if the path starts with "~"
return path.hasPrefix("~")
path.hasPrefix("~") // Check if the path starts with "~"
}
}
38 changes: 17 additions & 21 deletions Sources/FileSugar/path/FilePathParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ public final class FilePathParser {
* let appDocPath = FilePathParser.appDocPath() // "/Users/James/Documents"
*/
public static func appDocPath() -> String? {
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
return paths.first
let paths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true) // Get an array of paths to the user's document directory
return paths.first // Return the first path in the array (which should be the document directory)
}

/**
* Parses a string path into a URL object
* - Parameter stringPath: The string path to parse
Expand All @@ -22,9 +21,8 @@ public final class FilePathParser {
* let url = FilePathParser.path("file:///Users/Me/Desktop/Doc.txt") // NSURL object
*/
public static func path(_ stringPath: String) -> URL? {
URL(string: stringPath)
URL(string: stringPath) // Create a URL from the given string path
}

/**
* Converts a URL object to a string path
* - Parameter url: The URL object to convert
Expand All @@ -33,9 +31,8 @@ public final class FilePathParser {
* let path = FilePathParser.path(NSURL("file:///Users/Me/Desktop/Doc.txt")) // "/Users/Me/Desktop/Doc.txt"
*/
public static func path(_ url: URL) -> String {
url.path
url.path // Get the path component of the URL as a string
}

/**
* Converts a URL object to a string path
* - Parameter path: The URL object to convert
Expand All @@ -50,9 +47,8 @@ public final class FilePathParser {
* Returns the path to the user's home directory.
*/
public static func userHomePath() -> String {
NSHomeDirectory()
NSHomeDirectory() // Get the path to the user's home directory
}

/**
* Returns the file name of the given URL.
*
Expand All @@ -68,9 +64,10 @@ public final class FilePathParser {
* ```
*/
public static func fileName(_ fileURL: URL, _ withExtension: Bool = true) -> String {
// If withExtension is true, return the last path component of the URL (including the file extension)
// Otherwise, return the last path component of the URL without the file extension
withExtension ? fileURL.absoluteURL.lastPathComponent : fileURL.absoluteURL.deletingPathExtension().lastPathComponent
}

/**
* Returns the file name of the file at the given path.
*
Expand All @@ -85,10 +82,9 @@ public final class FilePathParser {
* ```
*/
public static func fileName(path filePath: String, withExtension: Bool = true) -> String? {
guard let url: URL = path(filePath) else { return nil }
return fileName(url, withExtension)
}

guard let url: URL = path(filePath) else { return nil } // Get the URL from the file path
return fileName(url, withExtension) // Get the file name from the URL and return it
}
/**
* Returns the directory of the given file URL.
*
Expand All @@ -102,9 +98,10 @@ public final class FilePathParser {
* ```
*/
public static func directory(_ fileURL: URL) -> String {
fileURL.absoluteURL.deletingPathExtension().absoluteString
fileURL.absoluteURL // Get the absolute URL of the file URL
.deletingPathExtension() // Delete the file extension from the URL
.absoluteString // Convert the URL back to a string
}

/**
* Returns the path to the project's resource folder.
*
Expand All @@ -114,9 +111,8 @@ public final class FilePathParser {
* ```
*/
public static var resourcePath: String? {
Bundle.main.resourcePath
Bundle.main.resourcePath // Get the path to the main bundle's resource directory
}

/**
* Returns the file extension of the file at the given path.
*
Expand All @@ -129,7 +125,7 @@ public final class FilePathParser {
* ```
*/
public static func fileExtension(_ filePath: String) -> String {
NSString(string: filePath).pathExtension
NSString(string: filePath).pathExtension // Get the file extension from the file path as an NSString
}
}
/**
Expand All @@ -148,7 +144,7 @@ extension FilePathParser {
* let fileURL = "file:///Users/username/Documents/example.txt" let fileName = FilePathParser.fileName(fileURL: fileURL) // fileName == "example.txt"
*/
public static func fileName(fileURL: String, _ withExtension: Bool = true) -> String? {
guard let path = path(fileURL) else { return nil }
return fileName(path, withExtension)
guard let path = path(fileURL) else { return nil } // Get the path from the file URL
return fileName(path, withExtension) // Get the file name from the path and return it
}
}
Loading

0 comments on commit 91ec7dd

Please sign in to comment.