vibox 0.2.8

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
//! Playlists: reading m3u files, editing them in memory, writing them on `:w`.

use std::path::{Path, PathBuf};


use crate::library::{self, Track};

use super::*;

impl App {
    /// `p`: puts the yanked tracks into the open playlist, after the cursor.
    ///
    /// Only into a playlist: vibox does not add files to a folder.
    pub fn paste_into_playlist(&mut self) {
        if self.playlist_view.is_none() {
            self.error("`p` adds to a playlist: open one with `gt` and enter");
            return;
        }
        if self.yank.is_empty() {
            self.error("nothing yanked");
            return;
        }

        self.checkpoint();
        let mut rows = Vec::new();
        for path in self.yank.clone() {
            if let Some(row) = self.tracks.iter().position(|t| t.path == path) {
                rows.push(row);
            }
        }
        let n = rows.len();
        let at = (self.cur + 1).min(self.playlist_rows.len());
        self.playlist_rows.splice(at..at, rows);
        self.playlist_dirty = true;
        self.rebuild_view();
        self.goto(at);
        let name = self.playlist_view.clone().unwrap_or_default();
        self.info(format!("put {n} track{} into `{name}`, `:w` saves", plural(n)));
    }

    /// `dd`: drops rows from the open playlist. The files stay on disk.
    pub fn remove_from_playlist(&mut self) {
        if self.playlist_view.is_none() {
            self.error("`dd` removes a track from a playlist, never from disk");
            return;
        }
        if self.playlist_rows.is_empty() {
            return;
        }
        self.checkpoint();
        let (a, b) = self.selection_range();
        let b = b.min(self.playlist_rows.len() - 1);

        // `dd` is a cut, as in vim: what comes out goes in the register, so a
        // `p` further down the playlist puts it back there.
        let taken: Vec<usize> = self.playlist_rows.drain(a..=b).collect();
        self.yank = taken
            .iter()
            .filter_map(|&i| self.tracks.get(i).map(|t| t.path.clone()))
            .collect();

        self.playlist_dirty = true;
        self.exit_visual();
        self.cur = a;
        self.rebuild_view();
        let n = b - a + 1;
        let name = self.playlist_view.clone().unwrap_or_default();
        self.info(format!(
            "cut {n} track{} from `{name}`, `p` puts them back, `:w` saves",
            plural(n)
        ));
    }

    /// Creates an empty playlist, ready for `p` to fill.
    pub fn create_playlist(&mut self, name: &str) {
        let name = name.trim();
        if name.is_empty() {
            self.error("`:playlist` needs a name: `:playlist late night`");
            return;
        }
        if name.contains('/') {
            self.error("a playlist name cannot contain `/`");
            return;
        }
        let Some(dir) = self.playlists_in() else {
            self.error("cannot find a data directory to save playlists in");
            return;
        };

        let path = dir.join(format!("{name}.m3u"));
        if path.exists() {
            self.error(format!("playlist `{name}` already exists"));
            return;
        }

        match library::write_m3u(&path, &[]) {
            Ok(()) => {
                self.reload_playlists();
                self.tab = Tab::Playlists;
                if let Some(row) = self.playlists.iter().position(|(n, _)| n == name) {
                    self.pl_cur = row;
                }
                self.info(format!("`{name}` created, `p` puts tracks in it"));
            }
            Err(e) => self.error(format!("{e}")),
        }
    }

    /// `dd` on a playlist row marks the m3u for deletion, and a second `dd`
    /// takes the mark off again. Nothing happens on disk until `:w`.
    pub fn delete_playlist(&mut self) {
        let Some((name, path)) = self.playlists.get(self.pl_cur).cloned() else {
            return;
        };

        self.checkpoint();
        if let Some(at) = self.doomed.iter().position(|p| *p == path) {
            self.doomed.remove(at);
            self.info(format!("`{name}` kept"));
        } else {
            self.doomed.push(path);
            self.info(format!("`{name}` will be deleted on `:w`, `dd` undoes it"));
        }
    }

    pub fn is_doomed(&self, path: &Path) -> bool {
        self.doomed.iter().any(|p| p == path)
    }

    /// Deletes the playlists marked with `dd`. The tracks they named stay.
    fn apply_deletes(&mut self) -> usize {
        let mut gone = 0;
        for path in std::mem::take(&mut self.doomed) {
            let name = path
                .file_stem()
                .map(|s| s.to_string_lossy().into_owned())
                .unwrap_or_default();
            match std::fs::remove_file(&path) {
                Ok(()) => {
                    if self.playlist_view.as_deref() == Some(name.as_str()) {
                        self.playlist_view = None;
                        self.playlist_dirty = false;
                        self.rebuild_view();
                    }
                    // A tab you are not looking at must not keep showing a
                    // playlist whose file no longer exists.
                    for tab in &mut self.tabs {
                        if tab.playlist.as_deref() == Some(name.as_str()) {
                            tab.playlist = None;
                            tab.rows.clear();
                            tab.dirty = false;
                        }
                    }
                    gone += 1;
                }
                Err(e) => self.error(format!("cannot delete `{name}`: {e}")),
            }
        }
        if gone > 0 {
            self.reload_playlists();
        }
        gone
    }

    /// Rewrites every playlist that names a file we just renamed, so a rename
    /// never leaves a playlist pointing at a path that is gone.
    pub(super) fn repair_playlists(&mut self, renamed: &[(PathBuf, PathBuf)]) -> usize {
        let mut fixed = 0;
        for (_, path) in self.playlists.clone() {
            let Ok(paths) = library::read_m3u(&path) else {
                continue;
            };
            if !paths.iter().any(|p| renamed.iter().any(|(old, _)| old == p)) {
                continue;
            }

            let updated: Vec<PathBuf> = paths
                .into_iter()
                .map(|p| {
                    renamed
                        .iter()
                        .find(|(old, _)| *old == p)
                        .map_or(p, |(_, new)| new.clone())
                })
                .collect();
            let tracks: Vec<&Track> = updated
                .iter()
                .filter_map(|p| self.tracks.iter().find(|t| t.path == *p))
                .collect();
            if library::write_m3u(&path, &tracks).is_ok() {
                fixed += 1;
            }
        }
        fixed
    }

    /// A path as `root/...`, since every change is inside the library anyway.
    /// Quoted when it has a space in it, so the arrow stays readable.
    pub fn under_root(&self, path: &Path) -> String {
        let shown = match path.strip_prefix(&self.root) {
            Ok(rel) => format!("root/{}", rel.display()),
            Err(_) => path.display().to_string(),
        };
        if shown.contains(' ') {
            format!("'{shown}'")
        } else {
            shown
        }
    }

    /// What `:w` would do right now, one line each, for `:changes`.
    ///
    /// Every line starts with its verb, which is also what colours it.
    pub fn pending_changes(&self) -> Vec<String> {
        let mut out = Vec::new();
        // Every rename, wherever it was typed, reads the same way.
        let mut queued = self.renames.clone();
        if let Some(edit) = self.edit.as_ref() {
            let now = edit.buf.text();
            if now.trim().is_empty() || now == self.name_of(&edit.what) {
                queued.remove(&edit.what);
            } else {
                queued.insert(edit.what.clone(), now);
            }
        }
        for (what, name) in &queued {
            out.push(match what {
                Renaming::Track(idx) => {
                    let path = &self.tracks[*idx].path;
                    format!(
                        "rename  {}  ->  {}",
                        self.under_root(path),
                        self.under_root(&path.with_file_name(name))
                    )
                }
                Renaming::Folder(path) => format!(
                    "rename  folder {}  ->  {}",
                    self.under_root(path),
                    self.under_root(&path.with_file_name(name))
                ),
                Renaming::Playlist(old) => {
                    format!("rename  playlist `{old}`  ->  `{name}`")
                }
            });
        }
        if self.playlist_dirty
            && let Some(name) = self.playlist_view.as_ref()
        {
            let n = self.view.len();
            out.push(format!("save    playlist `{name}`, {n} tracks"));
        }
        for (name, rows) in self.dirty_tabs() {
            let n = rows.len();
            out.push(format!("save    playlist `{name}`, {n} tracks"));
        }
        for path in &self.doomed {
            let name = path.file_stem().unwrap_or_default().to_string_lossy();
            out.push(format!("delete  playlist `{name}`, its tracks stay"));
        }
        for (from, to) in &self.moves {
            out.push(format!(
                "move    {}  ->  {}",
                self.under_root(from),
                self.under_root(to)
            ));
        }
        for (from, to) in &self.copies {
            out.push(format!(
                "copy    {}  ->  {}",
                self.under_root(from),
                self.under_root(to)
            ));
        }
        for path in &self.doomed_files {
            out.push(format!("DELETE  {}", self.under_root(path)));
        }
        for dir in &self.doomed_dirs {
            out.push(format!(
                "DELETE  folder {}, and anything else left in it",
                self.under_root(dir)
            ));
        }
        out
    }

    /// Everything waiting to be written, for the marker on the statusline.
    pub fn unsaved(&self) -> bool {
        self.edit_dirty()
            || self.playlist_dirty
            || !self.doomed.is_empty()
            || !self.doomed_files.is_empty()
            || !self.moves.is_empty()
            || !self.copies.is_empty()
            || !self.doomed_dirs.is_empty()
            || self.dirty_tabs().next().is_some()
    }

    /// Playlists changed in a tab you are not looking at. `:w` writes
    /// everything, so `:changes` and the `[+]` marker have to see everything.
    fn dirty_tabs(&self) -> impl Iterator<Item = (String, Vec<usize>)> + '_ {
        self.tabs
            .iter()
            .enumerate()
            .filter(move |(i, tab)| *i != self.tab_idx && tab.dirty)
            .filter_map(|(_, tab)| tab.playlist.clone().map(|name| (name, tab.rows.clone())))
    }

    /// `:w`: renames the files you edited and saves the playlist you changed,
    /// in one press, and says what it did.
    pub fn write_all(&mut self) {
        // Checked before a single thing is written, so a clash leaves every
        // change pending instead of applying half of them.
        if let Some(problem) = self.move_problem() {
            self.error(problem);
            return;
        }

        self.undo_stack.clear();
        self.redo_stack.clear();
        let renames = self.edit_dirty();
        let playlist = self.playlist_dirty;
        let deletes = !self.doomed.is_empty();

        if renames {
            self.apply_edits();
            // A failed batch leaves the names pending; do not go on and write a
            // playlist as if everything were fine.
            if self.edit_dirty() {
                return;
            }
        }

        // Playlists changed in other tabs are written too: `:w` means all of it.
        for (name, rows) in self.dirty_tabs().collect::<Vec<_>>() {
            let tracks: Vec<&Track> = rows.iter().filter_map(|&i| self.tracks.get(i)).collect();
            if let Some(dir) = self.playlists_in()
                && library::write_m3u(&dir.join(format!("{name}.m3u")), &tracks).is_err()
            {
                self.error(format!("cannot save playlist `{name}`"));
                return;
            }
        }
        for (i, tab) in self.tabs.iter_mut().enumerate() {
            if i != self.tab_idx {
                tab.dirty = false;
            }
        }

        if playlist {
            let renamed = self.msg.take();
            self.save_open_playlist();
            if let (Some((before, false)), Some((after, false))) = (renamed, self.msg.clone()) {
                self.info(format!("{before}, {after}"));
            }
        } else if !renames && !deletes {
            self.save_open_playlist();
        }

        let (moved, copied, removed) = self.apply_file_ops();
        if moved + copied + removed > 0 {
            let before = self.msg.take();
            let mut parts = Vec::new();
            if moved > 0 {
                parts.push(format!("moved {moved} file{}", plural(moved)));
            }
            if copied > 0 {
                parts.push(format!("copied {copied} file{}", plural(copied)));
            }
            if removed > 0 {
                parts.push(format!("deleted {removed} file{}", plural(removed)));
            }
            let line = parts.join(", ");
            match before {
                Some((text, false)) => self.info(format!("{text}, {line}")),
                _ => self.info(line),
            }
        }

        if deletes {
            let gone = self.apply_deletes();
            if gone > 0 {
                let before = self.msg.take();
                let line = format!("deleted {gone} playlist{}", plural(gone));
                match before {
                    Some((text, false)) => self.info(format!("{text}, {line}")),
                    _ => self.info(line),
                }
            }
        }
    }

    /// Writes the open playlist back over its own file.
    pub fn save_open_playlist(&mut self) {
        let Some(name) = self.playlist_view.clone() else {
            self.error("`:w` needs a name: `:w late night`");
            return;
        };
        self.save_playlist(&name);
        self.playlist_dirty = false;
    }

    /// Where saved playlists live. State, not configuration, so it sits under
    /// the data dir next to the lyrics cache.
    /// Where this instance keeps playlists. A field rather than a constant so
    /// a test can point it at a scratch directory instead of writing into the
    /// playlists someone actually listens to.
    pub(super) fn playlists_in(&self) -> Option<PathBuf> {
        self.playlists_dir.clone().or_else(Self::playlist_dir)
    }

    pub fn playlist_dir() -> Option<PathBuf> {
        Some(dirs::data_dir()?.join("vibox/playlists"))
    }

    /// Rereads the playlist directory. Cheap: it lists names, it does not open
    /// the files.
    pub fn reload_playlists(&mut self) {
        let Some(dir) = self.playlists_in() else {
            return;
        };
        let mut found: Vec<(String, PathBuf)> = std::fs::read_dir(dir)
            .into_iter()
            .flatten()
            .flatten()
            .map(|entry| entry.path())
            .filter(|path| library::is_playlist(path))
            .filter_map(|path| {
                let name = path.file_stem()?.to_string_lossy().into_owned();
                Some((name, path))
            })
            .collect();
        found.sort();
        self.playlists = found;
        self.pl_cur = self.pl_cur.min(self.playlists.len().saturating_sub(1));
    }

    pub fn move_playlist(&mut self, delta: isize) {
        if self.playlists.is_empty() {
            return;
        }
        let last = self.playlists.len() as isize - 1;
        self.pl_cur = (self.pl_cur as isize + delta).clamp(0, last) as usize;
        let h = self.folder_h.max(1);
        if self.pl_cur < self.pl_top {
            self.pl_top = self.pl_cur;
        }
        if self.pl_cur >= self.pl_top + h {
            self.pl_top = self.pl_cur + 1 - h;
        }
    }

    /// Shows the playlist under the cursor. The library, and so the folders
    /// tab, is left exactly as it was: a playlist is a view, not a new root.
    pub fn open_playlist(&mut self) {
        let Some((name, path)) = self.playlists.get(self.pl_cur).cloned() else {
            self.error("no playlists yet: `:w <name>` saves what you are looking at");
            return;
        };
        if self.jump_to_open(Some(&name), 0) {
            self.info(format!("`{name}` is already open"));
            return;
        }

        let wanted = match library::read_m3u(&path) {
            Ok(paths) => paths,
            Err(e) => {
                self.error(format!("{e}"));
                return;
            }
        };

        let mut rows = Vec::new();
        let mut missing = 0;
        for want in wanted {
            if let Some(row) = self.tracks.iter().position(|t| t.path == want) {
                rows.push(row);
            } else if want.is_file() {
                // A playlist may name tracks from outside the library; read
                // them in rather than dropping them.
                match library::scan(&want) {
                    Ok(mut found) if !found.is_empty() => {
                        self.tracks.push(found.remove(0));
                        rows.push(self.tracks.len() - 1);
                    }
                    _ => missing += 1,
                }
            } else {
                missing += 1;
            }
        }

        let n = rows.len();
        self.playlist_rows = rows;
        self.playlist_view = Some(name.clone());
        self.cur = 0;
        self.top = 0;
        self.rebuild_view();
        self.focus = Pane::Tracks;

        if missing > 0 {
            self.info(format!("{name}: {n} tracks, {missing} missing"));
        } else {
            self.info(format!("{name}: {n} tracks"));
        }
    }

    /// Writes the current view as a playlist. A bare name goes to the playlist
    /// directory, a name with a path in it goes exactly where it says.
    pub fn save_playlist(&mut self, name: &str) {
        let name = name.trim();
        if name.is_empty() {
            self.error("`:w` needs a name: `:w late night`");
            return;
        }

        let mut path = if name.contains('/') {
            PathBuf::from(name)
        } else {
            let Some(dir) = self.playlists_in() else {
                self.error("cannot find a data directory to save playlists in");
                return;
            };
            dir.join(name)
        };
        if path.extension().is_none() {
            path.set_extension("m3u");
        }

        let tracks: Vec<&Track> = self.view.iter().map(|&i| &self.tracks[i]).collect();
        if tracks.is_empty() {
            self.error("nothing to save");
            return;
        }

        let n = tracks.len();
        match library::write_m3u(&path, &tracks) {
            Ok(()) => {
                self.reload_playlists();
                self.info(format!("saved {n} tracks as {name}"));
            }
            Err(e) => self.error(format!("{e}")),
        }
    }
}