Skip to content

Commit

Permalink
refactor: rewrite a lot of things (#2)
Browse files Browse the repository at this point in the history
* refactor: rewrite a lot of things

* add docs and demo app build step
* rewrite most of Colors functions
* add fg and bg function to ComptimeColors
* add more unit test
  • Loading branch information
Garfield550 authored Jan 19, 2024
1 parent f2b2146 commit be61878
Show file tree
Hide file tree
Showing 11 changed files with 386 additions and 329 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,19 @@ concurrency:

jobs:
build-and-test:
name: Build zig project and run tests
name: Build & test w/zig
runs-on: ubuntu-latest
strategy:
matrix:
version: ['master']

steps:
- uses: actions/checkout@v4

- name: Setup zig
uses: goto-bus-stop/setup-zig@v2
with:
version: ${{ matrix.version }}

- name: Lint
run: zig fmt --check .
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
# Zig
zig-cache/
zig-out/
docs/
40 changes: 31 additions & 9 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,48 @@ const std = @import("std");

pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});

const optimize = b.standardOptimizeOption(.{});

const lib = b.addStaticLibrary(.{
.name = "zigsgr",
// const lib = b.addStaticLibrary(.{
// .name = "term-colors",
// .root_source_file = .{ .path = "src/lib.zig" },
// .target = target,
// .optimize = optimize,
// });
// b.installArtifact(lib);

const lib_unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/lib.zig" },
.target = target,
.optimize = optimize,
});
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);

b.installArtifact(lib);
const lib_docs = b.addInstallDirectory(.{
.source_dir = lib_unit_tests.getEmittedDocs(),
.install_dir = .prefix,
.install_subdir = "../docs",
});
const gen_docs_step = b.step("docs", "Generate documentation");
gen_docs_step.dependOn(&lib_docs.step);

const lib_unit_tests = b.addTest(.{
const lib_module = b.createModule(.{
.root_source_file = .{ .path = "src/lib.zig" },
.target = target,
.optimize = optimize,
});

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_lib_unit_tests.step);
const demo_app = b.addExecutable(.{
.name = "demo",
.root_source_file = .{ .path = "examples/demo.zig" },
.target = target,
.optimize = optimize,
});
demo_app.root_module.addImport("term-colors", lib_module);
const run_demo_app = b.addRunArtifact(demo_app);
run_demo_app.step.dependOn(b.getInstallStep());
const run_demo_app_step = b.step("demo", "Run the demo app");
run_demo_app_step.dependOn(&run_demo_app.step);
}
24 changes: 24 additions & 0 deletions examples/demo.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const std = @import("std");

pub fn main() !void {
// Prints to stderr (it's a shortcut based on `std.io.getStdErr()`)
std.debug.print("All your {s} are belong to us.\n", .{"codebase"});

// stdout is for the actual output of your application, for example if you
// are implementing gzip, then only the compressed bytes should be sent to
// stdout, not any debugging messages.
const stdout_file = std.io.getStdOut().writer();
var bw = std.io.bufferedWriter(stdout_file);
const stdout = bw.writer();

try stdout.print("Run `zig build test` to run the tests.\n", .{});

try bw.flush(); // don't forget to flush!
}

test "simple test" {
var list = std.ArrayList(i32).init(std.testing.allocator);
defer list.deinit(); // try commenting this out and see if zig detects the memory leak!
try list.append(42);
try std.testing.expectEqual(@as(i32, 42), list.pop());
}
182 changes: 91 additions & 91 deletions src/colors.zig
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const std = @import("std");
const consts = @import("consts.zig");
const enums = @import("enums.zig");
const utils = @import("utils.zig");

const CSI = consts.CSI;
const COLOR_16_FORMAT_STRING = consts.COLOR_16_FORMAT_STRING;

const Config = std.io.tty.Config;
const Color = enums.Color;
Expand All @@ -11,127 +14,124 @@ pub const Colors = union(enum) {
no_color,
escape_codes,

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

pub fn print(conf: Colors, writer: anytype, color: ColorParams, comptime format: []const u8, args: anytype) !void {
pub fn color16(conf: Colors, writer: anytype, color: Color, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => try std.fmt.format(writer, format, args),
.no_color => return,
.escape_codes => {
try utils.color16(writer, color[0], color[1]);
try std.fmt.format(writer, format, args);
try writer.writeAll(utils.default(color[1]));
const color_number = @intFromEnum(color);
const color_code: u7 = switch (text) {
.foreground => color_number,
.background => color_number + 10,
};
try std.fmt.format(writer, COLOR_16_FORMAT_STRING, .{color_code});
},
};
}

pub fn default(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try writer.writeAll(utils.default(text)),
};
pub fn print(writer: anytype, color: ColorParams, comptime format: []const u8, args: anytype) !void {
try color16(writer, color[0], color[1]);
try std.fmt.format(writer, format, args);
try default(color[1]);
}

pub fn setColor(out_stream: anytype, color: Color, text: TextColor) !void {
return color16(out_stream, color, text);
}

// 16 colors
pub fn black(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .black, text),
};
pub fn black(writer: anytype, text: TextColor) !void {
return color16(writer, .black, text);
}
pub fn brightBlack(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .bright_black, text),
};
pub fn brightBlack(writer: anytype, text: TextColor) !void {
return color16(writer, .bright_black, text);
}
pub fn red(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .red, text),
};
pub fn red(writer: anytype, text: TextColor) !void {
return color16(writer, .red, text);
}
pub fn brightRed(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .bright_red, text),
};
pub fn brightRed(writer: anytype, text: TextColor) !void {
return color16(writer, .bright_red, text);
}
pub fn green(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .green, text),
};
pub fn green(writer: anytype, text: TextColor) !void {
return color16(writer, .green, text);
}
pub fn brightGreen(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .bright_green, text),
};
pub fn brightGreen(writer: anytype, text: TextColor) !void {
return color16(writer, .bright_green, text);
}
pub fn yellow(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .yellow, text),
};
pub fn yellow(writer: anytype, text: TextColor) !void {
return color16(writer, .yellow, text);
}
pub fn brightYellow(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .bright_yellow, text),
};
pub fn brightYellow(writer: anytype, text: TextColor) !void {
return color16(writer, .bright_yellow, text);
}
pub fn blue(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .blue, text),
};
pub fn blue(writer: anytype, text: TextColor) !void {
return color16(writer, .blue, text);
}
pub fn brightBlue(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .bright_blue, text),
};
pub fn brightBlue(writer: anytype, text: TextColor) !void {
return color16(writer, .bright_blue, text);
}
pub fn magenta(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .magenta, text),
};
pub fn magenta(writer: anytype, text: TextColor) !void {
return color16(writer, .magenta, text);
}
pub fn brightMagenta(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .bright_magenta, text),
};
pub fn brightMagenta(writer: anytype, text: TextColor) !void {
return color16(writer, .bright_magenta, text);
}
pub fn cyan(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .cyan, text),
};
pub fn cyan(writer: anytype, text: TextColor) !void {
return color16(writer, .cyan, text);
}
pub fn brightCyan(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .bright_cyan, text),
};
pub fn brightCyan(writer: anytype, text: TextColor) !void {
return color16(writer, .bright_cyan, text);
}
pub fn white(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .white, text),
pub fn white(writer: anytype, text: TextColor) !void {
return color16(writer, .white, text);
}
pub fn brightWhite(writer: anytype, text: TextColor) !void {
return color16(writer, .bright_white, text);
}
};

pub const ComptimeColors = struct {
pub inline fn default(comptime text: TextColor) []const u8 {
return switch (text) {
.foreground => CSI ++ "39m",
.background => CSI ++ "49m",
};
}
pub fn brightWhite(conf: Colors, writer: anytype, text: TextColor) !void {
nosuspend switch (conf) {
.no_color => return,
.escape_codes => try utils.color16(writer, .bright_white, text),

pub inline fn color16(comptime color: Color, comptime text: TextColor) []const u8 {
const color_number = @intFromEnum(color);
const color_code: u7 = switch (text) {
.foreground => color_number,
.background => color_number + 10,
};
return std.fmt.comptimePrint(COLOR_16_FORMAT_STRING, .{color_code});
}

pub inline fn print(comptime color: Color, comptime text: TextColor, comptime format: []const u8, args: anytype) []const u8 {
const color_str = color16(color, text);
const str: []const u8 = std.fmt.comptimePrint(format, args);
const default_str = default(text);
return std.fmt.comptimePrint("{s}{s}{s}", .{ color_str, str, default_str });
}

pub inline fn fg(comptime color: Color) []const u8 {
return color16(color, .foreground);
}

pub inline fn bg(comptime color: Color) []const u8 {
return color16(color, .background);
}
};

Expand Down
28 changes: 0 additions & 28 deletions src/comptime_colors.zig

This file was deleted.

5 changes: 2 additions & 3 deletions src/lib.zig
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
pub const Colors = @import("colors.zig").Colors;
pub const ComptimeColors = @import("colors.zig").ComptimeColors;
pub const createColors = @import("colors.zig").createColors;

pub const ComptimeColors = @import("comptime_colors.zig").ComptimeColors;

pub const utils = @import("utils.zig");

test {
_ = @import("tests/comptime_colors_test.zig");
_ = @import("tests/colors_test.zig");
_ = @import("tests/utils_test.zig");
}
Loading

0 comments on commit be61878

Please sign in to comment.