Skip to content

Commit

Permalink
feat: add comptime 16 color helpers and unit tests (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Garfield550 authored Jan 20, 2024
1 parent a944858 commit 181578a
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[![Build and test](https://github.com/BiscuitTin/zig-term-colors/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/BiscuitTin/zig-term-colors/actions/workflows/build-and-test.yml)

# Zig terminal colors

A simple library for working with terminal formatting and colors in Zig.
50 changes: 50 additions & 0 deletions src/colors.zig
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,56 @@ pub const comptime_colors = struct {
pub inline fn bg(comptime color: Color) []const u8 {
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 fn createColors(config: Config) Colors {
Expand Down
99 changes: 99 additions & 0 deletions src/tests/colors_test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,105 @@ test "comptime colors bg" {
}
}

test "comptime 16 colors" {
{
// normal black foreground text
const expected = "\x1b[30m";
const actual = comptime_colors.black(.foreground);
try testing.expectEqualStrings(expected, actual);
}
{
// bright black background text
const expected = "\x1b[100m";
const actual = comptime_colors.brightBlack(.background);
try testing.expectEqualStrings(expected, actual);
}
{
// normal red foreground text
const expected = "\x1b[31m";
const actual = comptime_colors.red(.foreground);
try testing.expectEqualStrings(expected, actual);
}
{
// bright red background text
const expected = "\x1b[101m";
const actual = comptime_colors.brightRed(.background);
try testing.expectEqualStrings(expected, actual);
}
{
// normal green foreground text
const expected = "\x1b[32m";
const actual = comptime_colors.green(.foreground);
try testing.expectEqualStrings(expected, actual);
}
{
// bright green background text
const expected = "\x1b[102m";
const actual = comptime_colors.brightGreen(.background);
try testing.expectEqualStrings(expected, actual);
}
{
// normal yellow foreground text
const expected = "\x1b[33m";
const actual = comptime_colors.yellow(.foreground);
try testing.expectEqualStrings(expected, actual);
}
{
// bright yellow background text
const expected = "\x1b[103m";
const actual = comptime_colors.brightYellow(.background);
try testing.expectEqualStrings(expected, actual);
}
{
// normal blue foreground text
const expected = "\x1b[34m";
const actual = comptime_colors.blue(.foreground);
try testing.expectEqualStrings(expected, actual);
}
{
// bright blue background text
const expected = "\x1b[104m";
const actual = comptime_colors.brightBlue(.background);
try testing.expectEqualStrings(expected, actual);
}
{
// normal magenta foreground text
const expected = "\x1b[35m";
const actual = comptime_colors.magenta(.foreground);
try testing.expectEqualStrings(expected, actual);
}
{
// bright magenta background text
const expected = "\x1b[105m";
const actual = comptime_colors.brightMagenta(.background);
try testing.expectEqualStrings(expected, actual);
}
{
// normal cyan foreground text
const expected = "\x1b[36m";
const actual = comptime_colors.cyan(.foreground);
try testing.expectEqualStrings(expected, actual);
}
{
// bright cyan background text
const expected = "\x1b[106m";
const actual = comptime_colors.brightCyan(.background);
try testing.expectEqualStrings(expected, actual);
}
{
// normal white foreground text
const expected = "\x1b[37m";
const actual = comptime_colors.white(.foreground);
try testing.expectEqualStrings(expected, actual);
}
{
// bright white background text
const expected = "\x1b[107m";
const actual = comptime_colors.brightWhite(.background);
try testing.expectEqualStrings(expected, actual);
}
}

test "create colors function" {
var buffer: [8]u8 = undefined;
var fbs = std.io.fixedBufferStream(&buffer);
Expand Down

0 comments on commit 181578a

Please sign in to comment.