Skip to content

Swift library for building CLI tools on macOS — terminal colors, ANSI styling, table formatting, and structured output.

License

Notifications You must be signed in to change notification settings

ansilithic/swift-cli-core

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

swift-cli-core

Terminal styling, ANSI color output, table formatting, and structured messaging for Swift command-line tools. Used across Ansilithic tools.

Usage

Add to the Package.swift:

dependencies: [
    .package(url: "https://github.com/ansilithic/swift-cli-core.git", from: "1.0.0")
]

Then import in the target:

.executableTarget(name: "mytool", dependencies: [
    .product(name: "CLICore", package: "swift-cli-core")
])

Modules

Terminal

One Dark color palette with automatic TTY detection.

import CLICore

print(styled("success", .green))       // Colored in terminal, plain when piped
print(styled("bold red", .bold, .red))  // Multiple styles

let padded = "hello".padded(to: 20)    // ANSI-aware padding
let plain = colored.strippingANSI      // Strip escape sequences

Table

ANSI-aware table formatting with dynamic column widths.

let table = TableFormatter(columns: [
    .init("Name"), .init("Status"), .init("Branch")
], padding: 4)

let rows = [["myrepo", "clean", "main"], ["other", "dirty", "feature"]]
let widths = table.widths(for: rows)
table.printHeader(widths: widths)
for row in rows {
    table.printRow(row, widths: widths)
}

Output

Structured messaging with correct stream routing.

Output.info("Processing files...")      // stdout
Output.success("All done")             // stdout
Output.warning("Skipped 2 files")      // stderr
Output.error("File not found")         // stderr
Output.printSummary([
    styled("3 synced", .green),
    styled("1 skipped", .yellow)
])

ExitCode

Standard exit codes.

CLIExitCode.success.exit()  // 0
CLIExitCode.warning.exit()  // 1
CLIExitCode.error.exit()    // 2

Requirements

  • macOS 14+ (Sonoma)
  • Swift 6.0

License

MIT

About

Swift library for building CLI tools on macOS — terminal colors, ANSI styling, table formatting, and structured output.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors