tools

various tools I have been using throughout the years
Log | Files | Refs | README | LICENSE

step.zig (1458B)


      1 const options = @import("options.zig");
      2 const std = @import("std");
      3 
      4 pub const StepType = enum(u8) {
      5     task = 'T',
      6     task_break = 'B',
      7 
      8     pub fn message(self: StepType) []const u8 {
      9         return switch (self) {
     10             StepType.task => "work",
     11             StepType.task_break => "break",
     12         };
     13     }
     14 };
     15 
     16 pub const Step = struct {
     17     step_type: StepType = StepType.task,
     18 
     19     pub fn next(self: Step) Step {
     20         return switch (self.step_type) {
     21             StepType.task => Step{
     22                 .step_type = StepType.task_break,
     23             },
     24             StepType.task_break => Step{
     25                 .step_type = StepType.task,
     26             },
     27         };
     28     }
     29 
     30     pub fn length(self: Step, settings: options.Settings) u64 {
     31         const minutes: u64 = switch (self.step_type) {
     32             StepType.task => settings.task_length,
     33             StepType.task_break => settings.task_break,
     34         };
     35 
     36         return minutes * std.time.ns_per_min;
     37     }
     38 
     39     pub fn render(
     40         self: Step,
     41         allocator: std.mem.Allocator,
     42         countdown: u64,
     43     ) ![]u8 {
     44         _ = self;
     45         const total_secs = countdown / std.time.ns_per_s;
     46         const mins = total_secs / std.time.s_per_min;
     47         const secs = total_secs % std.time.s_per_min;
     48         return try std.fmt.allocPrint(
     49             allocator,
     50             "{:0>2}:{:0>2}",
     51             .{
     52                 mins,
     53                 secs,
     54             },
     55         );
     56     }
     57 };