https://github.com/thomasfazzari1/zig-colors
Beautiful terminal colors for Zig
https://github.com/thomasfazzari1/zig-colors
ansi ansi-colors colors console cross-platform hex-colors rgb styling terminal terminal-colors tui zig
Last synced: 2 months ago
JSON representation
Beautiful terminal colors for Zig
- Host: GitHub
- URL: https://github.com/thomasfazzari1/zig-colors
- Owner: thomasfazzari1
- License: other
- Created: 2025-07-18T16:43:46.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-07-25T13:03:01.000Z (11 months ago)
- Last Synced: 2025-07-27T13:02:07.103Z (11 months ago)
- Topics: ansi, ansi-colors, colors, console, cross-platform, hex-colors, rgb, styling, terminal, terminal-colors, tui, zig
- Language: Zig
- Homepage: https://thomasfazzari1.github.io/zig-colors-docs/
- Size: 25.4 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/thomasfazzari1/zig-colors/actions)
[](https://ziglang.org)
[](https://opensource.org/licenses/MIT)
## Features
- **Full color support** - 16 colors, 256 colors, and RGB/Hex (16M colors)
- **Chainable API** - Intuitive and composable: `red.bold().underline()`
- **Zero dependencies** - Pure Zig implementation
- **Auto-detection** - Automatically detects terminal capabilities
- **Cross-platform** - Works on Windows, macOS, and Linux
- **Extensive styling** - Bold, dim, italic, underline, and more
## Installation
Add `zig-colors` to your `build.zig.zon`:
```zig
.{
.name = "my-project",
.version = "0.1.0",
.dependencies = .{
.@"zig-colors" = .{
.url = "https://github.com/thomasfazzari1/zig-colors/archive/refs/tags/v0.1.0.tar.gz",
.hash = "1220331bffdeca5488ee0a3a73f9c274f9856b63006d22b7fa2b810eb8a3fb9db867",
},
},
}
```
Then in your build.zig:
```zig
zigconst colors_dep = b.dependency("zig-colors", .{});
exe.root_module.addImport("zig-colors", colors_dep.module("zig-colors"));
```
## Quick Start
```zig
const std = @import("std");
const colors = @import("zig-colors");
pub fn main() !void {
// Initialize color support detection
colors.init();
defer colors.deinit();
// Simple colors
std.debug.print("{}\n", .{colors.red.call("Error!")});
std.debug.print("{}\n", .{colors.green.bold().call("Success!")});
// Background colors
std.debug.print("{}\n", .{colors.white.bgBlue().call("Info")});
// RGB/Hex colors (if terminal supports it)
const style = colors.Style{};
std.debug.print("{}\n", .{style.hex("#FF6B6B").bold().call("Custom color!")});
}
```
## Usage Examples
### Basic Colors
```zig
// Foreground colors
colors.red.call("Red text")
colors.green.call("Green text")
colors.blue.call("Blue text")
colors.yellow.call("Yellow text")
colors.magenta.call("Magenta text")
colors.cyan.call("Cyan text")
colors.white.call("White text")
colors.black.call("Black text")
// Bright variants
colors.brightRed.call("Bright red")
colors.brightGreen.call("Bright green")
// ... and more
```
### Text Styles
```zig
colors.bold.call("Bold text")
colors.dim.call("Dim text")
colors.italic.call("Italic text")
colors.underline.call("Underlined text")
```
### Styles Chaining
```zig
// Combine multiple styles
colors.red.bold().underline().call("Important error!")
colors.green.italic().call("Emphasized success")
colors.yellow.dim().call("Subtle warning")
// Complex combinations
colors.white.bgRed().bold().call("Alert!")
colors.brightBlue.bgBlack().underline().call("Highlighted")
```
### Background Colors
```zig
// Using background methods
colors.white.bgRed().call("White on red")
colors.black.bgGreen().call("Black on green")
// Using the bg namespace
colors.bg.yellow().black().call("Black on yellow")
colors.bg.blue().white().bold().call("Bold white on blue")
```
### RGB & Hex Colors
```zig
// RGB colors (requires truecolor terminal support)
const style = colors.Style{};
style.rgb(255, 107, 107).call("Custom RGB")
style.rgb(100, 200, 255).bold().call("Bold custom blue")
// Hex colors
style.hex("#FF6B6B").call("Hex color")
style.hex("#00CED1").underline().call("Underlined hex")
// RGB backgrounds
colors.white.bgRgb(50, 50, 50).call("White on dark gray")
colors.black.bgHex("#FFD700").call("Black on gold")
```
### Conditional Styling
```zig
// Only style if colors are supported
if (colors.isSupported()) {
std.debug.print("{}\n", .{colors.green.call("✓ Colors supported!")});
} else {
std.debug.print("Colors not supported\n", .{});
}
// Check color level
const level = colors.getLevel();
if (level == .truecolor) {
// Use RGB colors
const style = colors.Style{};
std.debug.print("{}\n", .{style.hex("#FF6B6B").call("Truecolor!")});
}
```
### Building a Logger
```zig
const Logger = struct {
const err = colors.red.bold();
const warn = colors.yellow;
const info = colors.blue;
const success = colors.green.bold();
pub fn logError(msg: []const u8) void {
std.debug.print("{} {s}\n", .{err.call("ERROR:"), msg});
}
pub fn logWarn(msg: []const u8) void {
std.debug.print("{} {s}\n", .{warn.call("WARN:"), msg});
}
pub fn logInfo(msg: []const u8) void {
std.debug.print("{} {s}\n", .{info.call("INFO:"), msg});
}
pub fn logSuccess(msg: []const u8) void {
std.debug.print("{} {s}\n", .{success.call("SUCCESS:"), msg});
}
};
```
## API Reference
For complete API documentation, visit the **[full documentation](https://thomasfazzari1.github.io/zig-colors-docs/)**.
## Testing
The library includes comprehensive tests:
```bash
# Run unit tests
zig build test
# Run visual demo
zig build demo
```
## License
This project is licensed under the Apache License 2.0 - See [LICENSE](LICENSE) for details.