Skip to content

Commit

Permalink
refactor: remove all color-named functions (#9)
Browse files Browse the repository at this point in the history
* refactor: remove all color-named functions

* make setColor function params compatible with the zig standard library
* add setBackgroundColor function
* add some example comments
* update demo and banner images
  • Loading branch information
Garfield550 authored Jan 23, 2024
1 parent 593c610 commit 664b7fe
Show file tree
Hide file tree
Showing 8 changed files with 198 additions and 359 deletions.
14 changes: 14 additions & 0 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,17 @@ jobs:

- name: Build and test
run: zig build test

- name: Install kcov
run: |
sudo apt-get update
sudo apt-get install -y kcov
- name: Generate coverage reports
run: kcov $PWD/coverage ./zig-cache/o/*/test

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
with:
directory: ./coverage/
verbose: true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
# Zig
zig-cache/
zig-out/
coverage/
docs/
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

# Zig terminal colors

A simple library for working with terminal formatting and colors in Zig.
A simple library for ANSI terminal colors and styles in Zig.

<details>

Expand Down
11 changes: 5 additions & 6 deletions examples/demo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const comptime_colors_demo = struct {

inline for (@typeInfo(term_colors.Color).Enum.fields) |c| {
const color: term_colors.Color = @enumFromInt(c.value);
std.debug.print("{s} {d} {s}", .{ colors.fg(color), c.value, colors.default(.foreground) });
const str = colors.print(color, .foreground, " {d} ", .{c.value});
std.debug.print("{s}", .{str});
}

std.debug.print("{s} - ({d}) {s}\n", .{ colors.reset(), id, name });
Expand All @@ -23,7 +24,7 @@ const comptime_colors_demo = struct {
if (id == 9) if (c.value < 40) continue;

const color: term_colors.Color = @enumFromInt(c.value);
std.debug.print("{s} {s}", .{ colors.bg(color), colors.default(.background) });
std.debug.print("{s}{d:^8}{s}", .{ colors.bg(color), c.value, colors.default(.background) });
}

std.debug.print("{s} - ({d}x) {s}\n", .{ colors.reset(), id, name });
Expand Down Expand Up @@ -54,7 +55,7 @@ const runtime_colors_demo = struct {
fn printColorCodeCells(colors: term_colors.Colors, writer: anytype, id: u8, name: []const u8) !void {
inline for (@typeInfo(term_colors.Color).Enum.fields) |c| {
const color: term_colors.Color = @enumFromInt(c.value);
try colors.fg(writer, color);
try colors.setColor(writer, color);
try writer.print(" {d} ", .{c.value});
try colors.default(writer, .foreground);
}
Expand All @@ -69,9 +70,7 @@ const runtime_colors_demo = struct {
if (id == 9) if (c.value < 40) continue;

const color: term_colors.Color = @enumFromInt(c.value);
try colors.bg(writer, color);
try writer.print(" ", .{});
try colors.default(writer, .background);
try colors.print(writer, .{ color, .background }, "{d:^8}", .{c.value});
}

try colors.reset(writer);
Expand Down
Binary file modified images/banner-dark.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/banner-light.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
232 changes: 114 additions & 118 deletions src/colors.zig
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@ pub const Colors = union(enum) {
no_color,
escape_codes,

pub fn default(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => {
const str: []const u8 = switch (text) {
.foreground => CSI ++ "39m",
.background => CSI ++ "49m",
};
try writer.writeAll(str);
},
};
}

pub fn color16(conf: Colors, writer: anytype, color: Color, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
Expand All @@ -45,68 +32,65 @@ pub const Colors = union(enum) {
try default(conf, writer, color[1]);
}

pub fn setColor(conf: Colors, out_stream: anytype, color: Color, text: TextColor) !void {
return color16(conf, out_stream, color, text);
/// Example:
/// ```zig
/// // set text color to red
/// try colors.setColor(writer, .red);
/// ```
pub fn setColor(conf: Colors, out_stream: anytype, color: Color) !void {
return color16(conf, out_stream, color, .foreground);
}

/// Example:
/// ```zig
/// // set background color to red
/// try colors.setBackgroundColor(writer, .red);
/// ```
pub fn setBackgroundColor(conf: Colors, out_stream: anytype, color: Color) !void {
return color16(conf, out_stream, color, .background);
}

/// Example:
/// ```zig
/// // set text color to red
/// try colors.fg(writer, .red);
/// ```
pub fn fg(conf: Colors, writer: anytype, color: Color) !void {
return color16(conf, writer, color, .foreground);
}

/// Example:
/// ```zig
/// // set background color to red
/// try colors.bg(writer, .red);
/// ```
pub fn bg(conf: Colors, writer: anytype, color: Color) !void {
return color16(conf, writer, color, .background);
}

// 16 colors
pub fn black(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .black, text);
}
pub fn brightBlack(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .bright_black, text);
}
pub fn red(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .red, text);
}
pub fn brightRed(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .bright_red, text);
}
pub fn green(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .green, text);
}
pub fn brightGreen(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .bright_green, text);
}
pub fn yellow(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .yellow, text);
}
pub fn brightYellow(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .bright_yellow, text);
}
pub fn blue(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .blue, text);
}
pub fn brightBlue(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .bright_blue, text);
}
pub fn magenta(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .magenta, text);
}
pub fn brightMagenta(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .bright_magenta, text);
}
pub fn cyan(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .cyan, text);
}
pub fn brightCyan(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .bright_cyan, text);
}
pub fn white(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .white, text);
}
pub fn brightWhite(conf: Colors, writer: anytype, text: TextColor) !void {
return color16(conf, writer, .bright_white, text);
/// Example:
/// ```zig
/// // set text color to terminal default
/// try colors.default(writer, .foreground);
/// ```
pub fn default(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => {
const str: []const u8 = switch (text) {
.foreground => CSI ++ "39m",
.background => CSI ++ "49m",
};
try writer.writeAll(str);
},
};
}

/// Example:
/// ```zig
/// // reset all colors and styles
/// try colors.reset(writer);
/// ```
pub fn reset(conf: Colors, writer: anytype) !void {
nosuspend switch (conf) {
.no_color => return,
Expand All @@ -126,40 +110,81 @@ pub const Colors = union(enum) {
},
};
}

/// Example:
/// ```zig
/// // set bold font
/// try colors.bold(writer, true);
/// ```
pub fn bold(conf: Colors, writer: anytype, set: bool) !void {
return style(conf, writer, set, "1m", "22m");
}

/// Example:
/// ```zig
/// // set dim(faint) font
/// try colors.dim(writer, true);
/// ```
pub fn dim(conf: Colors, writer: anytype, set: bool) !void {
return style(conf, writer, set, "2m", "22m");
}

/// Example:
/// ```zig
/// // set italic font
/// try colors.italic(writer, true);
/// ```
pub fn italic(conf: Colors, writer: anytype, set: bool) !void {
return style(conf, writer, set, "3m", "23m");
}

/// Example:
/// ```zig
/// // add underline to text
/// try colors.underline(writer, true);
/// ```
pub fn underline(conf: Colors, writer: anytype, set: bool) !void {
return style(conf, writer, set, "4m", "24m");
}

/// Example:
/// ```zig
/// // blink the text
/// try colors.blink(writer, true);
/// ```
pub fn blink(conf: Colors, writer: anytype, set: bool) !void {
return style(conf, writer, set, "5m", "25m");
}

/// Example:
/// ```zig
/// // swap foreground and background colors
/// try colors.inverse(writer, true);
/// ```
pub fn inverse(conf: Colors, writer: anytype, set: bool) !void {
return style(conf, writer, set, "7m", "27m");
}

/// Example:
/// ```zig
/// // hide text
/// try colors.hidden(writer, true);
/// ```
pub fn hidden(conf: Colors, writer: anytype, set: bool) !void {
return style(conf, writer, set, "8m", "28m");
}

/// Example:
/// ```zig
/// // add strikethrough to text
/// try colors.strikethrough(writer, true);
/// ```
pub fn strikethrough(conf: Colors, writer: anytype, set: bool) !void {
return style(conf, writer, set, "9m", "29m");
}
};

pub const comptime_colors = struct {
pub inline fn default(comptime text: TextColor) []const u8 {
comptime return switch (text) {
.foreground => CSI ++ "39m",
.background => CSI ++ "49m",
};
}

pub inline fn color16(comptime color: Color, comptime text: TextColor) []const u8 {
comptime {
const color_number = @intFromEnum(color);
Expand Down Expand Up @@ -188,54 +213,11 @@ pub const comptime_colors = struct {
comptime return color16(color, .background);
}

// 16 colors
pub inline fn black(comptime text: TextColor) []const u8 {
comptime return color16(.black, text);
}
pub inline fn brightBlack(comptime text: TextColor) []const u8 {
comptime return color16(.bright_black, text);
}
pub inline fn red(comptime text: TextColor) []const u8 {
comptime return color16(.red, text);
}
pub inline fn brightRed(comptime text: TextColor) []const u8 {
comptime return color16(.bright_red, text);
}
pub inline fn green(comptime text: TextColor) []const u8 {
comptime return color16(.green, text);
}
pub inline fn brightGreen(comptime text: TextColor) []const u8 {
comptime return color16(.bright_green, text);
}
pub inline fn yellow(comptime text: TextColor) []const u8 {
comptime return color16(.yellow, text);
}
pub inline fn brightYellow(comptime text: TextColor) []const u8 {
comptime return color16(.bright_yellow, text);
}
pub inline fn blue(comptime text: TextColor) []const u8 {
comptime return color16(.blue, text);
}
pub inline fn brightBlue(comptime text: TextColor) []const u8 {
comptime return color16(.bright_blue, text);
}
pub inline fn magenta(comptime text: TextColor) []const u8 {
comptime return color16(.magenta, text);
}
pub inline fn brightMagenta(comptime text: TextColor) []const u8 {
comptime return color16(.bright_magenta, text);
}
pub inline fn cyan(comptime text: TextColor) []const u8 {
comptime return color16(.cyan, text);
}
pub inline fn brightCyan(comptime text: TextColor) []const u8 {
comptime return color16(.bright_cyan, text);
}
pub inline fn white(comptime text: TextColor) []const u8 {
comptime return color16(.white, text);
}
pub inline fn brightWhite(comptime text: TextColor) []const u8 {
comptime return color16(.bright_white, text);
pub inline fn default(comptime text: TextColor) []const u8 {
comptime return switch (text) {
.foreground => CSI ++ "39m",
.background => CSI ++ "49m",
};
}

pub inline fn reset() []const u8 {
Expand Down Expand Up @@ -272,6 +254,20 @@ pub const comptime_colors = struct {
}
};

/// Example:
/// ```zig
/// const stderr_file = std.io.getStdErr();
/// const stderr_writer = stderr_file.writer();
/// var bw = std.io.bufferedWriter(stderr_writer);
/// const stderr = bw.writer();
///
/// const tty_config = std.io.tty.detectConfig(stderr_file);
/// const colors = createColors(tty_config);
/// try colors.setColor(stderr, .red);
/// try stderr.print("This is a red text", .{});
///
/// try bw.flush();
/// ```
pub fn createColors(config: Config) Colors {
nosuspend switch (config) {
.no_color => return .no_color,
Expand Down
Loading

0 comments on commit 664b7fe

Please sign in to comment.