Skip to content

Commit

Permalink
feat: add textual mode
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesrocket committed Sep 1, 2024
1 parent efc3aa9 commit 5466ef1
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 4 deletions.
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
11 changes: 8 additions & 3 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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 };

Expand Down Expand Up @@ -210,6 +210,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 +230,12 @@ 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});
var buf: [4]u8 = undefined;
const char: [:0]u8 = switch (handler.mode) {
.binary, .decimal => try std.fmt.bufPrintZ(&buf, "{d}", .{rand_int}),
.hexadecimal => try std.fmt.bufPrintZ(&buf, "{c}", .{assets.hex_chars[rand_int]}),
.textual => try std.fmt.bufPrintZ(&buf, "{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

0 comments on commit 5466ef1

Please sign in to comment.