Skip to content

Commit

Permalink
feat: add hexadecimal mode
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesrocket committed Aug 30, 2024
1 parent df3d782 commit a3737ec
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/assets.zig
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub const hex_chars = [16]u8{ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
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).",
.description = "Set symbol mode (binary, decimal, hexadecimal).",
.short_name = 'm',
.long_name = "mode",
.val = ValueT.ofType(Mode, .{ .name = "mode_val", .default_val = Mode.binary, .alias_child_type = "string" }),
Expand Down
15 changes: 10 additions & 5 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 = 2, decimal = 10 };
pub const Mode = enum(u8) { binary, decimal, hexadecimal };
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 @@ -206,8 +206,11 @@ fn printCells(core: *Core, handler: *Handler, rand: std.rand.Random) !void {
}
}

const number = @mod(rand.int(u8), @intFromEnum(handler.mode));
const int: u8 = @intCast(number);
const rand_int = switch (handler.mode) {
.binary => rand.int(u1),
.decimal => rand.uintLessThan(u8, 10),
.hexadecimal => rand.uintLessThan(u8, 16),
};

var color = @intFromEnum(core.color);

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

var buf: [2]u8 = undefined;
const slice: [:0]u8 = try std.fmt.bufPrintZ(&buf, "{d}", .{int});
const char = if (handler.mode == .hexadecimal) assets.hex_chars[rand_int] else rand_int;

var buf: [3]u8 = undefined;
const slice: [:0]u8 = if (handler.mode == .hexadecimal) try std.fmt.bufPrintZ(&buf, "{c}", .{char}) else try std.fmt.bufPrintZ(&buf, "{d}", .{char});

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

Expand Down

0 comments on commit a3737ec

Please sign in to comment.