Skip to content

Commit

Permalink
feat: add speed option
Browse files Browse the repository at this point in the history
  • Loading branch information
charlesrocket committed Sep 2, 2024
1 parent cee1908 commit 68d7ecd
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
12 changes: 12 additions & 0 deletions src/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const std = @import("std");
const cova = @import("cova");
const assets = @import("assets.zig");

const Speed = main.Speed;
const Color = main.Color;
const Style = main.Style;
const Mode = main.Mode;
Expand Down Expand Up @@ -69,6 +70,7 @@ pub const CommandT = cova.Command.Custom(.{
},
.val_config = .{
.custom_types = &.{
Speed,
Color,
Style,
Mode,
Expand Down Expand Up @@ -133,6 +135,16 @@ pub const setup_cmd: CommandT = .{
.default_val = 0,
}),
},
.{
.name = "speed",
.description = "Set speed (fast, slow).",
.long_name = "speed",
.val = ValueT.ofType(Speed, .{
.name = "speed_val",
.default_val = Speed.fast,
.alias_child_type = "string",
}),
},
.{
.name = "version",
.description = "Show the 'xtxf' version.",
Expand Down
13 changes: 11 additions & 2 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const VERSION = if (build_opt.gxt.dirty == null) HEAD_HASH ++ "-unverified" else

const FRAME = 39730492;

pub const Mode = enum(u8) { binary, decimal, hexadecimal, textual };
pub const Speed = enum { slow, fast };
pub const Mode = enum { 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 @@ -135,6 +136,7 @@ const Core = struct {
const Handler = struct {
mutex: Mutex = Mutex{},
halt: bool = true,
speed: Speed = .fast,
duration: u32 = 0,
pause: bool = false,
mode: Mode = Mode.binary,
Expand Down Expand Up @@ -250,7 +252,10 @@ fn printCells(core: *Core, handler: *Handler, rand: std.rand.Random) !void {

_ = tb.tb_present();
core.setRendering(false);
std.time.sleep(FRAME);
std.time.sleep(switch (handler.speed) {
.slow => FRAME * 2,
.fast => FRAME,
});
}
}

Expand Down Expand Up @@ -327,6 +332,10 @@ pub fn main() !void {
handler.duration = try time.val.getAs(u32);
}

if (opts.get("speed")) |speed| {
handler.speed = try speed.val.getAs(Speed);
}

if (opts.get("pulse")) |pulse| {
core.pulse = try pulse.val.getAs(bool);
}
Expand Down
7 changes: 7 additions & 0 deletions test/cli.zig
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ test "default" {
try std.testing.expectEqual(term, std.process.Child.Term{ .Exited = 0 });
}

test "speed" {
const argv = [_][]const u8{ exe_path, "--time=1", "-s=default", "--speed=slow" };
const term = try runner(argv);

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

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

0 comments on commit 68d7ecd

Please sign in to comment.