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
//! Application state. One struct, mutated by key handlers and ex commands,
//! read by the renderer. No state lives anywhere else.

use std::path::PathBuf;

use anyhow::Result;

use crate::library::{self, SortKey, Track};
use crate::lyrics::Fetcher;
use crate::matrix::Matrix;
use crate::name::NameBuffer;
use crate::mpris::{self, Mpris};
use crate::player::Audio;

/// Rows kept between the cursor and the edge of the track pane.
pub const SCROLLOFF: usize = 3;

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Mode {
    Normal,
    /// The `:` line has focus.
    Command,
    /// The `/` or `?` line has focus.
    Search,
    /// Linewise visual, the only visual there is when rows are tracks.
    Visual,
    /// The track list is a buffer of filenames being edited in place.
    Edit,
    /// Typing inside that buffer.
    EditInsert,
}

impl Mode {
    pub fn label(self) -> &'static str {
        match self {
            Mode::Normal => "NORMAL",
            Mode::Command => "COMMAND",
            Mode::Search => "SEARCH",
            Mode::Visual => "VISUAL",
            Mode::Edit => "EDIT",
            Mode::EditInsert => "INSERT",
        }
    }
}

/// What the left pane is showing.
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Tab {
    Folders,
    Playlists,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum Pane {
    Folders,
    Tracks,
}

#[derive(Clone, Copy, PartialEq, Eq, Debug, Default)]
pub enum Repeat {
    #[default]
    Off,
    All,
    One,
}

impl Repeat {
    pub fn next(self) -> Repeat {
        match self {
            Repeat::Off => Repeat::All,
            Repeat::All => Repeat::One,
            Repeat::One => Repeat::Off,
        }
    }

    pub fn name(self) -> &'static str {
        match self {
            Repeat::Off => "off",
            Repeat::All => "all",
            Repeat::One => "one",
        }
    }
}

/// Which tag columns the track list shows, toggled with `:set`.
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Columns {
    pub file: bool,
    pub title: bool,
    pub artist: bool,
    pub album: bool,
}

impl Default for Columns {
    fn default() -> Self {
        // The title repeats the filename on a tagged library, so it stays off.
        Columns {
            file: true,
            title: false,
            artist: true,
            album: true,
        }
    }
}

impl Columns {
    pub fn get(&self, name: &str) -> Option<bool> {
        match name {
            "file" => Some(self.file),
            "title" => Some(self.title),
            "artist" => Some(self.artist),
            "album" => Some(self.album),
            _ => None,
        }
    }

    /// False when the name is not an option, so the caller can complain.
    pub fn set(&mut self, name: &str, on: bool) -> bool {
        match name {
            "file" => self.file = on,
            "title" => self.title = on,
            "artist" => self.artist = on,
            "album" => self.album = on,
            _ => return false,
        }
        true
    }

    /// Visible columns with the share of the width each one gets.
    pub fn shown(self) -> Vec<(&'static str, usize)> {
        [
            ("file", self.file, 40),
            ("title", self.title, 34),
            ("artist", self.artist, 26),
            ("album", self.album, 26),
        ]
        .into_iter()
        .filter(|(_, on, _)| *on)
        .map(|(name, _, weight)| (name, weight))
        .collect()
    }
}

/// Everything waiting for a `:w`, kept in one place so undo is a snapshot of
/// it and `:w` is the only thing that touches the disk.
#[derive(Clone, Default)]
pub struct Pending {
    pub renames: std::collections::BTreeMap<Renaming, String>,
    pub playlist_rows: Vec<usize>,
    pub playlist_dirty: bool,
    /// Playlist files marked with `dd`.
    pub doomed: Vec<PathBuf>,
    /// Tracks marked for deletion, danger mode only.
    pub doomed_files: Vec<PathBuf>,
    /// Files cut with `dd`, waiting for a `p` to turn them into moves.
    pub cut: Vec<PathBuf>,
    pub moves: Vec<(PathBuf, PathBuf)>,
    pub copies: Vec<(PathBuf, PathBuf)>,
    /// Folders marked with `dd`, taken with everything inside them.
    pub doomed_dirs: Vec<PathBuf>,
    /// The name being typed in the sidebar, so `u` works there as well.
    pub sidebar_name: Option<String>,
}

/// One open view: what it shows, and where you left it. Snapshotted on the way
/// out of a tab and restored on the way back, the way vim keeps a tab page.
#[derive(Clone)]
pub struct ViewTab {
    pub playlist: Option<String>,
    pub folder: usize,
    pub cur: usize,
    pub top: usize,
    pub sort_key: SortKey,
    rows: Vec<usize>,
    dirty: bool,
}

/// What a rename is renaming. Tracks come from the list, folders and playlists
/// from the sidebar, and all three queue up in the same pending set.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Renaming {
    Track(usize),
    Folder(PathBuf),
    Playlist(String),
}

/// The name being typed right now: one buffer, and what it will rename.
///
/// Only one thing is ever being typed at a time, in whichever pane `c` opened.
/// Leaving it puts the result in `renames` with everything else waiting for
/// `:w`, so a rename is not a special case that needs writing on its own.
pub struct NameEdit {
    pub buf: NameBuffer,
    pub what: Renaming,
}

pub struct App {
    pub root: PathBuf,
    pub tracks: Vec<Track>,
    pub sort_key: SortKey,
    pub columns: Columns,
    /// The library opened when vibox is started with no path, from `:set music=`.
    pub music: Option<PathBuf>,
    pub show_lyrics: bool,
    /// Whether the lyrics pane follows the song. Off leaves the words still and
    /// unstyled, which is what you want when lrclib's timings do not fit your
    /// recording.
    pub karaoke: bool,
    pub matrix: Matrix,
    pub lyrics: Fetcher,
    /// Directories that hold tracks. Index 0 of the pane is the whole library,
    /// so a folder `i` in this vec is row `i + 1`.
    pub folders: Vec<(String, PathBuf)>,
    pub folder_cur: usize,
    /// The folder actually shown, as opposed to the one the cursor sits on.
    pub folder_open: usize,
    pub folder_top: usize,
    pub folder_h: usize,
    /// Which tab the left pane shows, and the m3u files it lists.
    pub tab: Tab,
    pub playlists: Vec<(String, PathBuf)>,
    pub pl_cur: usize,
    pub pl_top: usize,
    /// The playlist currently shown, if any. It is a view over the library, so
    /// the folders tab keeps browsing the whole library while it is open.
    pub playlist_view: Option<String>,
    playlist_rows: Vec<usize>,
    /// Open views, and which one you are looking at.
    pub tabs: Vec<ViewTab>,
    pub tab_idx: usize,

    /// Indices into `tracks`, in display order.
    pub view: Vec<usize>,
    pub cur: usize,
    pub top: usize,
    pub track_h: usize,

    pub focus: Pane,
    pub mode: Mode,
    /// Text of the `:` or `/` line being typed, without its leading char.
    pub line: String,
    pub line_prefix: char,
    /// Cursor position in `line`, counted in characters.
    pub line_cur: usize,
    /// False puts the line in its own normal mode, where hjkl and w b move.
    pub line_insert: bool,
    pub count: Option<usize>,
    /// Half typed multi key sequence: `g`, `z`, `d`, `y` or ctrl-w.
    pub pending: Option<char>,

    pub last_search: String,
    pub search_back: bool,
    pub visual_anchor: Option<usize>,
    /// Tracks picked up by `y`, waiting for a `p` into a playlist.
    pub yank: Vec<PathBuf>,
    /// Playlist rows changed since the last `:w`.
    pub playlist_dirty: bool,
    /// Playlists marked for deletion, written by `:w` like every other change.
    pub doomed: Vec<PathBuf>,
    /// `:set danger`: lets `dd`, `p` and `o` touch files, still only on `:w`.
    pub danger: bool,
    pub doomed_files: Vec<PathBuf>,
    pub cut: Vec<PathBuf>,
    pub moves: Vec<(PathBuf, PathBuf)>,
    /// Yanked files waiting to be copied into a folder, danger mode only.
    pub copies: Vec<(PathBuf, PathBuf)>,
    pub doomed_dirs: Vec<PathBuf>,
    undo_stack: Vec<Pending>,
    redo_stack: Vec<Pending>,
    /// Filename edits waiting for `:w`, and where the cursor is inside one.
    pub edit: Option<NameEdit>,
    /// Renames waiting for `:w`: tracks, folders and playlists together.
    pub renames: std::collections::BTreeMap<Renaming, String>,
    /// Set only while the sidebar is renaming something.
    /// Overridden only by tests; `None` means the user's data directory.
    pub playlists_dir: Option<PathBuf>,

    /// Playback order, snapshotted from the view when playback starts.
    pub queue: Vec<usize>,
    pub qpos: usize,
    /// Queue positions already played, so shuffle can go back the way it came.
    history: Vec<usize>,
    pub playing: Option<usize>,
    pub repeat: Repeat,
    pub shuffle: bool,

    pub audio: Option<Audio>,
    /// Absent on a machine with no session bus; media keys just do nothing then.
    pub mpris: Option<Mpris>,
    pub msg: Option<(String, bool)>,
    /// Where the real terminal cursor goes while inserting, set by the renderer.
    pub cursor_screen: Option<(u16, u16)>,
    /// Everything played this session, oldest first, one line each. Repeats are
    /// kept: playing a track twice happened twice. It is a log rather than a
    /// list of indices, so a delete or a rename cannot make it point at the
    /// wrong track later.
    pub played: Vec<String>,
    pub show_history: bool,
    /// First line of the log on screen, clamped by the renderer.
    pub history_top: usize,
    pub show_info: bool,
    /// How far `:changes` is panned sideways, in cells.
    pub changes_pan: usize,
    pub show_changes: bool,
    pub show_help: bool,
    pub help_scroll: usize,
    pub quit: bool,
    rng: u64,
}

impl App {
    pub fn new(root: PathBuf, sort_key: SortKey, on: library::Report) -> Result<App> {
        let (audio, audio_err) = match Audio::new() {
            Ok(a) => (Some(a), None),
            Err(e) => (None, Some(format!("{e}"))),
        };

        let mut app = App {
            root,
            tracks: Vec::new(),
            sort_key,
            columns: Columns::default(),
            music: None,
            show_lyrics: false,
            karaoke: true,
            matrix: Matrix::default(),
            lyrics: Fetcher::new(),
            folders: Vec::new(),
            folder_cur: 0,
            folder_open: 0,
            folder_top: 0,
            folder_h: 1,
            tab: Tab::Folders,
            playlists: Vec::new(),
            pl_cur: 0,
            pl_top: 0,
            playlist_view: None,
            playlist_rows: Vec::new(),
            tabs: Vec::new(),
            tab_idx: 0,
            view: Vec::new(),
            cur: 0,
            top: 0,
            track_h: 1,
            focus: Pane::Tracks,
            mode: Mode::Normal,
            line: String::new(),
            line_prefix: ':',
            line_cur: 0,
            line_insert: true,
            count: None,
            pending: None,
            last_search: String::new(),
            search_back: false,
            visual_anchor: None,
            yank: Vec::new(),
            playlist_dirty: false,
            doomed: Vec::new(),
            danger: false,
            doomed_files: Vec::new(),
            cut: Vec::new(),
            moves: Vec::new(),
            copies: Vec::new(),
            doomed_dirs: Vec::new(),
            undo_stack: Vec::new(),
            redo_stack: Vec::new(),
            edit: None,
            renames: std::collections::BTreeMap::new(),
            playlists_dir: None,
            queue: Vec::new(),
            qpos: 0,
            history: Vec::new(),
            playing: None,
            repeat: Repeat::Off,
            shuffle: false,
            audio,
            mpris: mpris::start().ok(),
            msg: None,
            cursor_screen: None,
            played: Vec::new(),
            show_history: false,
            history_top: 0,
            show_info: false,
            changes_pan: 0,
            show_changes: false,
            show_help: false,
            help_scroll: 0,
            quit: false,
            rng: seed(),
        };

        app.reload_playlists();
        app.reload_reporting(on)?;
        app.tabs.push(app.snapshot());
        if let Some(e) = audio_err {
            app.error(e);
        }
        Ok(app)
    }



    pub fn info(&mut self, text: impl Into<String>) {
        self.msg = Some((text.into(), false));
    }

    pub fn error(&mut self, text: impl Into<String>) {
        self.msg = Some((text.into(), true));
    }
}

fn plural(n: usize) -> &'static str {
    if n == 1 { "" } else { "s" }
}

fn seed() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map_or(0x2545_F491_4F6C_DD1D, |d| d.as_nanos() as u64)
        | 1
}

// The rest of `impl App`, split by what each part deals with. Several impl
// blocks for one type are ordinary rust; the split is so a reader can find
// things, and so each area only reaches what it needs.
mod scan;
mod cursor;
mod undo;
mod danger;
mod tabs;
mod select;
mod playlists;
mod rename;
mod search;
mod playback;

#[cfg(test)]
mod tests;