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 textual mode #5

Merged
merged 2 commits into from
Sep 2, 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
79 changes: 79 additions & 0 deletions src/assets.zig
Original file line number Diff line number Diff line change
@@ -1,4 +1,83 @@
pub const hex_chars = [16]u8{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
pub const tex_chars = [76]u21{
'#',
'~',
'|',
'¦',
':',
'.',
'"',
'=',
'+',
'-',
'_',
'<',
'>',
'*',
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'「',
'」',
'チ',
'ノ',
'ハ',
'ロ',
'ミ',
'ヒ',
'ー',
'ウ',
'ヲ',
'シ',
'ナ',
'イ',
'ト',
'ョ',
'モ',
'ヨ',
'ニ',
'フ',
'サ',
'ワ',
'ツ',
'ン',
'ヤ',
'オ',
'リ',
'ア',
'ホ',
'テ',
'マ',
'ケ',
'メ',
'レ',
'エ',
'カ',
'キ',
'ム',
'ユ',
'ラ',
'セ',
'ネ',
'ス',
'タ',
'ヌ',
'ヘ',
'T',
'X',
'x',
'V',
'v',
'z',
};

pub const help_message =
\\ ██ ████
\\ ░██ ░██░
Expand Down
2 changes: 1 addition & 1 deletion src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub const setup_cmd: CommandT = .{
},
.{
.name = "mode",
.description = "Set symbol mode (binary, decimal, hexadecimal).",
.description = "Set symbol mode (binary, decimal, hexadecimal, textual).",
.short_name = 'm',
.long_name = "mode",
.val = ValueT.ofType(Mode, .{ .name = "mode_val", .default_val = Mode.binary, .alias_child_type = "string" }),
Expand Down
14 changes: 11 additions & 3 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ const VERSION = if (build_opt.gxt.dirty == null) HEAD_HASH ++ "-unverified" else

const FRAME = 39730492;

pub const Mode = enum(u8) { binary, decimal, hexadecimal };
pub const Mode = enum(u8) { binary, decimal, hexadecimal, textual };
pub const Style = enum { default, columns, crypto, grid, blocks };
pub const Color = enum(u32) { default = tb.TB_DEFAULT, red = tb.TB_RED, green = tb.TB_GREEN, blue = tb.TB_BLUE, yellow = tb.TB_YELLOW, magenta = tb.TB_MAGENTA };

var sbuf: [2]u8 = undefined;
var mbuf: [3]u8 = undefined;
var lbuf: [4]u8 = undefined;

const Core = struct {
allocator: std.mem.Allocator,
mutex: Mutex = Mutex{},
Expand Down Expand Up @@ -210,6 +214,7 @@ fn printCells(core: *Core, handler: *Handler, rand: std.rand.Random) !void {
.binary => rand.int(u1),
.decimal => rand.uintLessThan(u8, 10),
.hexadecimal => rand.int(u4),
.textual => rand.uintLessThan(u8, 76),
};

var color = @intFromEnum(core.color);
Expand All @@ -229,8 +234,11 @@ fn printCells(core: *Core, handler: *Handler, rand: std.rand.Random) !void {
color = color | tb.TB_BOLD;
}

var buf: [3]u8 = undefined;
const char: [:0]u8 = if (handler.mode == .hexadecimal) try std.fmt.bufPrintZ(&buf, "{c}", .{assets.hex_chars[rand_int]}) else try std.fmt.bufPrintZ(&buf, "{d}", .{rand_int});
const char: [:0]u8 = switch (handler.mode) {
.binary, .decimal => try std.fmt.bufPrintZ(&sbuf, "{d}", .{rand_int}),
.hexadecimal => try std.fmt.bufPrintZ(&mbuf, "{c}", .{assets.hex_chars[rand_int]}),
.textual => try std.fmt.bufPrintZ(&lbuf, "{u}", .{assets.tex_chars[rand_int]}),
};

_ = tb.tb_print(@intCast(w), @intCast(h), @intCast(color), @intCast(core.bg), char);

Expand Down
7 changes: 7 additions & 0 deletions test/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ test "mode: hexadecimal" {
try std.testing.expectEqual(term, std.process.Child.Term{ .Exited = 0 });
}

test "mode: textual" {
const argv = [_][]const u8{ exe_path, "--time=1", "-m=textual" };
const term = try runner(argv);

try std.testing.expectEqual(term, std.process.Child.Term{ .Exited = 0 });
}

test "color: red" {
const argv = [_][]const u8{ exe_path, "--time=1", "-c=red" };
const term = try runner(argv);
Expand Down