Something went wrong. Try again.
various tools I have been using throughout the years
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229const command = @import("command.zig");const options = @import("options.zig");const pausable_timer = @import("pausable_timer.zig");const socket_path = @import("socket_path.zig");const status_socket = @import("status_socket.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); }, .status => |status_settings| { try printRemoteStatus(init, allocator, stdout, status_settings); }, }}
fn run( init: std.process.Init, allocator: std.mem.Allocator, out: *std.Io.Writer, settings: options.Settings,) !void { const path = try resolvePath(allocator, init.environ_map, settings.socket); defer allocator.free(path);
var server = status_socket.Server.init(init.io, path) catch |err| switch (err) { error.AlreadyRunning => fatal( "pracomer: another instance is already running on {s}\n", .{path}, ), error.NameTooLong => fatalPathTooLong(), else => |other| return other, }; defer server.deinit(init.io);
var poller = try command.Poller.init(server.handle()); defer poller.deinit();
if (poller.interactive()) { try out.print("\n", .{}); try out.flush(); }
var current = step.Step{}; // The event loop runs until the user quits; the timer alternates forever. while (true) { var timer = pausable_timer.PausableTimer.init(init.io);
var remaining_ns = current.length(settings);
while (remaining_ns > 0) : ({ remaining_ns = current.length(settings) -| timer.read(init.io); }) { if (poller.interactive()) { try out.print("\x1b[F\x1b[2K\r", .{}); try out.flush(); }
const status = step.Status{ .remaining_ns = remaining_ns, .paused = timer.paused, };
try printStatus(out, status);
switch (try poller.pollTimeout(std.time.ns_per_s)) { .none => {}, .status => server.serveOne(init.io, status), .command => |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) { if (remaining_ns == current.length(settings)) { current = step.Step{}; } }
timer.reset(init.io); }, }, } }
current = current.next(); if (settings.notifications) { if (current.length(settings) != 0) { notify(init.io, allocator, current.step_type.message()); } } }}
fn printRemoteStatus( init: std.process.Init, allocator: std.mem.Allocator, out: *std.Io.Writer, settings: options.StatusSettings,) !void { const path = try resolvePath(allocator, init.environ_map, settings.socket); defer allocator.free(path);
var buffer: [128]u8 = undefined; const message = status_socket.query(init.io, path, &buffer) catch |err| switch (err) { error.NotRunning => fatal("pracomer: no timer running on {s}\n", .{path}), error.NameTooLong => fatalPathTooLong(), else => fatal("pracomer: failed to query {s}: {any}\n", .{ path, err }), };
try out.print("{s}\n", .{message}); try out.flush();}
fn resolvePath( allocator: std.mem.Allocator, environ: *std.process.Environ.Map, override: ?[]const u8,) ![]u8 { return socket_path.resolve(allocator, environ, override) catch |err| switch (err) { error.NoSocketDir => fatal( "pracomer: neither XDG_RUNTIME_DIR nor TMPDIR is set, pass --socket <path>\n", .{}, ), error.EmptyPath => fatal("pracomer: --socket path must not be empty\n", .{}), error.NameTooLong => fatalPathTooLong(), else => |other| return other, };}
fn fatalPathTooLong() noreturn { fatal( "pracomer: socket path is longer than {d} bytes, pass a shorter --socket <path>\n", .{socket_path.path_len_max}, );}
fn fatal(comptime format: []const u8, args: anytype) noreturn { std.debug.print(format, args); std.process.exit(1);}
fn notify(io: std.Io, allocator: std.mem.Allocator, message: []const u8) void { if (builtin.target.os.tag == .macos) { const script = allocator.print( "display notification \"{s}\" with title \"pracomer\"", .{message}, ) catch return; defer allocator.free(script); const argv = [_][]const u8{ "osascript", "-e", script }; notifyCommand(io, &argv); } else { const argv = [_][]const u8{ "notify-send", "-u", "critical", message }; notifyCommand(io, &argv); }}
/// Notifications are best effort: a missing or failing tool must not stop the/// timer, so every failure is logged and dropped.fn notifyCommand(io: std.Io, argv: []const []const u8) void { var proc = std.process.spawn(io, .{ .argv = argv, }) catch |err| { std.log.debug("failed to spawn notification: {any}", .{err}); return; };
const term = proc.wait(io) catch |err| { std.log.debug("failed to wait for notification: {any}", .{err}); return; };
if (!term.success()) { std.log.debug("notification command failed: {any}", .{term}); }}
fn printStatus(out: *std.Io.Writer, status: step.Status) !void { var buffer: [step.Status.max_len]u8 = undefined; const message = try status.render(&buffer);
try out.print("{s}\n", .{message}); try out.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());}