tminus 0.3.0

A countdown timer for the terminal with animated text effects, styled after Omarchy's screensaver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
mod app;
mod cli;
mod clock;
mod config;
mod control;
mod effects;
mod font;
mod plan;
mod scene;
mod term;
mod theme;
mod util;

use std::{
    collections::HashMap,
    ffi::OsString,
    io::{IsTerminal, Write},
    process::ExitCode,
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
    time::{Duration, Instant},
};

use clap::{CommandFactory, Parser};
use crossterm::{
    event::{self, Event as TermEvent, KeyEventKind},
    terminal,
};

use crate::{
    app::{App, Event, Reply, Step},
    cli::{Cli, Command},
    clock::Timer,
    config::Config,
    control::Server,
    term::{Input, Term},
};

/// Input events handled per frame at most (a backstop against input floods).
const MAX_EVENTS_PER_FRAME: usize = 64;
/// Frame interval when nothing is animating (the gradient drift is slow).
const IDLE_FRAME: Duration = Duration::from_millis(50);
/// How often headless mode wakes up.
const HEADLESS_TICK: Duration = Duration::from_millis(100);

fn fail(msg: &str) -> ExitCode {
    report(msg);
    ExitCode::from(2)
}

/// Print an error, and also show it as a desktop notification when there's
/// no terminal at all: started from a keybinding or a bar, stderr goes nowhere.
fn report(msg: &str) {
    eprintln!("tminus: {msg}");
    if no_terminal() {
        let mut c = std::process::Command::new("notify-send");
        c.args(["tminus", msg]);
        run_detached(c);
    }
}

/// Neither stdin, stdout nor stderr is a terminal. A script that only
/// redirects stderr still has one, so it doesn't get notifications.
fn no_terminal() -> bool {
    !std::io::stdin().is_terminal() && !std::io::stdout().is_terminal() && !std::io::stderr().is_terminal()
}

/// `t- status`, `t- pause -n tea`, ...: a `t- ctl` command typed without
/// the `ctl` (0.1/0.2 had a top-level `status`). None of these words is a
/// duration, so this never catches a real timer.
fn ctl_hint(argv: &[OsString]) -> Option<String> {
    let word = argv.get(1)?.to_str()?;
    let ctl = Cli::command();
    ctl.find_subcommand("ctl")?.find_subcommand(word)?;
    let typed: Vec<String> = argv[1..].iter().map(|a| a.to_string_lossy().into_owned()).collect();
    Some(format!(
        "`{word}` is a `t- ctl` command: try `t- ctl {}`",
        typed.join(" ")
    ))
}

/// Parse the command line, with config file values as defaults underneath it.
fn parse_cli() -> Result<(Cli, Config), ExitCode> {
    let argv: Vec<OsString> = std::env::args_os().collect();
    if let Some(hint) = ctl_hint(&argv) {
        return Err(fail(&hint));
    }
    // Parse once without the config: handles --help, finds --config/--no-config,
    // and means any error in the second parse must come from the config.
    let cli = Cli::try_parse_from(&argv).unwrap_or_else(|e| {
        // A bad option from a keybinding would otherwise fail silently.
        if e.use_stderr() && no_terminal() {
            let msg = e.to_string();
            report(msg.lines().next().unwrap_or_default().trim_start_matches("error: "));
        }
        e.exit()
    });
    // Subcommands (status, ctl) talk to a running timer; option defaults don't apply.
    if cli.no_config || cli.command.is_some() {
        return Ok((cli, Config::default()));
    }
    let Some((path, required)) = cli
        .config
        .clone()
        .map(|p| (p, true))
        .or_else(|| config::default_path().map(|p| (p, false)))
    else {
        return Ok((cli, Config::default()));
    };
    let in_config = |e: &dyn std::fmt::Display| fail(&format!("in config {}: {e}", path.display()));
    let mut config = config::load(&path, &Cli::command(), required).map_err(|e| in_config(&e))?;
    if config.args.is_empty() {
        return Ok((cli, config));
    }
    let from_config = std::mem::take(&mut config.args);
    let merged = argv[..1]
        .iter()
        .cloned()
        .chain(from_config)
        .chain(argv[1..].iter().cloned());
    let cli =
        Cli::try_parse_from(merged).map_err(|e| in_config(&e.to_string().trim_start_matches("error: ").trim_end()))?;
    Ok((cli, config))
}

fn main() -> ExitCode {
    // `COMPLETE=<shell> t-` prints the shell's completion hook, and the hook
    // calls back in here on every Tab; either way this exits before the
    // config is read or anything else happens.
    clap_complete::CompleteEnv::with_factory(Cli::command)
        .bin(BIN)
        .complete();
    let (cli, config) = match parse_cli() {
        Ok(parsed) => parsed,
        Err(code) => return code,
    };

    if let Some(command) = &cli.command {
        return run_command(command);
    }

    let mut themes = theme::builtin();
    // Real colors of the installed Omarchy themes, then config themes on top.
    theme::merge(&mut themes, theme::from_omarchy());
    theme::merge(&mut themes, config.themes);
    let default_theme = theme::detect_omarchy(&themes).unwrap_or(0);

    if cli.list_effects {
        return print_out(effects::list(theme::color_output()).as_bytes());
    }
    if cli.list_themes {
        return print_out(theme::list(&themes, default_theme).as_bytes());
    }

    let find_theme = |name: &str| {
        theme::find(&themes, name).ok_or_else(|| {
            let names: Vec<&str> = themes.iter().map(|t| t.name.as_str()).collect();
            format!("unknown theme {name:?} (available: {})", names.join(", "))
        })
    };
    let theme = match cli.theme.as_deref().map(find_theme).transpose() {
        Ok(t) => t.unwrap_or(default_theme),
        Err(e) => return fail(&e),
    };

    let built = build_plan(&cli, &config.presets, &find_theme);
    let (mut steps, timer) = match built {
        Ok(b) => b,
        Err(e) => return fail(&e),
    };
    if cli.demo {
        // Only the first segment's clock is shown.
        steps.truncate(1);
    }

    // Intro and at-zero effects fall back to --effect when not given.
    let pool_or_default =
        |chosen: &[effects::Kind]| effects::pool(if chosen.is_empty() { &cli.effect } else { chosen });
    let pools = effects::Pools {
        intro: pool_or_default(&cli.intro_effect),
        replay: effects::pool(&cli.effect),
        zero: pool_or_default(&cli.zero_effect),
    };

    if let Some(f) = &cli.sound
        && !f.is_file()
    {
        return fail(&format!("sound file {} not found", f.display()));
    }

    let name = match timer_name(&cli, config.name.as_deref()) {
        Ok(n) => n,
        Err(e) => return fail(&e),
    };
    // Remote control is best effort for the TUI, but the point of --headless.
    let server = match name.as_deref().map(Server::start) {
        Some(Ok(s)) => Some(s),
        Some(Err(e)) if cli.headless => return fail(&format!("can't open the control socket: {e}")),
        None | Some(Err(_)) => None,
    };
    // A headless timer has no screen to show its generated name on.
    if cli.headless
        && cli.name.is_none()
        && let Some(n) = &name
        && std::io::stderr().is_terminal()
    {
        eprintln!("tminus: started as {n} (t- ctl list, t- ctl ... -n {n})");
    }

    let mut app = App::new(&cli, pools, &themes, theme, steps, timer);
    app.name.clone_from(&name);
    let result = if cli.headless && !cli.demo {
        run_headless(&cli, app, server)
    } else {
        let prev_hook = std::panic::take_hook();
        std::panic::set_hook(Box::new(move |info| {
            term::restore();
            prev_hook(info);
        }));
        let r = run(&cli, &mut app, server);
        term::restore();
        if let (Ok(_), Some((theme, effect))) = (&r, app.demo_choice()) {
            return print_out(format!("--theme {theme} --effect {effect}\n").as_bytes());
        }
        r
    };
    match result {
        Ok(true) => ExitCode::SUCCESS,
        Ok(false) => ExitCode::from(1),
        Err(e) => fail(&e.to_string()),
    }
}

/// The binary's name, as completions and the man page should call it.
const BIN: &str = "t-";

/// Subcommands: talk to a running timer, or print completions / the man page.
fn run_command(command: &Command) -> ExitCode {
    match command {
        Command::Completions { shell } => {
            // The same hook `COMPLETE=<shell> t-` prints, for installing as a file.
            let name = shell.to_string();
            let shells = clap_complete::env::Shells::builtins();
            let Some(completer) = shells.completer(&name) else {
                return fail(&format!("no completions for {name}"));
            };
            let mut buf = Vec::new();
            if let Err(e) = completer.write_registration("COMPLETE", BIN, BIN, BIN, &mut buf) {
                return fail(&e.to_string());
            }
            if name == "zsh" {
                // Installed as an autoloaded `_t-` file (in $fpath), the first
                // Tab only runs this file: complete right away, not just on the
                // next Tab. Sourcing it (`source <(COMPLETE=zsh t-)`) is unaffected.
                buf.extend_from_slice(
                    b"\nif [[ \"${funcstack[1]}\" == _t- ]]; then\n    _clap_dynamic_completer_t_ \"$@\"\nfi\n",
                );
            }
            print_out(&buf)
        }
        Command::Man => {
            let mut buf = Vec::new();
            match clap_mangen::Man::new(Cli::command().name(BIN)).render(&mut buf) {
                Ok(()) => print_out(&buf),
                Err(e) => fail(&e.to_string()),
            }
        }
        Command::Ctl { name, command } => match control::ctl(name.as_deref(), command) {
            Ok(()) => ExitCode::SUCCESS,
            Err(e) => {
                report(&e);
                ExitCode::from(1)
            }
        },
    }
}

/// Write generated output; a reader that stops early (`| head`) is fine.
fn print_out(bytes: &[u8]) -> ExitCode {
    match std::io::stdout().write_all(bytes) {
        Ok(()) => ExitCode::SUCCESS,
        Err(e) if e.kind() == std::io::ErrorKind::BrokenPipe => ExitCode::SUCCESS,
        Err(e) => fail(&e.to_string()),
    }
}

/// The name `t- ctl -n` will reach this timer by (none in --demo). Every
/// timer has its own: an explicit --name that's taken is refused rather than
/// shadowing the running timer. Unnamed ones are named after the preset
/// (`t- @pomodoro` → `pomodoro`), else a title given on the command line,
/// else the config's `name`, else at random (`eager-comet`); see `free_name`.
fn timer_name(cli: &Cli, config_name: Option<&str>) -> Result<Option<String>, String> {
    if cli.demo {
        return Ok(None);
    }
    if let Some(n) = &cli.name {
        return match control::running_pid(n) {
            Some(pid) => Err(format!(
                "a timer called `{n}` is already running (pid {pid}); stop it with \
                 `t- ctl stop -n {n}`, or pick another --name"
            )),
            None => Ok(Some(n.clone())),
        };
    }
    // `cli.title` may come from the config, where it would override the
    // config's `name`, so only a title from the command line counts.
    let cli_title = Cli::try_parse_from(std::env::args_os()).ok().and_then(|c| c.title);
    let bases = [first_preset(&cli.durations), cli_title.as_deref(), config_name];
    let base = bases.into_iter().flatten().find_map(control::slug);
    Ok(Some(control::free_name(&mut rand::rng(), base.as_deref())))
}

/// The first `@preset` among the sequence tokens, if any.
fn first_preset(tokens: &[String]) -> Option<&str> {
    tokens
        .iter()
        .find_map(|t| t.trim().strip_prefix('@'))
        .filter(|p| !p.is_empty())
}

/// The plan: one or more segments, and the timer for the first.
fn build_plan(
    cli: &Cli,
    presets: &HashMap<String, Vec<String>>,
    find_theme: &dyn Fn(&str) -> Result<usize, String>,
) -> Result<(Vec<Step>, Timer), String> {
    if let Some(u) = &cli.until {
        let timer = Timer::until(clock::parse_until(u)?);
        return Ok((
            vec![Step {
                dur: timer.total,
                label: None,
                theme: None,
            }],
            timer,
        ));
    }
    let demo_default = ["25m".to_string()];
    let durations = if cli.demo && cli.durations.is_empty() {
        &demo_default[..]
    } else {
        &cli.durations[..]
    };
    let steps = plan::parse(durations, presets)?
        .into_iter()
        .map(|s| {
            let theme = s.theme.as_deref().map(find_theme).transpose()?;
            Ok(Step {
                dur: s.dur,
                label: s.label,
                theme,
            })
        })
        .collect::<Result<Vec<_>, String>>()?;
    let first = steps.first().ok_or("nothing to count down")?.dur;
    Ok((steps, Timer::new(first)))
}

/// Quit cleanly when killed (restoring the terminal), not just on `q`.
fn watch_signals() -> std::io::Result<Arc<AtomicBool>> {
    let signalled = Arc::new(AtomicBool::new(false));
    for sig in [
        signal_hook::consts::SIGTERM,
        signal_hook::consts::SIGHUP,
        signal_hook::consts::SIGINT,
    ] {
        signal_hook::flag::register(sig, Arc::clone(&signalled))?;
    }
    Ok(signalled)
}

/// Answer remote-control requests. Returns true if asked to stop.
fn serve(server: &mut Option<Server>, app: &mut App, now: f64) -> bool {
    let Some(srv) = server else { return false };
    for mut req in srv.accept() {
        let reply = match req.line.as_str() {
            "status" => app.status_json(),
            "info" => app.info_line(app.cli.headless),
            "follow" => {
                let status = app.status_json();
                srv.follow(req.stream, &status);
                continue;
            }
            line => match app.remote(line, now) {
                Ok(Reply::Ok) => "ok".to_string(),
                Ok(Reply::Stop) => {
                    let _ = writeln!(req.stream, "ok");
                    return true;
                }
                Err(e) => format!("error: {e}"),
            },
        };
        let _ = writeln!(req.stream, "{reply}");
    }
    srv.publish(&app.status_json());
    false
}

/// Bell, notification, sound and command when a segment or the whole
/// countdown ends.
fn announce(cli: &Cli, event: &Event, term: Option<&mut Term>) -> std::io::Result<()> {
    if !cli.no_bell
        && let Some(term) = term
    {
        term.bell()?;
    }
    let (kind, done, next) = match event {
        Event::SegmentDone { done, next } => ("segment", done, next.as_deref()),
        Event::Completed { done } => ("done", done, None),
    };
    if cli.notify {
        let msg = match event {
            Event::SegmentDone { next, .. } => {
                format!("Time's up. Next: {}", next.as_deref().unwrap_or("next segment"))
            }
            Event::Completed { .. } => cli.title.clone().unwrap_or_else(|| "Time's up!".into()),
        };
        notify(&msg);
    }
    if let Some(file) = &cli.sound {
        play_sound(file);
    }
    if let Some(cmd) = &cli.exec {
        let env = [
            ("TMINUS_EVENT", kind),
            ("TMINUS_LABEL", done.as_deref().unwrap_or_default()),
            ("TMINUS_NEXT", next.unwrap_or_default()),
            ("TMINUS_TITLE", cli.title.as_deref().unwrap_or_default()),
        ];
        let mut c = std::process::Command::new("sh");
        c.args(["-c", cmd]).envs(env);
        run_detached(c);
    }
    Ok(())
}

/// Start `cmd` now, without letting it draw over the screen, and reap it on
/// a background thread. Starting it right away matters: with --exit we may
/// quit a moment later, and a started process outlives us.
fn run_detached(mut cmd: std::process::Command) -> bool {
    use std::process::Stdio;
    let Ok(mut child) = cmd
        .stdin(Stdio::null())
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .spawn()
    else {
        return false;
    };
    std::thread::spawn(move || child.wait());
    true
}

/// Play a sound file with the first audio player that's installed.
fn play_sound(file: &std::path::Path) {
    for player in ["pw-play", "paplay", "aplay", "afplay"] {
        let mut c = std::process::Command::new(player);
        c.arg(file);
        if run_detached(c) {
            return;
        }
    }
}

/// Runs the TUI. Returns Ok(true) if the countdown completed.
fn run(cli: &Cli, app: &mut App, mut server: Option<Server>) -> std::io::Result<bool> {
    let signalled = watch_signals()?;
    let mut term =
        Term::enter().map_err(|e| std::io::Error::new(e.kind(), format!("needs to run in a terminal ({e})")))?;
    app.term_bg = term.background();
    let mut rng = rand::rng();
    let epoch = Instant::now();
    let frame_dur = Duration::from_secs_f64(1.0 / f64::from(cli.fps));
    let (w, h) = terminal::size()?;
    app.resize(w, h);

    loop {
        let tick = Instant::now();
        let now = epoch.elapsed().as_secs_f64();
        if signalled.load(Ordering::Relaxed) {
            return Ok(app.completed());
        }

        // Input. crossterm's reader is only used when our own poll says there
        // is real input, since it spins forever on a hung-up terminal.
        match term.wait_input(Duration::ZERO)? {
            Input::HungUp => return Ok(app.completed()),
            Input::None => {}
            Input::Ready => {
                for _ in 0..MAX_EVENTS_PER_FRAME {
                    if !event::poll(Duration::ZERO)? {
                        break;
                    }
                    if let TermEvent::Key(k) = event::read()?
                        && k.kind != KeyEventKind::Release
                        && app.on_key(k.code, k.modifiers, now)
                    {
                        return Ok(app.completed());
                    }
                }
            }
        }
        // Resizes are picked up by checking the size, not via crossterm's reader.
        let (w, h) = terminal::size()?;
        if (i32::from(w), i32::from(h)) != (app.frame.w, app.frame.h) {
            app.resize(w, h);
            term.invalidate();
        }

        if serve(&mut server, app, now) {
            return Ok(app.completed());
        }
        if let Some(event) = app.tick(now)
            && !cli.demo
        {
            announce(cli, &event, Some(&mut term))?;
            if matches!(event, Event::Completed { .. }) && cli.exit {
                return Ok(true);
            }
        }

        app.draw(now, &mut rng);
        term.present(&app.frame)?;

        let budget = if app.animating(now) {
            frame_dur
        } else {
            frame_dur.max(IDLE_FRAME)
        };
        if let Some(wait) = budget.checked_sub(tick.elapsed())
            && term.wait_input(wait)? == Input::HungUp
        {
            return Ok(app.completed());
        }
    }
}

/// No screen: keep time, answer remote control, and announce the end.
fn run_headless(cli: &Cli, mut app: App, mut server: Option<Server>) -> std::io::Result<bool> {
    let signalled = watch_signals()?;
    let epoch = Instant::now();
    loop {
        let now = epoch.elapsed().as_secs_f64();
        if signalled.load(Ordering::Relaxed) || serve(&mut server, &mut app, now) {
            return Ok(app.completed());
        }
        if let Some(event) = app.tick(now) {
            announce(cli, &event, None)?;
            if matches!(event, Event::Completed { .. }) && cli.exit {
                return Ok(true);
            }
        }
        std::thread::sleep(HEADLESS_TICK);
    }
}

/// Fire-and-forget desktop notification.
fn notify(msg: &str) {
    let mut c = std::process::Command::new("notify-send");
    c.args(["-u", "critical", "tminus", msg]);
    run_detached(c);
}

#[cfg(test)]
mod tests {
    #[test]
    fn ctl_commands_without_ctl_get_a_hint() {
        let hint = |args: &[&str]| {
            let argv: Vec<std::ffi::OsString> = ["t-"].iter().chain(args).map(Into::into).collect();
            super::ctl_hint(&argv)
        };
        assert_eq!(
            hint(&["status", "--follow"]).as_deref(),
            Some("`status` is a `t- ctl` command: try `t- ctl status --follow`")
        );
        assert!(
            hint(&["pause", "-n", "tea"])
                .unwrap()
                .ends_with("`t- ctl pause -n tea`")
        );
        // Durations, options first, and other subcommands are left alone.
        assert_eq!(hint(&["5m"]), None);
        assert_eq!(hint(&["--no-config", "status"]), None);
        assert_eq!(hint(&["ctl", "status"]), None);
        assert_eq!(hint(&["help"]), None);
        assert_eq!(hint(&[]), None);
    }

    #[test]
    fn wide_chars_take_two_cells() {
        let g = crate::font::plain("a中b");
        assert_eq!(g.w, 4);
        assert_eq!(g.chars[2], crate::term::WIDE_TAIL);
        assert_eq!(crate::term::str_width("café 中"), 7);
    }

    #[test]
    fn fonts_are_rectangular() {
        use crate::font;
        for text in ["00:00", "1:23:45", "98:76", "12:34.56"] {
            for g in [font::shadow(text), font::block(text, 3), font::plain(text)] {
                assert_eq!(g.chars.len(), g.w * g.h);
                assert_eq!(g.owner.len(), g.w * g.h);
            }
        }
        // Digits are fixed-width so the clock doesn't wobble.
        assert_eq!(font::shadow("11:11").w, font::shadow("00:00").w);
    }
}