//! Stopwatch that counts elapsed time in `interval_ns` increments. Self- //! chains via tick tea.Cmds; call `start` to begin, `stop` to pause, `reset` to //! zero out. View renders elapsed nanoseconds in a human-readable form. const std = @import("std"); const tea = @import("blacktea"); const util = @import("util.zig"); const t = std.testing; const Allocator = std.mem.Allocator; const Writer = std.Io.Writer; const Stopwatch = @This(); interval_ns: u64 = default_interval_ns, elapsed_ns: u64 = 0, running: bool = false, model: tea.Model = .{ .vtable = &.{ .init = vtableInit, .update = vtableUpdate, .view = vtableView, }, }, const default_interval_ns: u64 = std.time.ns_per_s; var tick_marker: u8 = 0; fn tickTypeId() usize { return @intFromPtr(&tick_marker); } fn vtableInit(m: *tea.Model) ?tea.Cmd { const self: *Stopwatch = @fieldParentPtr("model", m); return self.start(m.gpa); } fn vtableUpdate(m: *tea.Model, msg: tea.Msg) ?tea.Cmd { const self: *Stopwatch = @fieldParentPtr("model", m); return self.update(m.gpa, msg); } fn vtableView(m: *tea.Model, writer: *Writer) Writer.Error!tea.View { const self: *Stopwatch = @fieldParentPtr("model", m); try self.view(writer); return .{}; } pub fn start(self: *Stopwatch, allocator: Allocator) ?tea.Cmd { if (self.running) return null; self.running = true; return self.scheduleTick(allocator); } pub fn stop(self: *Stopwatch) void { self.running = false; } pub fn toggle(self: *Stopwatch, allocator: Allocator) ?tea.Cmd { if (self.running) { self.stop(); return null; } return self.start(allocator); } pub fn reset(self: *Stopwatch) void { self.elapsed_ns = 0; } pub fn elapsed(self: *const Stopwatch) u64 { return self.elapsed_ns; } pub fn update(self: *Stopwatch, allocator: Allocator, msg: tea.Msg) ?tea.Cmd { switch (msg) { .custom => |c| { if (c.type_id != tickTypeId()) return null; if (!self.running) return null; self.elapsed_ns +|= self.interval_ns; return self.scheduleTick(allocator); }, else => return null, } } pub fn view(self: *const Stopwatch, writer: *Writer) Writer.Error!void { try util.formatDuration(writer, self.elapsed_ns); } fn scheduleTick(self: *Stopwatch, allocator: Allocator) ?tea.Cmd { return tea.Cmd.tick(allocator, self.interval_ns, .{ .custom = .{ .type_id = tickTypeId() }, }) catch { self.running = false; return null; }; } test "start sets running" { var sw: Stopwatch = .{}; var gpa_state: std.heap.DebugAllocator(.{}) = .init; defer _ = gpa_state.deinit(); const gpa = gpa_state.allocator(); const cmd = sw.start(gpa); try t.expect(sw.running); if (cmd) |c| if (c.free_ctx) |free| if (c.ctx) |ctx| free(gpa, ctx); } test "reset zeroes elapsed" { var sw: Stopwatch = .{ .elapsed_ns = 5_000_000_000 }; sw.reset(); try t.expectEqual(@as(u64, 0), sw.elapsed_ns); } test "view formats seconds" { var buf: [32]u8 = undefined; var w: Writer = .fixed(&buf); const sw: Stopwatch = .{ .elapsed_ns = 5 * std.time.ns_per_s }; try sw.view(&w); try t.expectEqualStrings("5s", w.buffered()); } test "view formats minutes" { var buf: [32]u8 = undefined; var w: Writer = .fixed(&buf); const sw: Stopwatch = .{ .elapsed_ns = 65 * std.time.ns_per_s }; try sw.view(&w); try t.expectEqualStrings("1m5s", w.buffered()); }