Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
eonist committed Jan 15, 2025
1 parent 5736499 commit ee18426
Showing 1 changed file with 73 additions and 8 deletions.
81 changes: 73 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ FileSugar is a lightweight Swift package that provides an elegant API for file s

## Features

- Open, save, delete, and create files
- Simple API for working with files
- Open, save, delete, copy, move, and create files and directories
- Stream reading and writing support for handling large files efficiently
- Asynchronous file operations with `async`/`await`
- File path utilities for normalizing and expanding paths
- XML parsing capabilities on macOS
- Simple and consistent API for file system interactions

## Installation

Expand All @@ -27,14 +31,70 @@ You can install FileSugar using Swift Package Manager. Simply add the following
### Usage
Here are some examples of how to use FileSugar:
```swift
// Write to a file
FileModifier.write("~/Desktop/temp.txt".tildePath, "test")
// Write text to a file
let success = FileModifier.write("~/Desktop/temp.txt".tildePath, content: "Hello, World!")

// Check if a file exists
FileAsserter.exists("~/Desktop/temp.txt".tildePath) // Output: true
// Write data to a file
let data = Data([0x00, 0x01, 0x02])
let dataWritten = try FileModifier.write(path: "~/Desktop/data.bin".tildePath, data: data)

// Read the contents of a file
FileParser.content("~/Desktop/temp.txt".tildePath) // Output: test
// Read text content from a file
if let content = FileParser.content(filePath: "~/Desktop/temp.txt".tildePath) {
print(content)
}

// Read data from a file
if let data = FileParser.data(filePath: "~/Desktop/data.bin".tildePath) {
// Process data
}

// Asynchronously read content from a file
Task {
do {
let content = try await FileParser.readContentAsync(url: URL(fileURLWithPath: "~/Desktop/temp.txt".tildePath))
print(content)
} catch {
print("Error reading file: \(error)")
}
}

// Asynchronously write content to a file
Task {
do {
try await FileModifier.writeContentAsync(url: URL(fileURLWithPath: "~/Desktop/temp.txt".tildePath), content: "Async write")
} catch {
print("Error writing file: \(error)")
}
}

// Move a file
let moved = FileModifier.move("~/Desktop/source.txt".tildePath, toURL: "~/Desktop/destination.txt".tildePath)

// Copy a file
let copied = FileModifier.copy("~/Desktop/source.txt".tildePath, toURL: "~/Desktop/copy.txt".tildePath)

// Delete a file
let deleted = FileModifier.delete("~/Desktop/temp.txt".tildePath)

// Create a directory
let dirCreated = FileModifier.createDir(path: "~/Desktop/NewFolder".tildePath)

// Write data to a file at a specific index
let data = "Partial".data(using: .utf8)!
try FileStreamWriter.write(filePath: "~/Desktop/stream.txt".tildePath, data: data, index: 5)

// Read data from a file starting at a specific index
let readData = try FileStreamReader.read(filePath: "~/Desktop/stream.txt".tildePath, startIndex: 5, endIndex: 10)

// Normalize a file path
if let normalizedPath = FilePathModifier.normalize("~/Desktop/../Documents/file.txt") {
print(normalizedPath) // Outputs the absolute path
}

// Expand a relative file path
if let expandedPath = FilePathModifier.expand("file.txt", baseURL: "~/Desktop") {
print(expandedPath)
}
```

### Contributing
Expand All @@ -47,3 +107,8 @@ FileSugar is available under the MIT license. See the LICENSE file for more info
- Add test file in spm resource .bundle
- Move FileStreamer into it's own repo again?
- Add more examples to readme?

## Platforms

- iOS 17 or later
- macOS 14 or later

0 comments on commit ee18426

Please sign in to comment.