playr-app 0.9.1

What playr's Rust frontends share: actions, : commands, key bindings, settings and dispatch
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
//! The words every frontend shows for each message.
//!
//! Tests elsewhere assert on messages as data; this is the one place their
//! wording is checked, so changing a phrase changes one line here.

use std::time::Duration;

use playr_app::action::Key;
use playr_app::command::parse;
use playr_app::message::{fmt_time, home_as_tilde, text, Message};
use playr_app::{Display, Theme, View};
use playr_core::audio::Mode;
use playr_core::notice::{Notice, Outcome, Refusal, Task};

fn secs(s: u64) -> Duration {
    Duration::from_secs(s)
}

#[test]
fn outcomes_are_worded() {
    let home = std::env::home_dir().unwrap();
    for (outcome, words) in [
        (Outcome::AddedToSelection, "added to selection"),
        (Outcome::AlreadyInSelection, "already in selection"),
        (Outcome::RemovedFromSelection, "removed from selection"),
        (
            Outcome::RemovedTrack {
                title: "So What".into(),
            },
            "removed \"So What\"",
        ),
        (Outcome::SelectionCleared, "selection cleared"),
        (
            Outcome::Saved {
                name: "late".into(),
                tracks: 3,
                left_out: 0,
            },
            "saved \"late\" (3 tracks)",
        ),
        (
            Outcome::Saved {
                name: "mix".into(),
                tracks: 1,
                left_out: 1,
            },
            "saved \"mix\" (1 tracks, 1 not in the library left out)",
        ),
        (
            Outcome::Renamed {
                from: "late".into(),
                to: "night".into(),
            },
            "renamed \"late\" to \"night\"",
        ),
        (
            Outcome::Deleted {
                name: "late".into(),
            },
            "deleted \"late\"",
        ),
        (
            Outcome::PlayingPlaylist {
                name: "dusk".into(),
            },
            "playing \"dusk\"",
        ),
        (Outcome::Mode(Mode::RepeatOne), "mode: repeat one"),
        (
            Outcome::Marked {
                at: secs(30),
                kept: true,
            },
            "marked 0:30",
        ),
        (
            Outcome::Marked {
                at: secs(65),
                kept: false,
            },
            "marked 1:05 (not kept: no library file)",
        ),
        (Outcome::MarkRemoved { at: secs(5) }, "removed mark at 0:05"),
        (Outcome::MarksCleared, "marks cleared"),
        (Outcome::AtMark { at: secs(90) }, "mark at 1:30"),
        (Outcome::PlanStarted, "planning slices"),
        (
            Outcome::Planned { slices: 1 },
            "1 slice planned: enter writes, esc discards",
        ),
        (
            Outcome::Planned { slices: 4 },
            "4 slices planned: enter writes, esc discards",
        ),
        (Outcome::ExportStarted, "exporting"),
        (
            Outcome::Exported {
                dir: home.join("Music/playr/samples/amen"),
                slices: 1,
            },
            "exported 1 slice to ~/Music/playr/samples/amen",
        ),
        (
            Outcome::Exported {
                dir: "/tmp/cuts".into(),
                slices: 8,
            },
            "exported 8 slices to /tmp/cuts",
        ),
    ] {
        assert_eq!(text(&outcome.into()), words);
    }
}

#[test]
fn scans_and_opened_files_are_worded() {
    use playr_core::scan::{ScanReport, ScanStats};
    let home = std::env::home_dir().unwrap();
    let report = ScanReport {
        stats: ScanStats {
            seen: 1200,
            added: 300,
            skipped: 899,
            failed: 1,
        },
        missing: 2,
        unavailable: 0,
        total: 5000,
    };
    for (outcome, words) in [
        (
            Outcome::ScanStarted {
                dir: Some(home.join("music")),
            },
            "scanning ~/music",
        ),
        (Outcome::ScanStarted { dir: None }, "rescanning library"),
        (
            Outcome::Scanning {
                seen: 1200,
                added: 300,
            },
            "scanning: 1200 files, 300 added",
        ),
        (
            Outcome::Scanned {
                dir: Some(home.join("music")),
                report,
            },
            "scanned ~/music: 300 added, 1 unreadable, 2 missing; 5000 tracks",
        ),
        (
            Outcome::Scanned {
                dir: Some(home.join("music")),
                report: ScanReport {
                    unavailable: 1,
                    ..report
                },
            },
            "scanned ~/music: 300 added, 1 unreadable, 2 missing, a directory unreadable; 5000 tracks",
        ),
        (
            Outcome::Scanned {
                dir: None,
                report: ScanReport {
                    unavailable: 2,
                    ..report
                },
            },
            "scanned library: 300 added, 1 unreadable, 2 missing, 2 directories unreadable; 5000 tracks",
        ),
        (
            Outcome::Scanned {
                dir: None,
                report: ScanReport {
                    missing: 0,
                    ..report
                },
            },
            "scanned library: 300 added, 1 unreadable; 5000 tracks",
        ),
        (
            Outcome::Scanned {
                dir: Some(home.join("music")),
                report: ScanReport {
                    missing: 0,
                    ..report
                },
            },
            "scanned ~/music: 300 added, 1 unreadable; 5000 tracks",
        ),
        (
            Outcome::PruneStarted {
                dir: Some(home.join("music")),
            },
            "pruning ~/music",
        ),
        (Outcome::PruneStarted { dir: None }, "pruning library"),
        (
            Outcome::Pruned {
                dir: Some(home.join("music")),
                removed: playr_core::db::Pruned {
                    tracks: 1,
                    marks: 3,
                },
            },
            "pruned ~/music: 1 track and 3 marks of missing files",
        ),
        (
            Outcome::Pruned {
                dir: None,
                removed: playr_core::db::Pruned {
                    tracks: 1,
                    marks: 3,
                },
            },
            "pruned library: 1 track and 3 marks of missing files",
        ),
        (
            Outcome::Forgot {
                dir: home.join("music"),
                removed: playr_core::db::Pruned {
                    tracks: 12,
                    marks: 1,
                },
            },
            "forgot ~/music: 12 tracks and 1 mark removed",
        ),
        (Outcome::Opening, "opening"),
        (
            Outcome::Opened {
                tracks: 1,
                skipped: 0,
            },
            "playing 1 track",
        ),
        (
            Outcome::Opened {
                tracks: 12,
                skipped: 2,
            },
            "playing 12 tracks; 2 skipped",
        ),
    ] {
        assert_eq!(text(&outcome.into()), words);
    }
}

#[test]
fn refusals_are_worded() {
    for (refusal, words) in [
        (Refusal::NothingPlaying, "nothing is playing"),
        (Refusal::SelectionEmpty, "selection is empty"),
        (
            Refusal::NoLibraryFile,
            "no library to save to; `playr scan <dir>` creates one",
        ),
        (Refusal::NameEmpty, "playlist name cannot be empty"),
        (Refusal::NameUnchanged, "name unchanged"),
        (
            Refusal::NameTaken("early".into()),
            "a playlist named \"early\" already exists",
        ),
        (
            Refusal::NoPlaylistNamed("nope".into()),
            "no single playlist named \"nope\"",
        ),
        (Refusal::PlaylistEmpty, "playlist is empty"),
        (
            Refusal::AlreadyMarked { at: secs(0) },
            "already marked at 0:00",
        ),
        (Refusal::NoMarks, "no marks in this track"),
        (Refusal::NoLaterMark, "no later mark"),
        (Refusal::NoEarlierMark, "no earlier mark"),
        (Refusal::NoLibraryPath, "no library file to scan into"),
        (
            Refusal::NotADirectory("/opt/nothing".into()),
            "not a directory: /opt/nothing",
        ),
        (Refusal::ScanRunning, "a scan or prune is already running"),
        (
            Refusal::NoRoots,
            "no directories recorded; :scan DIR adds one",
        ),
        (
            Refusal::NotARoot("/opt/nothing".into()),
            "not one of the library's directories: /opt/nothing; :roots lists them",
        ),
    ] {
        assert_eq!(text(&refusal.into()), words);
    }
}

#[test]
fn failures_and_playback_errors_are_worded() {
    let failed = |task| {
        text(&Message::Core(Notice::Failed {
            task,
            error: "disk full".into(),
        }))
    };
    assert_eq!(failed(Task::Save), "could not save: disk full");
    assert_eq!(failed(Task::Rename), "could not rename: disk full");
    assert_eq!(failed(Task::Mark), "could not mark: disk full");
    assert_eq!(failed(Task::RemoveMark), "could not remove mark: disk full");
    assert_eq!(failed(Task::ClearMarks), "could not clear marks: disk full");
    assert_eq!(failed(Task::Slice), "slicing failed: disk full");
    assert_eq!(failed(Task::Export), "export failed: disk full");
    assert_eq!(failed(Task::Scan), "scan failed: disk full");
    assert_eq!(failed(Task::Prune), "prune failed: disk full");
    assert_eq!(failed(Task::Open), "could not open: disk full");

    let playback = |missed| {
        text(&Message::Core(Notice::PlaybackError {
            error: "cannot play b.flac".into(),
            missed,
        }))
    };
    assert_eq!(playback(0), "cannot play b.flac");
    assert_eq!(playback(2), "cannot play b.flac (and 2 more)");
}

#[test]
fn terminal_messages_are_worded() {
    let d = Key::parse("d").unwrap();
    for (message, words) in [
        (Message::Cancelled, "cancelled"),
        (Message::NoMatches, "no matches"),
        (
            Message::NoPlaylistUnderCursor,
            "no playlist under the cursor in the playlists view",
        ),
        (Message::Display(Display::Decibels), "display: db"),
        (Message::Theme(Theme::Light), "theme: light"),
        (Message::Snap(true), "snap to zero crossings: on"),
        (Message::NoWaveform, "no waveform to move along yet"),
        (Message::EmptyRange, "the range is empty"),
        (Message::Loop(true), "loop: on"),
        (
            Message::Edge(playr_app::sampler::Edge::End),
            "moving the range end",
        ),
        (
            Message::NoEdge(playr_app::sampler::Edge::Start),
            "no range start to move: set it with < or >",
        ),
        (
            Message::NoRangeToLoop,
            "no range to loop: set one with < and >, or drag",
        ),
        (
            Message::Range {
                start: Some(8_000),
                end: Some(20_000),
                rate: 8_000,
            },
            "range 0:01.000-0:02.500 (1.500 s)",
        ),
        (
            Message::Range {
                start: Some(8_000),
                end: None,
                rate: 8_000,
            },
            "range from 0:01.000",
        ),
        (
            Message::Range {
                start: None,
                end: None,
                rate: 8_000,
            },
            "range cleared",
        ),
        (
            Message::Mapped(parse("map selection ctrl-x clear", View::Library).unwrap()),
            "map selection ctrl-x clear",
        ),
        (Message::Unmapped(d), "unmapped d"),
        (
            Message::NotBound {
                key: d,
                view: Some(View::Selection),
            },
            "d has no binding in the selection view",
        ),
        (
            Message::NotBound { key: d, view: None },
            "d has no binding for all views",
        ),
        (
            Message::NoSlicesPlanned,
            "no slices planned; :slice plans them",
        ),
        (Message::SlicesDiscarded, "slices discarded"),
        (
            Message::Command("not a time: soon (try 1:23 or 90)".into()),
            "not a time: soon (try 1:23 or 90)",
        ),
    ] {
        assert_eq!(text(&message), words);
    }
}

#[test]
fn paths_under_home_are_shown_from_tilde() {
    use std::path::Path;
    let home = std::env::home_dir().unwrap();
    assert_eq!(
        home_as_tilde(&home.join("Music/playr/samples/amen")),
        "~/Music/playr/samples/amen"
    );
    assert_eq!(home_as_tilde(Path::new("/opt/cuts")), "/opt/cuts");
}

#[test]
fn times_are_minutes_and_seconds_then_hours() {
    assert_eq!(fmt_time(Duration::from_millis(59_999)), "0:59");
    assert_eq!(fmt_time(secs(754)), "12:34");
    assert_eq!(fmt_time(secs(3723)), "1:02:03");
}