timerd-rs 0.4.0

A small foreground/background terminal timer for Linux
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
use std::path::PathBuf;

use clap::{Parser, Subcommand};
use clap_complete::Shell;

use crate::display::DisplaySize;

#[derive(Debug, Parser)]
#[command(
    name = "timer",
    version,
    about = "A small foreground/background terminal timer",
    args_conflicts_with_subcommands = true
)]
pub struct Cli {
    /// Countdown duration: 15, 15s, 5m, 1h30m, or 00:15:00
    #[arg(value_name = "DURATION")]
    pub duration: Option<String>,

    /// A short note shown with the timer
    #[arg(short = 'n', long, value_name = "TEXT")]
    pub note: Option<String>,

    /// Start detached from the terminal
    #[arg(short = 'd', long)]
    pub detach: bool,

    /// Foreground size: small (s/1), medium (m/2), large (l/3), or xlarge (xl/4)
    #[arg(
        short = 's',
        long,
        value_enum,
        value_name = "SIZE",
        conflicts_with = "detach"
    )]
    pub size: Option<DisplaySize>,

    /// Animated foreground theme; run `timer theme list` to see names
    #[arg(
        short = 't',
        long,
        value_name = "THEME",
        value_parser = crate::theme::ThemeNameParser,
        hide_possible_values = true,
        conflicts_with = "detach"
    )]
    pub theme: Option<String>,

    #[command(subcommand)]
    pub command: Option<Command>,
}

#[derive(Debug, Subcommand)]
pub enum Command {
    /// Start a stopwatch that counts up from zero
    #[command(visible_alias = "sw")]
    Stopwatch {
        /// A short note shown with the timer
        #[arg(short = 'n', long, value_name = "TEXT")]
        note: Option<String>,

        /// Start detached from the terminal
        #[arg(short = 'd', long)]
        detach: bool,

        /// Foreground size: small (s/1), medium (m/2), large (l/3), or xlarge (xl/4)
        #[arg(
            short = 's',
            long,
            value_enum,
            value_name = "SIZE",
            conflicts_with = "detach"
        )]
        size: Option<DisplaySize>,

        /// Animated foreground theme; run `timer theme list` to see names
        #[arg(
            short = 't',
            long,
            value_name = "THEME",
            value_parser = crate::theme::ThemeNameParser,
            hide_possible_values = true,
            conflicts_with = "detach"
        )]
        theme: Option<String>,
    },

    /// Start a countdown
    #[command(visible_alias = "cd")]
    Countdown {
        /// Countdown duration
        #[arg(value_name = "DURATION")]
        duration: String,

        /// A short note shown with the timer
        #[arg(short = 'n', long, value_name = "TEXT")]
        note: Option<String>,

        /// Start detached from the terminal
        #[arg(short = 'd', long)]
        detach: bool,

        /// Foreground size: small (s/1), medium (m/2), large (l/3), or xlarge (xl/4)
        #[arg(
            short = 's',
            long,
            value_enum,
            value_name = "SIZE",
            conflicts_with = "detach"
        )]
        size: Option<DisplaySize>,

        /// Animated foreground theme; run `timer theme list` to see names
        #[arg(
            short = 't',
            long,
            value_name = "THEME",
            value_parser = crate::theme::ThemeNameParser,
            hide_possible_values = true,
            conflicts_with = "detach"
        )]
        theme: Option<String>,
    },

    /// Schedule a one-time alarm at a local clock time
    #[command(name = "at", visible_alias = "alarm")]
    Alarm {
        /// Local time: 08:00, 08:00:30, or 2030-10-30 08:00
        #[arg(value_name = "TIME")]
        time: String,

        /// A short description shown with the alarm
        #[arg(short = 'n', long, value_name = "TEXT")]
        note: Option<String>,

        /// Start detached from the terminal
        #[arg(short = 'd', long)]
        detach: bool,

        /// Foreground size: small (s/1), medium (m/2), large (l/3), or xlarge (xl/4)
        #[arg(
            short = 's',
            long,
            value_enum,
            value_name = "SIZE",
            conflicts_with = "detach"
        )]
        size: Option<DisplaySize>,

        /// Animated foreground theme; run `timer theme list` to see names
        #[arg(
            short = 't',
            long,
            value_name = "THEME",
            value_parser = crate::theme::ThemeNameParser,
            hide_possible_values = true,
            conflicts_with = "detach"
        )]
        theme: Option<String>,
    },

    /// Attach to the latest detached timer
    #[command(visible_alias = "a")]
    Attach {
        /// Timer ID; defaults to the latest detached timer
        id: Option<u64>,

        /// Foreground size: small (s/1), medium (m/2), large (l/3), or xlarge (xl/4)
        #[arg(short = 's', long, value_enum, value_name = "SIZE")]
        size: Option<DisplaySize>,

        /// Animated foreground theme; run `timer theme list` to see names
        #[arg(
            short = 't',
            long,
            value_name = "THEME",
            value_parser = crate::theme::ThemeNameParser,
            hide_possible_values = true
        )]
        theme: Option<String>,
    },

    /// List all timers
    #[command(visible_alias = "ls")]
    List,

    /// Pause a running stopwatch or countdown
    #[command(visible_alias = "p")]
    Pause {
        /// Timer ID; defaults to the latest running timer
        id: Option<u64>,
    },

    /// Resume a paused timer
    #[command(visible_alias = "r")]
    Resume {
        /// Timer ID; defaults to the latest paused timer
        id: Option<u64>,
    },

    /// Stop a stopwatch or cancel a countdown/alarm
    #[command(visible_alias = "x")]
    Stop {
        /// Timer ID; defaults to the latest active timer
        id: Option<u64>,
    },

    /// Remove completed, stopped, and cancelled timers
    Clear,

    /// Inspect display configuration
    Config {
        #[command(subcommand)]
        command: ConfigCommand,
    },

    /// List, preview, and validate animated themes
    #[command(visible_alias = "themes")]
    Theme {
        #[command(subcommand)]
        command: ThemeCommand,
    },

    /// Generate shell completion code
    #[command(
        visible_alias = "completion",
        after_help = r#"Examples:
  # Fish: enable now
  timer completions fish | source

  # Fish: install permanently
  mkdir -p ~/.config/fish/completions
  timer completions fish > ~/.config/fish/completions/timer.fish

  # Bash: enable now
  source <(timer completions bash)

  # Zsh: enable now
  source <(timer completions zsh)

Run `timer completions <SHELL> > FILE` to save generated completion code."#
    )]
    Completions {
        /// Shell to generate completions for
        #[arg(value_enum)]
        shell: Shell,
    },

    /// Stop the background timer service
    Shutdown,

    #[command(name = "__daemon", hide = true)]
    Daemon,
}

#[derive(Debug, Subcommand)]
pub enum ConfigCommand {
    /// Print the configuration file path
    Path,

    /// Show the active configuration
    Show,
}

#[derive(Debug, Subcommand)]
pub enum ThemeCommand {
    /// List built-in and user-installed themes
    List,

    /// Preview a theme; press q or Esc to exit
    Preview {
        /// Theme name shown by `timer theme list`
        #[arg(
            value_parser = crate::theme::ThemeNameParser,
            hide_possible_values = true
        )]
        name: String,
    },

    /// Validate a theme file without installing it
    Check {
        /// Path to a theme TOML file
        #[arg(value_hint = clap::ValueHint::FilePath)]
        file: PathBuf,
    },

    /// Print the directory scanned for user themes
    Path,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parses_direct_countdown() {
        let cli = Cli::try_parse_from(["timer", "15m", "-d", "-n", "tea"]).unwrap();
        assert_eq!(cli.duration.as_deref(), Some("15m"));
        assert!(cli.detach);
        assert_eq!(cli.note.as_deref(), Some("tea"));
        assert_eq!(cli.size, None);
        assert_eq!(cli.theme, None);
        assert!(cli.command.is_none());

        let sized = Cli::try_parse_from(["timer", "15m", "-s", "xl", "-t", "sleepy-cat"]).unwrap();
        assert_eq!(sized.size, Some(DisplaySize::Xlarge));
        assert_eq!(sized.theme.as_deref(), Some("sleepy-cat"));

        let custom = Cli::try_parse_from(["timer", "15m", "-t", "my-own-theme"]).unwrap();
        assert_eq!(custom.theme.as_deref(), Some("my-own-theme"));
    }

    #[test]
    fn parses_readable_commands_and_aliases() {
        let full = Cli::try_parse_from(["timer", "attach", "3"]).unwrap();
        assert!(matches!(
            full.command,
            Some(Command::Attach {
                id: Some(3),
                size: None,
                theme: None
            })
        ));

        let short = Cli::try_parse_from(["timer", "a"]).unwrap();
        assert!(matches!(
            short.command,
            Some(Command::Attach {
                id: None,
                size: None,
                theme: None
            })
        ));

        let countdown =
            Cli::try_parse_from(["timer", "cd", "5m", "-n", "break", "-s", "3"]).unwrap();
        assert!(matches!(
            countdown.command,
            Some(Command::Countdown {
                duration,
                note: Some(note),
                detach: false,
                size: Some(DisplaySize::Large),
                theme: None,
            }) if duration == "5m" && note == "break"
        ));

        let alarm = Cli::try_parse_from(["timer", "at", "08:00", "-n", "breakfast", "-d"]).unwrap();
        assert!(matches!(
            alarm.command,
            Some(Command::Alarm {
                time,
                note: Some(note),
                detach: true,
                size: None,
                theme: None,
            }) if time == "08:00" && note == "breakfast"
        ));

        let alarm_alias = Cli::try_parse_from(["timer", "alarm", "08:00:30"]).unwrap();
        assert!(matches!(alarm_alias.command, Some(Command::Alarm { .. })));

        let completions = Cli::try_parse_from(["timer", "completions", "fish"]).unwrap();
        assert!(matches!(
            completions.command,
            Some(Command::Completions { shell: Shell::Fish })
        ));
    }

    #[test]
    fn parses_sizes_and_config_commands() {
        let stopwatch = Cli::try_parse_from(["timer", "sw", "-s", "m"]).unwrap();
        assert!(matches!(
            stopwatch.command,
            Some(Command::Stopwatch {
                size: Some(DisplaySize::Medium),
                theme: None,
                ..
            })
        ));

        let attach = Cli::try_parse_from([
            "timer",
            "a",
            "11",
            "--size",
            "xlarge",
            "--theme",
            "sleepy-cat",
        ])
        .unwrap();
        assert!(matches!(
            attach.command,
            Some(Command::Attach {
                id: Some(11),
                size: Some(DisplaySize::Xlarge),
                theme: Some(theme),
            }) if theme == "sleepy-cat"
        ));

        let theme = Cli::try_parse_from(["timer", "theme", "preview", "sleepy-cat"]).unwrap();
        assert!(matches!(
            theme.command,
            Some(Command::Theme {
                command: ThemeCommand::Preview { name }
            }) if name == "sleepy-cat"
        ));

        let check = Cli::try_parse_from(["timer", "themes", "check", "cat.toml"]).unwrap();
        assert!(matches!(
            check.command,
            Some(Command::Theme {
                command: ThemeCommand::Check { file }
            }) if file == std::path::Path::new("cat.toml")
        ));

        let config = Cli::try_parse_from(["timer", "config", "show"]).unwrap();
        assert!(matches!(
            config.command,
            Some(Command::Config {
                command: ConfigCommand::Show
            })
        ));

        assert!(Cli::try_parse_from(["timer", "sw", "--detach", "--size", "large"]).is_err());
        assert!(Cli::try_parse_from(["timer", "sw", "--detach", "--theme", "sleepy-cat"]).is_err());
    }

    #[test]
    fn completion_help_explains_how_to_enable_it() {
        let error = Cli::try_parse_from(["timer", "completions", "--help"]).unwrap_err();
        let help = error.to_string();

        assert!(help.contains("timer completions fish | source"));
        assert!(help.contains("timer completions fish > ~/.config/fish/completions/timer.fish"));
        assert!(help.contains("source <(timer completions bash)"));
        assert!(help.contains("source <(timer completions zsh)"));
    }
}