const command = @import("command.zig"); const options = @import("options.zig"); const pausable_timer = @import("pausable_timer.zig"); const std = @import("std"); const builtin = @import("builtin"); const step = @import("step.zig"); pub fn main(init: std.process.Init) !void { const allocator = init.gpa; const program_and_args = try init.minimal.args.toSlice(init.arena.allocator()); const program = program_and_args[0]; const program_args = program_and_args[1..]; var stdout_buffer: [1024]u8 = undefined; var stdout_writer = std.Io.File.stdout().writer(init.io, &stdout_buffer); const stdout = &stdout_writer.interface; const opts = options.parseArgs(program_args) catch { try printUsage(allocator, stdout, program); std.process.exit(1); }; switch (opts) { .help => { try printUsage(allocator, stdout, program); }, .settings => |settings| { try run(init, allocator, stdout, settings); }, } } fn run(init: std.process.Init, allocator: std.mem.Allocator, out: *std.Io.Writer, settings: options.Settings) !void { var poller = try command.Poller.init(allocator); defer poller.deinit(); const status_file: ?std.Io.File = if (settings.file) |path| try std.Io.Dir.cwd().createFile(init.io, path, .{}) else null; defer if (status_file) |f| f.close(init.io); defer if (settings.file) |path| std.Io.Dir.cwd().deleteFile(init.io, path) catch |err| { std.log.debug("failed to delete status file: {any}", .{err}); }; if (poller.interactive()) { try out.print("\n", .{}); try out.flush(); } var current = step.Step{}; while (true) { var timer = pausable_timer.PausableTimer.init(init.io); var remaining = current.length(settings); while (remaining > 0) : ({ remaining = current.length(settings) -| timer.read(init.io); }) { if (poller.interactive()) { try out.print("\x1b[F\x1b[2K\r", .{}); try out.flush(); } try printStatus( allocator, out, status_file, init.io, current, remaining, ); if (try poller.pollTimeout(std.time.ns_per_s)) |command_| { switch (command_) { command.Command.Quit => { try out.print("\n", .{}); try out.flush(); return; }, command.Command.Pause => { timer.togglePause(init.io); }, command.Command.Reset => { if (timer.paused and remaining == current.length(settings)) { current = step.Step{}; } timer.reset(init.io); }, } } } current = current.next(); if (settings.notifications and current.length(settings) > 0) { notify(init.io, allocator, current.step_type.message()); } } } fn notify(io: std.Io, allocator: std.mem.Allocator, message: []const u8) void { if (builtin.os.tag == .macos) { const script = std.fmt.allocPrint(allocator, "display notification \"{s}\" with title \"pracomer\"", .{message}) catch return; defer allocator.free(script); const argv = [_][]const u8{ "osascript", "-e", script }; var proc = std.process.spawn(io, .{ .argv = &argv, }) catch return; _ = proc.wait(io) catch |err| { std.log.debug("failed to wait for process: {any}", .{err}); }; } else { const argv = [_][]const u8{ "notify-send", "-u", "critical", message }; var proc = std.process.spawn(io, .{ .argv = &argv, }) catch return; _ = proc.wait(io) catch |err| { std.log.debug("failed to wait for process: {any}", .{err}); }; } } fn printStatus( allocator: std.mem.Allocator, out: *std.Io.Writer, status_file: ?std.Io.File, io: std.Io, current: step.Step, remaining: u64, ) !void { const msg = try current.render(allocator, remaining); defer allocator.free(msg); try out.print("{s}\n", .{msg}); try out.flush(); if (status_file) |f| { var buf: [1024]u8 = undefined; var fw = f.writer(io, &buf); try fw.seekTo(0); try fw.interface.writeAll(msg); try fw.interface.writeAll("\n"); try fw.flush(); } } fn printUsage( allocator: std.mem.Allocator, out: *std.Io.Writer, program: []const u8, ) !void { const usage = try options.usage(allocator, program); defer allocator.free(usage); try out.print("{s}", .{usage}); try out.flush(); } test "main.zig" { std.testing.refAllDecls(@This()); }