playr-app 0.19.0

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
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
//! Messages about what an action did: as data, and in words.
//!
//! A core operation reports a [`Notice`]; the rest are about the interface
//! itself: prompts, key bindings and views. [`text`] is the only place either
//! is turned into words, so every frontend says the same thing, and tests
//! assert on messages while one test checks the wording of each.

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

use playr_core::notice::{Notice, Outcome, Refusal, Task};

use crate::action::{Action, Key};
use crate::command;
use crate::{Display, Theme, View};
use playr_core::analysis::{self, tempo, Analysis, Finding, Md5};
use playr_core::columns::{Column, SortKey};
use playr_core::db::Track;
use playr_core::gain::Gains;

/// A message for the person using the interface.
#[derive(Debug, Clone, PartialEq)]
pub enum Message {
    Core(Notice),
    /// A confirmation was answered with anything but `y`.
    Cancelled,
    NoMatches,
    /// A Loops view action with no loop under the cursor.
    NoLoopUnderCursor,
    /// A loop taken out of the loop library, by name.
    LoopRemoved(String),
    /// A loop's beats or key as now set.
    LoopSet(Box<playr_core::db::loops::Loop>),
    LoopFailed(String),
    NoPlaylistUnderCursor,
    /// `:enqueue all` with no search results or selection to queue.
    NothingToQueue,
    /// `:clear` in the Queue view, with nothing in the queue.
    QueueEmpty,
    Display(Display),
    Theme(Theme),
    /// The tone control's gains in dB, by band.
    Eq([f32; 3]),
    /// The bands `:eq bypass` holds while the EQ plays flat.
    EqBypassed([f32; 3]),
    /// The columns a list shows, as `:columns` set them.
    Columns(Vec<Column>),
    /// What lists are sorted by, as `:sort` set it.
    Sorted(Vec<SortKey>),
    /// A nudge came before the sampler view showed a waveform.
    NoWaveform,
    Snap(bool),
    Fit(bool),
    /// The sampler's range, as set, in frames at `rate`.
    Range {
        start: Option<u64>,
        end: Option<u64>,
        rate: u32,
    },
    EmptyRange,
    Loop(bool),
    /// Loop `slot` recalled into the range, in frames at `rate`.
    LoopRecalled {
        slot: u8,
        start: u64,
        end: u64,
        rate: u32,
    },
    /// An empty loop slot, or a save, with no range to save.
    NoRangeToSave(u8),
    /// A loop was asked for with only one end of the range set.
    NoRangeToLoop,
    /// Nothing under the playhead has both ends, so there is nothing to hear.
    NothingToAudition,
    /// A span is playing once, and will pause at its end.
    Auditioning,
    /// The sound around a mark is being read, to find the rise to snap to.
    Snapping,
    /// This end of the range is selected.
    Edge(crate::sampler::Edge),
    /// An edge move found no such end of the range to move.
    NoEdge(crate::sampler::Edge),
    /// An edit key with nothing selected to act on.
    NothingSelected,
    Deselected,
    Undone,
    NothingToUndo,
    Redone,
    NothingToRedo,
    /// Planned slice `slice`, from 1, now starts at frame `at` of `rate`.
    SliceMoved {
        slice: usize,
        at: u64,
        rate: u32,
    },
    /// Planned slice `slice + 1` was joined to slice `slice`, from 1.
    SlicesJoined {
        slice: usize,
    },
    /// The first planned slice's start was asked to go.
    FirstSlice,
    /// No planned slice starts where one was asked for.
    NoSliceHere,
    /// The decks' and the tape's heads now read with this.
    Interp(playr_dsp::Interp),
    /// Tracks fade in and out over this long from now; zero for none.
    Fade(std::time::Duration),
    /// `:tempo` asked for more than an octave from the track's tempo.
    TempoOutOfReach {
        bpm: f32,
        found: f32,
    },
    /// The sampler shows this track without playing it.
    Previewing(PathBuf),
    /// The sampler shows the playing track again.
    PreviewEnded,
    /// `:preview` in a list with no track under the cursor.
    NothingToPreview,
    /// `:preview` in the sampler with nothing previewed.
    NotPreviewing,
    /// An edit refused while the sampler previews this track.
    PreviewOnly(PathBuf),
    /// A `map` command took effect; holds the `Action::Map`.
    Mapped(Action),
    Unmapped(Key),
    NotBound {
        key: Key,
        view: Option<View>,
    },
    NoSlicesPlanned,
    SlicesDiscarded,
    /// Marks were added at this many planned slice starts.
    SlicesMarked {
        added: usize,
    },
    /// A `:` command could not be parsed or run; the parser's own words.
    Command(String),
    /// A `:sql` statement is running on its own thread.
    Querying,
    /// A `:sql` statement named this many library tracks.
    Found(usize),
    /// Saved searches are not renamed in place yet.
    SearchNotRenamed,
    /// Playback will stop once the track playing ends, or no longer will.
    StopAfter(bool),
    /// The sleep timer was set to this, or turned off.
    StopIn(Option<Duration>),
    /// The sleep timer ran out and stopped playback.
    Slept,
    Tape(crate::tape::TapeMessage),
    Dj(crate::dj::DjMessage),
    /// Every fader, as `:mix` reports them.
    Mix(crate::mix::Mix),
    /// The fader law now in use.
    FaderLaw(crate::mix::Law),
    /// The master is being recorded to this file.
    MasterRecording(std::path::PathBuf),
    /// The master's recording stops once its file is written.
    MasterStopping,
    MasterRecorded(playr_core::audio::record::Recorded),
    MasterFailed(String),
}

impl From<Notice> for Message {
    fn from(notice: Notice) -> Self {
        Message::Core(notice)
    }
}

impl From<Outcome> for Message {
    fn from(outcome: Outcome) -> Self {
        Message::Core(Notice::Done(outcome))
    }
}

impl From<Refusal> for Message {
    fn from(refusal: Refusal) -> Self {
        Message::Core(Notice::Refused(refusal))
    }
}

/// `n` slices, as a count with its noun.
fn slices(n: usize) -> String {
    match n {
        1 => "1 slice".into(),
        n => format!("{n} slices"),
    }
}

/// The words for `message`.
pub fn text(message: &Message) -> String {
    match message {
        Message::Core(Notice::Done(outcome)) => outcome_text(outcome),
        Message::Core(Notice::Refused(refusal)) => refusal_text(refusal),
        Message::Core(Notice::Failed { task, error }) => {
            let what = match task {
                Task::Save => "could not save",
                Task::Rename => "could not rename",
                Task::Mark => "could not mark",
                Task::Loop => "could not save the loop",
                Task::MoveMark => "could not move mark",
                Task::RemoveMark => "could not remove mark",
                Task::ClearMarks => "could not clear marks",
                Task::Slice => "slicing failed",
                Task::Export => "export failed",
                Task::Convert => "conversion failed",
                Task::Scan => "scan failed",
                Task::Analyze => "analysis failed",
                Task::Prune => "prune failed",
                Task::Open => "could not open",
                Task::FixTempo => "could not correct the tempo",
            };
            format!("{what}: {error}")
        }
        // Only the latest error is kept, so a run of bad files would
        // otherwise show one name and hide the rest.
        Message::Core(Notice::PlaybackError { error, missed: 0 }) => error.clone(),
        Message::Core(Notice::PlaybackError { error, missed }) => {
            format!("{error} (and {missed} more)")
        }
        Message::Cancelled => "cancelled".into(),
        Message::NoMatches => "no matches".into(),
        Message::NoLoopUnderCursor => "no loop under the cursor".into(),
        Message::LoopRemoved(name) => format!("took {name} out of the loop library"),
        Message::LoopSet(l) => {
            let mut parts = vec![l.name.clone()];
            if let (Some(b), Some(t)) = (l.beats, l.bpm) {
                parts.push(format!("{b} beats, {t:.1} BPM"));
            }
            if let Some(k) = &l.key {
                parts.push(format!("key {k}"));
            }
            parts.join(": ")
        }
        Message::LoopFailed(e) => format!("loop library: {e}"),
        Message::NothingToQueue => {
            "nothing to queue; :enqueue all queues search results or the selection".into()
        }
        Message::QueueEmpty => "the queue is empty".into(),
        Message::Querying => "running the query...".into(),
        Message::Found(1) => "1 track".into(),
        Message::Found(n) => format!("{n} tracks"),
        Message::SearchNotRenamed => {
            "save it again under the new name, then delete the old one".into()
        }
        Message::StopAfter(true) => "stopping after this track".into(),
        Message::StopAfter(false) => "playing on after this track".into(),
        Message::StopIn(Some(d)) => format!("stopping in {}", fmt_time(*d)),
        Message::StopIn(None) => "sleep timer off".into(),
        Message::Slept => "sleep timer ran out; stopped".into(),
        Message::Tape(m) => tape_text(m),
        Message::Dj(m) => dj_text(m),
        Message::Mix(mix) => mix_text(mix),
        Message::FaderLaw(law) => format!("fader law: {}", law.name()),
        Message::MasterRecording(path) => {
            format!("recording the master to {}", home_as_tilde(path))
        }
        Message::MasterStopping => "finishing the master's recording".into(),
        Message::MasterRecorded(r) => {
            let secs = r.frames as f64 / f64::from(r.rate.max(1));
            let done = format!(
                "recorded the master: {secs:.2} s to {}",
                home_as_tilde(&r.path)
            );
            match r.dropped {
                0 => done,
                n => format!("{done}; {n} blocks lost, the disk was too slow"),
            }
        }
        Message::MasterFailed(e) => format!("master recording: {e}"),
        Message::NoPlaylistUnderCursor => {
            "no playlist under the cursor in the playlists view".into()
        }
        Message::Display(display) => format!("display: {}", display.name()),
        Message::Theme(theme) => format!("theme: {}", theme.name()),
        Message::Eq(gains) => eq(*gains),
        Message::EqBypassed(gains) => format!("{} bypassed", eq(*gains)),
        Message::Columns(columns) => {
            let names: Vec<&str> = columns.iter().map(|c| c.name()).collect();
            format!("columns: {}", names.join(", "))
        }
        Message::Sorted(keys) if keys.is_empty() => "sorted: as the library was scanned".into(),
        Message::Sorted(keys) => {
            let names: Vec<String> = keys.iter().map(|k| k.text()).collect();
            format!("sorted by {}", names.join(", then "))
        }
        Message::NoWaveform => "no waveform to move along yet".into(),
        Message::Snap(on) => format!("snap to zero crossings: {}", if *on { "on" } else { "off" }),
        Message::Fit(on) => format!("fit the range: {}", if *on { "on" } else { "off" }),
        Message::Range { start, end, rate } => {
            let at = |f: &u64| crate::sampler::fmt_frames(*f, *rate);
            match (start, end) {
                (Some(a), Some(b)) => format!(
                    "range {}-{} ({:.3} s)",
                    at(a),
                    at(b),
                    (b - a) as f64 / *rate as f64
                ),
                (Some(a), None) => format!("range from {}", at(a)),
                (None, Some(b)) => format!("range to {}", at(b)),
                (None, None) => "range cleared".into(),
            }
        }
        Message::EmptyRange => "the range is empty".into(),
        Message::Loop(on) => format!("loop: {}", if *on { "on" } else { "off" }),
        Message::LoopRecalled {
            slot,
            start,
            end,
            rate,
        } => format!(
            "loop {slot}: {}-{}",
            crate::sampler::fmt_frames(*start, *rate),
            crate::sampler::fmt_frames(*end, *rate)
        ),
        Message::NoRangeToSave(slot) => {
            format!("no range to save as loop {slot}: set one with i and o, or drag")
        }
        Message::NoRangeToLoop => "the range has one end: set the other with i or o".into(),
        Message::NothingToAudition => "nothing to hear here: set a range, or mark one".into(),
        Message::Auditioning => "playing once".into(),
        Message::Snapping => "looking for the nearest rise".into(),
        Message::Edge(edge) => format!("selected the range {}", edge.name()),
        Message::NoEdge(edge) => format!("no range {} to select: set it with i or o", edge.name()),
        Message::NothingSelected => {
            "nothing selected: select a mark with { }, a range end with [ ], a slice with , ."
                .into()
        }
        Message::Deselected => "selected nothing".into(),
        Message::Undone => "undone".into(),
        Message::NothingToUndo => "nothing to undo".into(),
        Message::Redone => "redone".into(),
        Message::NothingToRedo => "nothing to redo".into(),
        Message::SliceMoved { slice, at, rate } => format!(
            "slice {slice} starts at {}",
            crate::sampler::fmt_frames(*at, *rate)
        ),
        Message::SlicesJoined { slice } => format!("joined slice {} to slice {slice}", slice + 1),
        Message::FirstSlice => "the first slice has none before it to join".into(),
        Message::NoSliceHere => "no planned slice starts here".into(),
        Message::Previewing(path) => {
            format!("previewing {}; :preview here ends it", file_name(path))
        }
        Message::PreviewEnded => "preview ended".into(),
        Message::Interp(i) => format!("read heads: {}", i.name()),
        Message::Fade(d) if d.is_zero() => "fades off: tracks play as they are".into(),
        Message::Fade(d) => format!(
            "each track fades in and out over {} s",
            (d.as_secs_f64() * 100.0).round() / 100.0
        ),
        Message::TempoOutOfReach { bpm, found } => format!(
            "{bpm} BPM is more than 12 semitones from the track's {:.1}",
            found
        ),
        Message::NothingToPreview => "no track under the cursor to preview".into(),
        Message::NotPreviewing => "nothing previewed: :preview in a list shows its row".into(),
        Message::PreviewOnly(path) => format!("previewing {}: play it to edit", file_name(path)),
        Message::Mapped(map) => command::line(map, None),
        Message::Unmapped(key) => format!("unmapped {key}"),
        Message::NotBound { key, view } => {
            format!("{key} has no binding {}", command::scope(*view))
        }
        Message::NoSlicesPlanned => "no slices planned; :slice plans them".into(),
        Message::SlicesDiscarded => "slices discarded".into(),
        Message::SlicesMarked { added: 0 } => "every slice start is marked already".into(),
        Message::SlicesMarked { added: 1 } => "marked 1 slice start".into(),
        Message::SlicesMarked { added } => format!("marked {added} slice starts"),
        Message::Command(error) => error.clone(),
    }
}

fn outcome_text(outcome: &Outcome) -> String {
    match outcome {
        Outcome::AddedToSelection => "added to selection".into(),
        Outcome::AlreadyInSelection => "already in selection".into(),
        Outcome::RemovedFromSelection => "removed from selection".into(),
        Outcome::RemovedTrack { title } => format!("removed \"{title}\""),
        Outcome::SelectionCleared => "selection cleared".into(),
        Outcome::Saved {
            name,
            tracks,
            left_out,
        } => {
            let note = match left_out {
                0 => String::new(),
                n => format!(", {n} not in the library left out"),
            };
            format!("saved \"{name}\" ({tracks} tracks{note})")
        }
        Outcome::Renamed { from, to } => format!("renamed \"{from}\" to \"{to}\""),
        Outcome::Deleted { name } => format!("deleted \"{name}\""),
        Outcome::PlayingPlaylist { name } => format!("playing \"{name}\""),
        Outcome::Queued { tracks: 1, next } => match next {
            true => "plays next".into(),
            false => "queued".into(),
        },
        Outcome::Editing { name } => format!("editing \"{name}\"; s saves it"),
        Outcome::SearchSaved { name } => format!("saved search \"{name}\""),
        Outcome::DraftOverwritten => "the old draft gives way to the selection".into(),
        Outcome::DraftAppended => "the old draft is back in the selection".into(),
        Outcome::QueueCleared { tracks: 1 } => "took 1 track out of the queue".into(),
        Outcome::QueueCleared { tracks } => format!("took {tracks} tracks out of the queue"),
        Outcome::QueueReplaced { tracks: 1 } => "queue replaced; 1 track was waiting".into(),
        Outcome::QueueReplaced { tracks } => {
            format!("queue replaced; {tracks} tracks were waiting")
        }
        Outcome::Queued { tracks, next } => match next {
            true => format!("{tracks} tracks play next"),
            false => format!("queued {tracks} tracks"),
        },
        Outcome::Mode(mode) => format!("mode: {}", mode.name()),
        Outcome::ReplayGain(r) => format!("replaygain: {}", r.name()),
        Outcome::SliceEdges(e) => format!("slice edges: {}", e.name()),
        Outcome::LoopSaved { slot, kept } => format!(
            "loop {slot} saved{}",
            if *kept {
                ""
            } else {
                " (not kept: no library file)"
            }
        ),
        Outcome::LoopCleared { slot } => format!("loop {slot} cleared"),
        Outcome::LoopsCleared => "loops cleared".into(),
        Outcome::Marked { at, kept } => {
            // As with playlists: in memory, the mark is gone when playr exits.
            let kept = if *kept {
                ""
            } else {
                " (not kept: no library file)"
            };
            format!("marked {}{kept}", fmt_time(*at))
        }
        Outcome::MarkRemoved { at } => format!("removed mark at {}", fmt_time(*at)),
        Outcome::Labelled { at, label: Some(l) } => {
            format!("labelled the mark at {} \"{l}\"", fmt_time(*at))
        }
        Outcome::Labelled { at, label: None } => {
            format!("cleared the label of the mark at {}", fmt_time(*at))
        }
        Outcome::MarksExported {
            path,
            format,
            marks,
        } => format!(
            "wrote {} as {} to {}",
            count(*marks, "mark"),
            match format {
                playr_core::labels::MarkFile::Audacity => "Audacity labels",
                playr_core::labels::MarkFile::Cue => "a cue sheet",
            },
            home_as_tilde(path)
        ),
        Outcome::MarkMoved { from, to } => {
            format!("moved mark from {} to {}", fmt_time(*from), fmt_time(*to))
        }
        Outcome::MarksCleared => "marks cleared".into(),
        Outcome::AtMark { at } => format!("mark at {}", fmt_time(*at)),
        Outcome::PlanStarted => "planning slices".into(),
        Outcome::Planned { slices: n } => {
            format!("{} planned: enter writes, esc discards", slices(*n))
        }
        Outcome::ExportStarted => "exporting".into(),
        Outcome::Exported { dir, slices: n } => {
            format!("exported {} to {}", slices(*n), home_as_tilde(dir))
        }
        Outcome::ConvertStarted { format } => format!("converting to {format}"),
        Outcome::Converted {
            dir,
            parts,
            warnings,
        } => {
            // The loss first: a narrow status line cuts the end.
            let done = match parts {
                1 => format!("converted to {}", home_as_tilde(dir)),
                n => format!("converted to {} as {n} presets", home_as_tilde(dir)),
            };
            match warnings.as_slice() {
                [] => done,
                [one] => format!("ConvertWithMoss: {one} {done}"),
                [first, rest @ ..] => {
                    format!("ConvertWithMoss: {first} ({} more) {done}", rest.len())
                }
            }
        }
        Outcome::ScanStarted { dir: Some(dir) } => format!("scanning {}", home_as_tilde(dir)),
        Outcome::ScanStarted { dir: None } => "rescanning library".into(),
        Outcome::Scanning { seen, added } => format!("scanning: {seen} files, {added} added"),
        Outcome::AnalysisStarted { dir: None } => "analysing the library".into(),
        Outcome::AnalysisStarted { dir: Some(dir) } => {
            format!("analysing {}", home_as_tilde(dir))
        }
        Outcome::FindingTempo => "analysing the track for its tempo".into(),
        Outcome::TempoFixed { bpm, factor } => {
            let by = match *factor {
                1.0 => "as analysed".to_string(),
                f if f > 1.0 => format!("x{} the analysis", f as u32),
                f => format!("/{} the analysis", (1.0 / f) as u32),
            };
            format!("tempo {bpm:.1} BPM, {by}")
        }
        Outcome::Analysing { done, total } => format!("analysing: {done}/{total} tracks"),
        Outcome::Analysed { analysed: 0, .. } => {
            "nothing to analyse; every track is up to date".into()
        }
        Outcome::Analysed { analysed, failed } => {
            let failed = match failed {
                0 => String::new(),
                n => format!(", {n} unreadable"),
            };
            format!("analysed {analysed} tracks{failed}")
        }
        Outcome::Scanned { dir, report } => {
            let missing = match (report.missing, report.unavailable) {
                (0, _) => String::new(),
                (n, 0) => format!(", {n} missing"),
                // Said plainly: the tracks are counted missing, but the
                // directory could not be read, so they may well be there.
                (n, 1) => format!(", {n} missing, a directory unreadable"),
                (n, u) => format!(", {n} missing, {u} directories unreadable"),
            };
            let where_ = match dir {
                Some(dir) => home_as_tilde(dir),
                None => "library".into(),
            };
            format!(
                "scanned {where_}: {} added, {} unreadable{missing}; {} tracks",
                report.stats.added, report.stats.failed, report.total
            )
        }
        Outcome::PruneStarted { dir: Some(dir) } => {
            format!("pruning {}", home_as_tilde(dir))
        }
        Outcome::PruneStarted { dir: None } => "pruning library".into(),
        Outcome::Pruned { dir, removed } => {
            let where_ = match dir {
                Some(dir) => home_as_tilde(dir),
                None => "library".into(),
            };
            format!(
                "pruned {where_}: {} and {} of missing files",
                count(removed.tracks, "track"),
                count(removed.marks, "mark")
            )
        }
        Outcome::Forgot { dir, removed } => format!(
            "forgot {}: {} and {} removed",
            home_as_tilde(dir),
            count(removed.tracks, "track"),
            count(removed.marks, "mark")
        ),
        Outcome::Opening => "opening".into(),
        Outcome::Opened { tracks, skipped } => {
            let tracks = match tracks {
                1 => "1 track".to_string(),
                n => format!("{n} tracks"),
            };
            match skipped {
                0 => format!("playing {tracks}"),
                n => format!("playing {tracks}; {n} skipped"),
            }
        }
    }
}

fn refusal_text(refusal: &Refusal) -> String {
    match refusal {
        Refusal::NothingPlaying => "nothing is playing".into(),
        Refusal::NothingExported => "no slices written yet to convert; :slice first".into(),
        Refusal::ConvertOff => {
            ":convert is off; set convert-with-moss.enable = true under [extensions] in settings.toml"
                .into()
        }
        Refusal::NoConvertWithMoss(program) => playr_core::convertwithmoss::not_installed(program),
        Refusal::NoKit(dir) => format!(
            "no kit in {}; exports have one since playr 0.15",
            home_as_tilde(dir)
        ),
        Refusal::SelectionEmpty => "selection is empty".into(),
        Refusal::NoSearch => "no search results to save; search first".into(),
        Refusal::Sql(error) => format!("sql: {error}"),
        Refusal::NameReserved(name) => format!("\"{name}\" is kept for the selection's draft"),
        Refusal::QueueEmpty => "the queue is empty".into(),
        Refusal::NoLibraryFile => "no library to save to; `playr scan <dir>` creates one".into(),
        Refusal::NameEmpty => "playlist name cannot be empty".into(),
        Refusal::NameUnchanged => "name unchanged".into(),
        Refusal::NameTaken(name) => format!("a playlist named \"{name}\" already exists"),
        Refusal::WouldReplace(name) => format!("a playlist named \"{name}\" would be replaced"),
        Refusal::NoPlaylistNamed(name) => format!("no single playlist named \"{name}\""),
        Refusal::PlaylistEmpty => "playlist is empty".into(),
        Refusal::AlreadyMarked { at } => format!("already marked at {}", fmt_time(*at)),
        Refusal::NoMarks => "no marks in this track".into(),
        Refusal::NoTempo => "no clear pulse in this track to cut beats at".into(),
        Refusal::NoTempoToFix => "no tempo for this track to correct; :analyze it first".into(),
        Refusal::TempoFixLimit => "a tempo is corrected by at most 2 octaves either way".into(),
        Refusal::NoLoops => "no loops saved in this track".into(),
        Refusal::NoLaterMark => "no later mark".into(),
        Refusal::NoEarlierMark => "no earlier mark".into(),
        Refusal::NoLibraryPath => "no library file to scan into".into(),
        Refusal::NotADirectory(path) => format!("not a directory: {}", home_as_tilde(path)),
        Refusal::ScanRunning => "a scan or prune is already running".into(),
        Refusal::AnalysisRunning => "an analysis is already running".into(),
        Refusal::NoRoots => "no directories recorded; :scan DIR adds one".into(),
        Refusal::NoMarkHere => "no mark selected; { and } select one".into(),
        Refusal::MarkInTheWay { at } => {
            format!("a mark is already at {}", fmt_time(*at))
        }
        Refusal::NoOnsetNear => "no rise near that mark to snap to".into(),
        Refusal::NotARoot(path) => format!(
            "not one of the library's directories: {}; :roots lists them",
            home_as_tilde(path)
        ),
    }
}

/// `m:ss`, or `h:mm:ss` past an hour.
pub fn fmt_time(d: Duration) -> String {
    let total = d.as_secs();
    let (h, m, s) = (total / 3600, (total % 3600) / 60, total % 60);
    if h > 0 {
        format!("{h}:{m:02}:{s:02}")
    } else {
        format!("{m}:{s:02}")
    }
}

/// The question [`crate::model::Input::Draft`] asks, about a draft of `tracks`.
pub fn draft_question(tracks: usize) -> String {
    let tracks = match tracks {
        1 => "1 track".into(),
        n => format!("{n} tracks"),
    };
    format!("the draft from before holds {tracks}: overwrite it, append it, or save it as?")
}

/// A pending stop, for the status bar: after this track, on the sleep
/// timer's `left`, or both; `None` when neither is set.
pub fn stopping(after: bool, left: Option<Duration>) -> Option<String> {
    match (after, left) {
        (false, None) => None,
        (true, None) => Some("stop after track".into()),
        (false, Some(d)) => Some(format!("stop in {}", fmt_time(d))),
        (true, Some(d)) => Some(format!("stop after track, or in {}", fmt_time(d))),
    }
}

/// `path`, with the home directory shown as `~` to keep messages short.
/// Why a tape control does nothing, as its tooltip says.
pub fn idle_text(idle: crate::tape::Idle) -> &'static str {
    use crate::tape::Idle;
    match idle {
        Idle::VoiceOff => "no effect: the voice is off",
        Idle::WriteOff => "no effect: Write is off",
        Idle::NoSend => "no effect: Send is 0",
        Idle::Silent => "no effect: Level is 0",
        Idle::Muted => "no effect: muted",
        Idle::Flat => "no effect: the bell's gain is 0 dB",
        Idle::Still => "no effect: Rate is 0, so the head never wraps",
        Idle::NoRoom => "no effect: no audio either side of the window to crossfade into",
        Idle::Unchanging => {
            "nothing changes: Feedback is 1, Wear and Thin are 0 and every Send is 0"
        }
        Idle::Turns => "no effect: Ping turns the head at the edges, so it never wraps",
        Idle::NoFxSend => "no effect: nothing heard sends to this effect",
        Idle::Model => "no effect: the selected model has no such control",
    }
}

fn tape_text(m: &crate::tape::TapeMessage) -> String {
    use crate::tape::{Bounce, TapeAction, TapeMessage};
    let secs = |frames: f64, rate: u32| frames / f64::from(rate.max(1));
    match m {
        TapeMessage::Loading => "reading the tape".into(),
        TapeMessage::Loaded { frames, rate } => {
            format!(
                "tape loaded: {:.2} s; :tape play",
                secs(*frames as f64, *rate)
            )
        }
        TapeMessage::LoadedVoice {
            voice,
            frames,
            rate,
        } => format!("voice {voice} loaded: {:.2} s", secs(*frames as f64, *rate)),
        TapeMessage::NoSuchLoop => "that loop is no longer in the library".into(),
        TapeMessage::Done(TapeAction::Play) => "tape playing".into(),
        TapeMessage::Done(TapeAction::Stop) => "tape stopped".into(),
        TapeMessage::Done(TapeAction::Reset) => "tape reset to the loop as loaded".into(),
        TapeMessage::Done(TapeAction::Bounce(Some(Bounce::Pass))) => {
            "bouncing one pass of the mix into voice 4's loop".into()
        }
        TapeMessage::Done(TapeAction::Bounce(Some(Bounce::Long))) => {
            "bouncing the mix until stopped, up to 5 minutes; it then replaces voice 4's loop"
                .into()
        }
        TapeMessage::Done(TapeAction::Clear(_)) => {
            "the write loop cleared; :tape reset restores it".into()
        }
        TapeMessage::Done(TapeAction::Eject(n)) => format!("voice {n} ejected"),
        TapeMessage::Done(TapeAction::Share(n)) => {
            format!("voice {n} reads voice 4's loop, the write loop")
        }
        TapeMessage::Done(a) => command::line(&Action::Tape(*a), None),
        TapeMessage::NoTape => "no tape loaded; :tape load first".into(),
        TapeMessage::Empty(n) => format!("voice {n} is empty"),
        TapeMessage::OwnLoop(n) => {
            format!("voice {n} plays a loop of its own; clear empties only the write loop")
        }
        TapeMessage::Emptied => "every voice is empty; the tape is gone".into(),
        TapeMessage::Resumed { lost } if lost.is_empty() => "tape resumed".into(),
        TapeMessage::Resumed { lost } => {
            let names: Vec<String> = lost
                .iter()
                .map(|p| {
                    p.file_name()
                        .unwrap_or(p.as_os_str())
                        .to_string_lossy()
                        .into_owned()
                })
                .collect();
            format!(
                "tape resumed; these could not be read, so their voices are empty: {}",
                names.join(", ")
            )
        }
        TapeMessage::NoRange => "no range to load; set one, or :tape load N for a loop".into(),
        TapeMessage::EmptySlot(n) => format!("loop {n} is empty"),
        TapeMessage::EmptyWindow => {
            "the window is empty; its start must come before its end".into()
        }
        TapeMessage::AlreadySaving => "the tape is already being saved".into(),
        TapeMessage::SaveAborted => "the save was cut short; save again".into(),
        TapeMessage::DeviceLost(e) => format!("audio device lost: {e}; load the tape again"),
        TapeMessage::Took => "the tape took over from the player".into(),
        TapeMessage::TakeWaiting => "the tape takes over at the range's start".into(),
        TapeMessage::PastRange => "the player is past the range; the tape is loaded".into(),
        TapeMessage::TakeDropped => "the player left the track; the tape is loaded".into(),
        TapeMessage::Saving => "saving the tape".into(),
        TapeMessage::Bounced => "bounced the mix into voice 4's loop; Save loop keeps it".into(),
        TapeMessage::NothingBounced => "nothing bounced: the tape was stopped".into(),
        TapeMessage::RecorderBusy => {
            "one recording at a time: stop Record or the bounce until stopped first".into()
        }
        TapeMessage::Saved(path) => format!("saved the tape to {}", home_as_tilde(path)),
        TapeMessage::Recording(path) => format!("recording the tape to {}", home_as_tilde(path)),
        TapeMessage::Recorded {
            path,
            frames,
            rate,
            dropped,
        } => {
            let done = format!(
                "recorded {:.2} s to {}",
                secs(*frames as f64, *rate),
                home_as_tilde(path)
            );
            match dropped {
                0 => done,
                n => format!("{done}; {n} blocks lost, the disk was too slow"),
            }
        }
        TapeMessage::Failed(e) => format!("tape: {e}"),
    }
}

fn mix_text(mix: &crate::mix::Mix) -> String {
    use crate::mix::Strip;
    let strips: Vec<String> = Strip::ALL
        .into_iter()
        .map(|s| {
            let p = (mix.level(s) * 100.0).round();
            let db = match mix.law().gain(mix.level(s)) {
                0.0 => "off".to_string(),
                g => format!("{:.1} dB", 20.0 * g.log10()),
            };
            let muted = if mix.muted(s) { ", muted" } else { "" };
            format!("{} {p}% ({db}{muted})", s.name())
        })
        .collect();
    format!("{}; law {}", strips.join(", "), mix.law().name())
}

fn dj_text(m: &crate::dj::DjMessage) -> String {
    use crate::dj::{DjAction, DjMessage};
    let deck = |s: &crate::dj::Side| crate::command::side_name(*s);
    match m {
        DjMessage::Loading(s) => format!("reading the track for deck {}", deck(s)),
        DjMessage::Loaded {
            side,
            title,
            bpm: Some(bpm),
        } => format!("deck {}: {title}, {bpm:.2} BPM", deck(side)),
        DjMessage::Loaded {
            side,
            title,
            bpm: None,
        } => format!("deck {}: {title}, no beat grid", deck(side)),
        DjMessage::FindingGrid(s) => {
            format!("deck {}: analysing the track for its beat grid", deck(s))
        }
        DjMessage::NoGrid(s) => format!(
            "deck {} has no beat grid; :dj {} tap sets one",
            deck(s),
            deck(s)
        ),
        DjMessage::Done(DjAction::Play(s)) => format!("deck {} playing", deck(s)),
        DjMessage::Done(DjAction::Pause(s)) => format!("deck {} paused", deck(s)),
        DjMessage::Done(a) => command::line(&Action::Dj(*a), None),
        DjMessage::Empty(s) => format!("deck {} is empty; :dj {} load first", deck(s), deck(s)),
        DjMessage::NoTrack => "no track under the cursor to load".into(),
        DjMessage::Playing(s) => format!("deck {} is playing; pause it first", deck(s)),
        DjMessage::SpeedOutOfReach(cents) => format!(
            "a deck reaches +7 semitones at most; the player is at {}",
            semitones(*cents)
        ),
        DjMessage::Took(s) => format!("deck {} took over from the player", deck(s)),
        DjMessage::OutOfReach(s) => format!(
            "deck {} cannot reach the other deck's tempo within 50%",
            deck(s)
        ),
        DjMessage::NoMark(s) => format!("deck {}: no mark that way", deck(s)),
        DjMessage::Queued { side, title } => {
            format!("deck {}: {title} loads when the deck stops", deck(side))
        }
        DjMessage::NoCueChannels(n) => {
            format!("the output device has {n} channels; the cue on 3 and 4 needs 4")
        }
        DjMessage::Tapped(s) => format!("deck {}: tap again on the beat", deck(s)),
        DjMessage::Grid { side, bpm, t0 } => {
            format!(
                "deck {} grid: {bpm:.2} BPM, a beat at {t0:.3} s",
                deck(side)
            )
        }
        DjMessage::DeviceLost(e) => format!("audio device lost: {e}; load the decks again"),
        DjMessage::Failed(e) => format!("dj: {e}"),
    }
}

pub fn home_as_tilde(path: &Path) -> String {
    let home = std::env::home_dir();
    match home.and_then(|h| path.strip_prefix(h).ok().map(|rest| rest.to_path_buf())) {
        Some(rest) => format!("~/{}", rest.display()),
        None => path.display().to_string(),
    }
}

/// `n` of `thing`, as `1 mark` or `3 marks`.
fn count(n: usize, thing: &str) -> String {
    match n {
        1 => format!("1 {thing}"),
        n => format!("{n} {thing}s"),
    }
}

/// The ReplayGain applied, as the bottom line shows it: `rg -6.2 dB`.
/// The tone control's bands away from 0: `eq bass +2 treble -3.5`, or `eq flat`.
pub fn eq(gains: [f32; 3]) -> String {
    let bands: Vec<String> = playr_core::audio::eq::Band::ALL
        .iter()
        .zip(gains)
        .filter(|(_, db)| *db != 0.0)
        .map(|(band, db)| format!("{} {:+}", band.name(), (db * 10.0).round() / 10.0))
        .collect();
    match bands.is_empty() {
        true => "eq flat".into(),
        false => format!("eq {}", bands.join(" ")),
    }
}

pub fn replaygain(db: f32) -> String {
    // Rounding to one decimal would show a tiny cut as -0.0.
    let db = if db.abs() < 0.05 { 0.0 } else { db };
    format!("rg {db:+.1} dB")
}

/// What a [`Finding`] says beyond its name, for a report or a dialog.
pub fn finding_detail(f: &Finding) -> String {
    match f {
        Finding::Unreadable(e) => e.clone(),
        Finding::Damaged { skipped, md5_bad } => match (skipped, md5_bad) {
            (0, _) => "audio does not match its MD5".into(),
            (n, false) => format!("{n} packets skipped"),
            (n, true) => format!("{n} packets skipped; audio does not match its MD5"),
        },
        Finding::NoChecksum => "no MD5 in the FLAC header".into(),
        Finding::WrongLength { decoded, header } => {
            format!("{decoded} frames decoded, header says {header}")
        }
        Finding::Padded { used, bits } => format!("{used} of {bits} bits used"),
        Finding::PossibleLossySource { hz } | Finding::PossibleUpsampling { hz } => {
            format!("content stops at {:.1} kHz", *hz as f32 / 1000.0)
        }
    }
}

/// The rows `:info` shows for `track`: what the library knows from its tags,
/// then what `playr analyze` measured, then the problems it found.
///
/// `analysis` is `None` until the track is analysed, or once the file has
/// changed since; `gains` is what ReplayGain would apply.
pub fn info_rows(
    track: &Track,
    analysis: Option<&Analysis>,
    gains: Gains,
) -> Vec<(String, String)> {
    let mut rows = vec![("file".into(), home_as_tilde(Path::new(&track.path)))];
    if let Some(album) = &track.album {
        rows.push(("album".into(), album.clone()));
    }
    let mut format = String::new();
    if let Some(rate) = track.sample_rate {
        format.push_str(&format!("{:.1} kHz", f64::from(rate) / 1000.0));
    }
    if let Some(channels) = track.channels {
        format.push_str(&format!(", {channels} ch"));
    }
    if let Some(bits) = track.bit_depth {
        format.push_str(&format!(", {bits} bit"));
    }
    if let Some(ms) = track.duration_ms {
        format.push_str(&format!(", {}", fmt_time(Duration::from_millis(ms as u64))));
    }
    rows.push(("format".into(), format.trim_start_matches(", ").to_string()));

    let Some(a) = analysis else {
        rows.push(("measured".into(), "not yet; :analyze measures it".into()));
        return rows;
    };

    match a.loudness {
        Some(lufs) => rows.push(("loudness".into(), format!("{lufs:.1} LUFS"))),
        None => rows.push(("loudness".into(), "silent".into())),
    }
    if let Some(peak) = a.peak {
        let dbfs = match peak > 0.0 {
            true => format!("{:.1} dBFS", 20.0 * peak.log10()),
            false => "silent".into(),
        };
        rows.push(("peak".into(), format!("{peak:.3} ({dbfs})")));
    }
    let gain = |name: &str, g: Option<playr_core::gain::Gain>| {
        let g = g?;
        let capped = match g.linear() < 10f32.powf(g.db / 20.0) - 1e-6 {
            true => ", held back by the peak",
            false => "",
        };
        Some((name.to_string(), format!("{:+.1} dB{capped}", g.db)))
    };
    rows.extend(gain("track gain", gains.track));
    rows.extend(gain("album gain", gains.album));

    let tempo = match (a.bpm_tag, a.tempo) {
        (Some(tag), _) => format!("{tag:.0} BPM, from the file's tag"),
        (None, Some(t)) if t.confidence >= tempo::MIN_CONFIDENCE => {
            let alt = match t.alt {
                Some(alt) => format!(", or {alt:.0} BPM"),
                None => String::new(),
            };
            format!("{:.0} BPM{alt} (confidence {:.2})", t.bpm, t.confidence)
        }
        (None, Some(t)) => format!("no clear pulse (confidence {:.2})", t.confidence),
        (None, None) => "too short to measure".into(),
    };
    rows.push(("tempo".into(), tempo));

    if let Some(c) = a.cutoff {
        rows.push((
            "content to".into(),
            format!(
                "{:.1} kHz, falling {:.0} dB",
                c.hz as f32 / 1000.0,
                c.fall_db
            ),
        ));
    }
    if let (Some(used), Some(bits)) = (a.bits_used, a.bits) {
        rows.push(("bits used".into(), format!("{used} of {bits}")));
    }
    if let Some(md5) = a.md5 {
        let says = match md5 {
            Md5::Ok => "matches the FLAC header",
            Md5::Bad => "does not match the FLAC header",
            Md5::Absent => "none in the FLAC header",
        };
        rows.push(("checksum".into(), says.into()));
    }
    for finding in analysis::findings(a) {
        rows.push((finding.name().into(), finding_detail(&finding)));
    }
    rows
}

/// The last part of `path`, or all of it when it has none.
fn file_name(path: &Path) -> String {
    path.file_name().map_or_else(
        || path.display().to_string(),
        |n| n.to_string_lossy().into_owned(),
    )
}

/// A speed shift of `cents` in signed semitones, to a hundredth: `+3`, `-0.5`.
pub fn semitones(cents: i32) -> String {
    let sign = if cents < 0 { "-" } else { "+" };
    let (whole, part) = (cents.abs() / 100, cents.abs() % 100);
    match part {
        0 => format!("{sign}{whole}"),
        p if p % 10 == 0 => format!("{sign}{whole}.{}", p / 10),
        p => format!("{sign}{whole}.{p:02}"),
    }
}