Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add comptime color demo #6

Merged
merged 1 commit into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 48 additions & 16 deletions examples/demo.zig
Original file line number Diff line number Diff line change
@@ -1,24 +1,56 @@
const std = @import("std");
const term_colors = @import("term-colors");

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"});
fn printColorCodeCells(comptime style: []const u8, id: comptime_int, comptime name: []const u8) void {
const colors = term_colors.comptime_colors;

// 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();
std.debug.print("{s}", .{style});

try stdout.print("Run `zig build test` to run the tests.\n", .{});
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) });
}

try bw.flush(); // don't forget to flush!
std.debug.print("{s} - ({d}) {s}\n", .{ colors.reset(), id, name });
}

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());
fn printColorCells(id: comptime_int, comptime name: []const u8) void {
const colors = term_colors.comptime_colors;

inline for (@typeInfo(term_colors.Color).Enum.fields) |c| {
if (id == 3) if (c.value > 40) break;
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}x) {s}\n", .{ colors.reset(), id, name });
}

fn comptimeColorsDemo() void {
std.debug.print("{s:-^64}\n", .{"foreground text colors and font styles"});

const colors = term_colors.comptime_colors;
printColorCodeCells("", 0, "normal");
printColorCodeCells(colors.bold(true), 1, "bold");
printColorCodeCells(colors.dim(true), 2, "dim");
printColorCodeCells(colors.italic(true), 3, "italic");
printColorCodeCells(colors.underline(true), 4, "underline");
printColorCodeCells(colors.blink(true), 5, "blink");
printColorCodeCells(colors.inverse(true), 7, "inverse");
printColorCodeCells(colors.hidden(true), 8, "hidden");
printColorCodeCells(colors.strikethrough(true), 9, "strikethrough");

std.debug.print("{s:-^64}\n", .{"normal and bright background colors"});

printColorCells(3, "normal");
printColorCells(9, "bright");
}

pub fn main() !void {
// sometimes zig build will print debug info before the program output
std.debug.print("\n", .{});

comptimeColorsDemo();
}
2 changes: 2 additions & 0 deletions src/lib.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub const Color = @import("enums.zig").Color;

pub const Colors = @import("colors.zig").Colors;
pub const comptime_colors = @import("colors.zig").comptime_colors;
pub const createColors = @import("colors.zig").createColors;
Expand Down