vibox 0.2.0

a jukebox you exit with :q - a cli music player with vi motions, ex commands, and tmux manners
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
//! Ex commands: everything typed after `:`.
//!
//! Names follow vim where vim has an opinion (`:q`, `:e`, `:w`, `:h`) and stay
//! spelled out where it does not (`:vol`, `:seek`, `:shuffle`).

use std::path::PathBuf;
use std::time::Duration;

use crate::app::{App, Repeat};
use crate::library::SortKey;

pub struct Parsed<'a> {
    pub name: &'a str,
    pub bang: bool,
    pub args: &'a str,
}

pub fn parse(line: &str) -> Option<Parsed<'_>> {
    let line = line.trim_start();
    if line.is_empty() {
        return None;
    }
    let (head, args) = line.split_once(char::is_whitespace).unwrap_or((line, ""));
    let bang = head.ends_with('!');
    Some(Parsed {
        name: head.trim_end_matches('!'),
        bang,
        args: args.trim(),
    })
}

pub fn run(app: &mut App, line: &str) {
    let Some(cmd) = parse(line) else {
        return;
    };

    // `:42` is a line number, same as vim.
    if let Ok(row) = cmd.name.parse::<usize>() {
        app.goto(row.saturating_sub(1));
        return;
    }

    match cmd.name {
        "q" | "qa" | "quit" | "qall" | "x" => {
            let all = cmd.name == "qa" || cmd.name == "qall";
            // Like vim: `:q` cares about the tab it is closing, `:qa` cares
            // about everything. Closing a clean tab is never blocked by changes
            // sitting in a different one.
            let closing_a_tab = !all && app.tabs.len() > 1;
            let blocked = if closing_a_tab {
                app.playlist_dirty
            } else {
                app.unsaved()
            };

            if blocked && !cmd.bang {
                app.error(
                    "no write since last change: `:ch` lists it, `:w` saves, `:q!` throws it away",
                );
            } else if all {
                app.quit = true;
            } else {
                app.close_tab();
            }
        }
        // `:e!` with pending renames is vim's "throw it away and reread".
        "e" | "edit" | "cd" if cmd.bang && cmd.args.is_empty() && app.edit.is_some() => {
            app.end_edit();
            app.info("renames discarded");
        }
        "e" | "edit" | "cd" => edit(app, cmd.args, cmd.bang),
        "pwd" => {
            let root = app.root.display().to_string();
            app.info(root);
        }
        "reload" | "scan" => match app.reload() {
            Ok(()) => {
                let n = app.tracks.len();
                app.info(format!("{n} tracks"));
            }
            Err(e) => app.error(format!("{e}")),
        },
        // One `:w` writes everything outstanding: the pending renames and the
        // playlist you reordered. With a name it saves the view as a new one.
        "w" | "write" | "save" => {
            if !cmd.args.is_empty() {
                app.save_playlist(cmd.args);
            } else {
                app.write_all();
            }
        }
        "vol" | "volume" => volume(app, cmd.args),
        "mute" => match app.audio.as_mut() {
            Some(audio) => {
                audio.toggle_mute();
                let muted = audio.muted();
                app.info(if muted { "muted" } else { "unmuted" });
            }
            None => app.error("no audio device: playback is disabled"),
        },
        "seek" => seek(app, cmd.args),
        "next" | "n" => app.advance(1, false),
        "prev" | "p" | "previous" => app.advance(-1, false),
        "play" => {
            if app.audio.as_ref().is_some_and(super::player::Audio::is_paused) {
                if let Some(audio) = app.audio.as_ref() {
                    audio.resume();
                }
            } else {
                app.play_cursor();
            }
        }
        "pause" => match app.audio.as_ref() {
            Some(audio) => audio.pause(),
            None => app.error("no audio device: playback is disabled"),
        },
        "stop" => app.stop(),
        "repeat" => {
            app.repeat = match cmd.args {
                "" | "toggle" => app.repeat.next(),
                "off" | "none" => Repeat::Off,
                "all" => Repeat::All,
                "one" | "track" => Repeat::One,
                other => {
                    app.error(format!("bad repeat mode `{other}` - use off, all or one"));
                    return;
                }
            };
            let name = app.repeat.name();
            app.info(format!("repeat {name}"));
        }
        "shuffle" => {
            app.shuffle = match cmd.args {
                "" | "toggle" => !app.shuffle,
                "on" | "1" => true,
                "off" | "0" => false,
                other => {
                    app.error(format!("bad shuffle value `{other}` - use on or off"));
                    return;
                }
            };
            let on = app.shuffle;
            app.info(if on { "shuffle on" } else { "shuffle off" });
        }
        "sort" => match SortKey::parse(cmd.args) {
            Some(key) => {
                app.set_sort(key);
                let name = key.name();
                app.info(format!("sorted by {name}"));
            }
            None => app.error("bad sort key - use path, title, artist, album or duration"),
        },
        "set" | "se" => set(app, cmd.args),
        "mkrc" => mkrc(app, cmd.bang),
        "matrix" => {
            app.matrix.on = !app.matrix.on;
            let on = app.matrix.on;
            app.info(if on { "wake up..." } else { "" });
        }
        "mkplaylist" | "mkpl" => app.create_playlist(cmd.args),
        "mkdir" => app.make_dir(cmd.args),
        "changes" | "ch" => app.show_changes = true,
        "h" | "help" => app.show_help = true,
        other => app.error(format!("not a vibox command: `{other}` - see :help")),
    }
}

/// `:set`, spelled the way vim spells it: `:set artist`, `:set noartist`,
/// `:set artist!` to flip it and `:set artist?` to ask.
fn set(app: &mut App, args: &str) {
    if args.is_empty() {
        app.info(list_options(app));
        return;
    }

    // `root=<path>` is the one option with a value: the library vibox opens
    // when it is started with no path.
    if let Some(rest) = args.strip_prefix("root=") {
        let path = expand(rest.trim());
        match app.open(&path) {
            Ok(()) => {
                let shown = path.display().to_string();
                app.music = Some(path);
                app.info(format!("library is {shown}, `:mkrc` keeps it"));
            }
            Err(e) => app.error(format!("{e}")),
        }
        return;
    }
    if args.trim() == "root?" {
        let shown = app
            .music
            .clone()
            .unwrap_or_else(|| app.root.clone())
            .display()
            .to_string();
        app.info(format!("root={shown}"));
        return;
    }

    let mut touched: Vec<String> = Vec::new();
    for word in args.split_whitespace() {
        let (name, action) = match word {
            w if w.ends_with('?') => (w.trim_end_matches('?'), '?'),
            w if w.ends_with('!') => (w.trim_end_matches('!'), '!'),
            w if w.starts_with("no") && option(app, &w[2..]).is_some() => (&w[2..], '0'),
            w => (w, '1'),
        };

        let Some(current) = option(app, name) else {
            let known = OPTIONS.join(", ");
            app.error(format!("unknown option `{name}` - try {known}"));
            return;
        };

        match action {
            '?' => {
                let no = if current { "" } else { "no" };
                app.info(format!("{no}{name}"));
                return;
            }
            '!' => set_option(app, name, !current),
            '0' => set_option(app, name, false),
            _ => set_option(app, name, true),
        }

        // Report back only what was touched. Listing every option on a toggle
        // buries the answer to the question that was actually asked.
        let now = option(app, name).unwrap_or(false);
        touched.push(format!("{}{name}", if now { "" } else { "no" }));
    }

    // An option only lasts the session until `:mkrc` writes it, so say so
    // every time rather than expecting anyone to remember.
    let mut line = format!("{} | `:mkrc` keeps these", touched.join(" "));
    if app.danger {
        line.push_str(" | measure twice, `:w` once");
    }
    app.info(line);
}

/// Where the last session is remembered. This is state, not configuration, so
/// it lives in the data dir and never touches the rc file the user edits.
fn state_path() -> Option<PathBuf> {
    Some(dirs::data_dir()?.join("vibox/state"))
}

/// Writes volume, shuffle, repeat and the options back out on quit, as ex
/// commands, so restoring them is just running them.
pub fn save_state(app: &App) {
    let Some(path) = state_path() else {
        return;
    };
    if let Some(dir) = path.parent()
        && std::fs::create_dir_all(dir).is_err()
    {
        return;
    }

    let volume = app.audio.as_ref().map_or(80, super::player::Audio::volume);
    let body = format!(
        "\" last session, overwritten on quit. edit viboxrc instead\nset {}\nvol {volume}\nshuffle {}\nrepeat {}\n",
        saved_options(app),
        if app.shuffle { "on" } else { "off" },
        app.repeat.name(),
    );
    let _ = std::fs::write(path, body);
}

/// Restores the last session. Runs before the rc file, so a line the user put
/// in `viboxrc` always wins over what they happened to leave behind.
pub fn load_state(app: &mut App) {
    let Some(path) = state_path() else {
        return;
    };
    let Ok(text) = std::fs::read_to_string(path) else {
        return;
    };
    for line in text.lines() {
        let line = line.trim();
        if !line.is_empty() && !line.starts_with('"') {
            run(app, line);
        }
    }
    app.msg = None;
}

/// `~/.config/vibox/viboxrc`, a file of ex commands run at startup.
pub fn rc_path() -> Option<PathBuf> {
    Some(dirs::config_dir()?.join("vibox/viboxrc"))
}

/// The library named by `set root=` in the rc file, read before any state
/// exists so startup can open it.
pub fn configured_music() -> Option<PathBuf> {
    let text = std::fs::read_to_string(rc_path()?).ok()?;
    text.lines()
        .map(str::trim)
        .filter_map(|line| line.trim_start_matches(':').strip_prefix("set root="))
        .next_back()
        .map(|value| expand(value.trim()))
}

/// Runs the rc file. Every line is an ex command without its `:`, and `"`
/// starts a comment, the way a vimrc reads.
pub fn load_rc(app: &mut App) {
    let Some(path) = rc_path() else {
        return;
    };
    let Ok(text) = std::fs::read_to_string(path) else {
        return;
    };
    for line in text.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('"') {
            continue;
        }
        run(app, line.trim_start_matches(':'));
    }
    app.msg = None;
}

/// `:mkrc`, after vim's `:mkvimrc`: writes the current options back out.
fn mkrc(app: &mut App, bang: bool) {
    let Some(path) = rc_path() else {
        app.error("cannot find a config directory to write to");
        return;
    };
    if path.exists() && !bang {
        let shown = path.display();
        app.error(format!("`{shown}` exists - `:mkrc!` overwrites it"));
        return;
    }

    let music = app
        .music
        .clone()
        .unwrap_or_else(|| app.root.clone())
        .display()
        .to_string();
    let body = format!(
        "\" written by :mkrc\nset root={music}\nset {}\n",
        saved_options(app)
    );
    let wrote = path
        .parent()
        .map_or(Ok(()), std::fs::create_dir_all)
        .and_then(|()| std::fs::write(&path, body));

    match wrote {
        Ok(()) => {
            let shown = path.display().to_string();
            app.info(format!("wrote {shown}"));
        }
        Err(e) => {
            let shown = path.display();
            app.error(format!("cannot write `{shown}`: {e}"));
        }
    }
}

/// Every `:set` option: the tag columns, plus the panes that can be turned off.
const OPTIONS: [&str; 6] = ["file", "title", "artist", "album", "lyrics", "danger"];

fn option(app: &App, name: &str) -> Option<bool> {
    match name {
        "lyrics" => Some(app.show_lyrics),
        "danger" => Some(app.danger),
        other => app.columns.get(other),
    }
}

fn set_option(app: &mut App, name: &str, on: bool) {
    if name == "danger" {
        app.danger = on;
    } else if name == "lyrics" {
        app.show_lyrics = on;
    } else {
        app.columns.set(name, on);
    }
}

/// The options written to disk. `danger` is deliberately absent: it must be
/// turned on for the session you want it in, or typed into the rc file by hand.
/// Persisting it silently is how it ends up on when nobody meant it to be.
fn saved_options(app: &App) -> String {
    OPTIONS
        .iter()
        .filter(|name| **name != "danger")
        .map(|name| {
            let on = option(app, name).unwrap_or(false);
            format!("{}{name}", if on { "" } else { "no" })
        })
        .collect::<Vec<_>>()
        .join(" ")
}

fn list_options(app: &App) -> String {
    OPTIONS
        .iter()
        .map(|name| {
            let on = option(app, name).unwrap_or(false);
            format!("{}{name}", if on { "" } else { "no" })
        })
        .collect::<Vec<_>>()
        .join(" ")
}

fn edit(app: &mut App, args: &str, _bang: bool) {
    let target = if args.is_empty() {
        app.root.clone()
    } else {
        expand(args)
    };
    match app.open(&target) {
        Ok(()) => {
            let n = app.tracks.len();
            let root = app.root.display().to_string();
            if n == 0 {
                app.error(format!("no audio files under `{root}`"));
            } else {
                app.info(format!("{root}: {n} tracks"));
            }
        }
        Err(e) => app.error(format!("{e}")),
    }
}

fn volume(app: &mut App, args: &str) {
    let Some(audio) = app.audio.as_mut() else {
        app.error("no audio device: playback is disabled");
        return;
    };
    if args.is_empty() {
        let v = audio.volume();
        app.info(format!("volume {v}%"));
        return;
    }
    match parse_delta(args) {
        Some(Delta::Relative(d)) => audio.nudge_volume(d as i32),
        Some(Delta::Absolute(v)) => audio.set_volume(v.clamp(0, 100) as u8),
        None => {
            app.error(format!("bad volume `{args}` - use `:vol 70`, `:vol +5`"));
            return;
        }
    }
    let v = app.audio.as_ref().map_or(0, super::player::Audio::volume);
    app.info(format!("volume {v}%"));
}

fn seek(app: &mut App, args: &str) {
    let Some(audio) = app.audio.as_ref() else {
        app.error("no audio device: playback is disabled");
        return;
    };
    if !audio.has_track() {
        app.error("nothing playing");
        return;
    }
    let result = match parse_seek(args) {
        Some(Delta::Relative(d)) => audio.seek_by(d),
        Some(Delta::Absolute(s)) => audio.seek(Duration::from_secs(s.max(0) as u64)),
        None => {
            app.error(format!(
                "bad position `{args}` - use `:seek 1:30`, `:seek +30`"
            ));
            return;
        }
    };
    if let Err(e) = result {
        app.error(format!("{e}"));
    }
}

#[derive(Debug, PartialEq, Eq)]
pub enum Delta {
    Absolute(i64),
    Relative(i64),
}

/// `70`, `+5`, `-5`.
pub fn parse_delta(s: &str) -> Option<Delta> {
    let s = s.trim();
    if let Some(rest) = s.strip_prefix('+') {
        return rest.parse().ok().map(Delta::Relative);
    }
    if let Some(rest) = s.strip_prefix('-') {
        return rest.parse::<i64>().ok().map(|n| Delta::Relative(-n));
    }
    s.parse().ok().map(Delta::Absolute)
}

/// Same as [`parse_delta`] but also accepts `m:ss` and plain seconds.
pub fn parse_seek(s: &str) -> Option<Delta> {
    let s = s.trim();
    if s.is_empty() {
        return None;
    }
    if let Some(d) = parse_delta(s) {
        return Some(d);
    }
    if let Some((m, sec)) = s.split_once(':') {
        let m: i64 = m.trim().parse().ok()?;
        let sec: i64 = sec.trim().parse().ok()?;
        if !(0..60).contains(&sec) {
            return None;
        }
        return Some(Delta::Absolute(m * 60 + sec));
    }
    s.parse().ok().map(Delta::Absolute)
}

/// Expands a leading `~`. Everything else is left to the filesystem.
fn expand(arg: &str) -> PathBuf {
    if let Some(rest) = arg.strip_prefix("~/")
        && let Some(home) = dirs::home_dir()
    {
        return home.join(rest);
    }
    PathBuf::from(arg)
}


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

    #[test]
    fn a_bang_is_split_off_the_command_name() {
        let cmd = parse("q!").unwrap();
        assert_eq!(cmd.name, "q");
        assert!(cmd.bang);
        assert_eq!(cmd.args, "");
    }

    #[test]
    fn arguments_keep_the_spaces_inside_them() {
        let cmd = parse("  w  late night mix  ").unwrap();
        assert_eq!(cmd.name, "w");
        assert_eq!(cmd.args, "late night mix");
    }

    #[test]
    fn an_empty_line_is_not_a_command() {
        assert!(parse("   ").is_none());
        assert!(parse("").is_none());
    }

    #[test]
    fn volume_takes_absolute_and_relative_values() {
        assert_eq!(parse_delta("70"), Some(Delta::Absolute(70)));
        assert_eq!(parse_delta("+5"), Some(Delta::Relative(5)));
        assert_eq!(parse_delta("-15"), Some(Delta::Relative(-15)));
        assert_eq!(parse_delta("loud"), None);
    }

    #[test]
    fn seek_accepts_clock_time_as_well_as_seconds() {
        assert_eq!(parse_seek("1:30"), Some(Delta::Absolute(90)));
        assert_eq!(parse_seek("0:07"), Some(Delta::Absolute(7)));
        assert_eq!(parse_seek("90"), Some(Delta::Absolute(90)));
        assert_eq!(parse_seek("+30"), Some(Delta::Relative(30)));
        assert_eq!(parse_seek("1:75"), None, "seconds past 59 are a typo");
        assert_eq!(parse_seek(""), None);
    }

    #[test]
    fn sort_keys_have_the_short_forms_a_vi_user_would_try() {
        assert_eq!(SortKey::parse("t"), Some(SortKey::Title));
        assert_eq!(SortKey::parse("al"), Some(SortKey::Album));
        assert_eq!(SortKey::parse("duration"), Some(SortKey::Duration));
        assert_eq!(SortKey::parse("nope"), None);
    }
}