Skip to content

Commit

Permalink
feat: add runtime color demo (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
Garfield550 authored Jan 20, 2024
1 parent c28f293 commit 9a912b8
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 36 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
[![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)

<p align="center">
<img width="600" height="470" src="images/demo.png">
</p>

# Zig terminal colors

A simple library for working with terminal formatting and colors in Zig.
144 changes: 108 additions & 36 deletions examples/demo.zig
Original file line number Diff line number Diff line change
@@ -1,56 +1,128 @@
const std = @import("std");
const term_colors = @import("term-colors");

fn printColorCodeCells(comptime style: []const u8, id: comptime_int, comptime name: []const u8) void {
const colors = term_colors.comptime_colors;
const comptime_colors_demo = struct {
fn printColorCodeCells(comptime style: []const u8, id: comptime_int, comptime name: []const u8) void {
const colors = term_colors.comptime_colors;

std.debug.print("{s}", .{style});
std.debug.print("{s}", .{style});

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) });
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) });
}

std.debug.print("{s} - ({d}) {s}\n", .{ colors.reset(), id, name });
}

std.debug.print("{s} - ({d}) {s}\n", .{ colors.reset(), id, name });
}
fn printColorCells(id: comptime_int, comptime name: []const u8) void {
const colors = term_colors.comptime_colors;

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;

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) });
}

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 });
}

std.debug.print("{s} - ({d}x) {s}\n", .{ colors.reset(), id, name });
}
pub fn print() void {
std.debug.print("{s:-^64}\n", .{"foreground text colors and font styles"});

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");

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("\n{s:-^64}\n", .{"normal and bright background colors"});

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

printColorCells(3, "normal");
printColorCells(9, "bright");
}
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 writer.print(" {d} ", .{c.value});
try colors.default(writer, .foreground);
}

try colors.reset(writer);
try writer.print(" - ({d}) {s}\n", .{ id, name });
}

fn printColorCells(colors: term_colors.Colors, writer: anytype, id: comptime_int, name: []const u8) !void {
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);
try colors.bg(writer, color);
try writer.print(" ", .{});
try colors.default(writer, .background);
}

try colors.reset(writer);
try writer.print(" - ({d}x) {s}\n", .{ id, name });
}

pub fn print() !void {
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 = term_colors.createColors(tty_config);

try stderr.print("{s:-^64}\n", .{"foreground text colors and font styles"});

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

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

try printColorCells(colors, stderr, 3, "normal");
try printColorCells(colors, stderr, 9, "bright");

try bw.flush();
}
};

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

comptime_colors_demo.print();

std.debug.print("\n{s:=^64}\n\n", .{" Runtime "});

comptimeColorsDemo();
try runtime_colors_demo.print();

std.debug.print("\n", .{});
}
Binary file added images/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9a912b8

Please sign in to comment.