perfectstar2k 0.4.0

A modern TUI homage to WordStar & WordPerfect for DOS — a real writing tool built on WordStar's touch-typist command language and long-hand-page metaphor.
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
//! The central command table: every command's name and chord in one place.
//! The key dispatcher, the delayed prefix menus, the command palette, and the
//! help screen are all generated from this table.

/// A prefix key (^K, ^Q, ^O, ^P) awaiting its second key.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Prefix {
    K,
    Q,
    O,
    P,
}

impl Prefix {
    pub fn label(self) -> &'static str {
        match self {
            Prefix::K => "^K Block & File",
            Prefix::Q => "^Q Quick",
            Prefix::O => "^O Onscreen",
            Prefix::P => "^P Project",
        }
    }
}

/// Every editor command.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Cmd {
    // Movement — the diamond and friends
    Up,
    Down,
    Left,
    Right,
    WordLeft,
    WordRight,
    ScrollUp,
    ScrollDown,
    PageUp,
    PageDown,
    // Movement — ^Q quick
    LineStart,
    LineEnd,
    ScreenTop,
    ScreenBottom,
    DocStart,
    DocEnd,
    SentenceBack,
    SentenceFwd,
    ParaBack,
    ParaFwd,
    PrevPosition,
    Outline,
    NextMisspelling,
    // Editing
    DeleteRight,
    DeleteLeft,
    DeleteWordRight,
    DeleteLine,
    DeleteToLineEnd,
    InsertBlankLine,
    TransposeWords,
    TransposeChars,
    Undo,
    // Search
    FindIncremental,
    FindReplace,
    FindNext,
    // Macros & palette
    MacroRecord,
    MacroPlay,
    Palette,
    // Blocks & kill ring
    BlockBegin,
    BlockEnd,
    BlockCopy,
    BlockMove,
    BlockDelete,
    BlockWrite,
    BlockRead,
    BlockHide,
    BlockPrev,
    Put,
    CopyFromOther,
    JumpBlockBegin,
    JumpBlockEnd,
    JumpBlockSource,
    // File
    Save,
    SaveExit,
    Quit,
    ExportClean,
    ExportManuscript,
    ExportDocx,
    ExportEpub,
    ExportHtml,
    ExportProjectDocx,
    ExportProjectEpub,
    ExportProjectHtml,
    // Onscreen
    CycleTheme,
    RevealCodes,
    ToggleWrap,
    SetWrapMargin,
    ToggleInsert,
    CycleHelpLevel,
    ToggleSpellcheck,
    AddToDictionary,
    ToggleTypewriter,
    OtherWindow,
    // Project
    ProjectNew,
    ProjectOpen,
    BinderToggle,
    BinderMoveUp,
    BinderMoveDown,
    ProjectAddDoc,
    ProjectRemoveDoc,
    /// Toggle always-on word count display (R2.1).
    WordCount,
    /// Set a session writing goal (R2.3).
    SetGoal,
    /// Show daily writing history overlay (R2.5).
    StatsOverlay,
    /// Search across all project documents (R6.1).
    ProjectFind,
    /// Replace across all project documents (R6.3).
    ProjectReplace,
}

/// How a command is typed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Chord {
    /// Ctrl+letter, no prefix.
    Bare(char),
    /// Prefix then a second key (typed plain or with Ctrl held).
    Pref(Prefix, char),
}

pub struct Binding {
    pub cmd: Cmd,
    pub chord: Chord,
    pub name: &'static str,
}

use Chord::{Bare, Pref};
use Prefix::{K, O, P, Q};

pub const BINDINGS: &[Binding] = &[
    // The diamond
    Binding {
        cmd: Cmd::Up,
        chord: Bare('e'),
        name: "cursor up",
    },
    Binding {
        cmd: Cmd::Down,
        chord: Bare('x'),
        name: "cursor down",
    },
    Binding {
        cmd: Cmd::Left,
        chord: Bare('s'),
        name: "cursor left",
    },
    Binding {
        cmd: Cmd::Right,
        chord: Bare('d'),
        name: "cursor right",
    },
    Binding {
        cmd: Cmd::WordLeft,
        chord: Bare('a'),
        name: "word left",
    },
    Binding {
        cmd: Cmd::WordRight,
        chord: Bare('f'),
        name: "word right",
    },
    Binding {
        cmd: Cmd::ScrollUp,
        chord: Bare('w'),
        name: "scroll up",
    },
    Binding {
        cmd: Cmd::ScrollDown,
        chord: Bare('z'),
        name: "scroll down",
    },
    Binding {
        cmd: Cmd::PageUp,
        chord: Bare('r'),
        name: "page up",
    },
    Binding {
        cmd: Cmd::PageDown,
        chord: Bare('c'),
        name: "page down",
    },
    // Editing
    Binding {
        cmd: Cmd::DeleteRight,
        chord: Bare('g'),
        name: "delete char",
    },
    Binding {
        cmd: Cmd::DeleteLeft,
        chord: Bare('h'),
        name: "delete left",
    },
    Binding {
        cmd: Cmd::DeleteWordRight,
        chord: Bare('t'),
        name: "delete word",
    },
    Binding {
        cmd: Cmd::DeleteLine,
        chord: Bare('y'),
        name: "delete line",
    },
    Binding {
        cmd: Cmd::InsertBlankLine,
        chord: Bare('n'),
        name: "insert line",
    },
    Binding {
        cmd: Cmd::Undo,
        chord: Bare('u'),
        name: "undo",
    },
    Binding {
        cmd: Cmd::FindNext,
        chord: Bare('l'),
        name: "find next",
    },
    Binding {
        cmd: Cmd::ToggleInsert,
        chord: Bare('v'),
        name: "insert/overtype",
    },
    // ^Q Quick
    Binding {
        cmd: Cmd::LineStart,
        chord: Pref(Q, 's'),
        name: "line start",
    },
    Binding {
        cmd: Cmd::LineEnd,
        chord: Pref(Q, 'd'),
        name: "line end",
    },
    Binding {
        cmd: Cmd::ScreenTop,
        chord: Pref(Q, 'e'),
        name: "screen top",
    },
    Binding {
        cmd: Cmd::ScreenBottom,
        chord: Pref(Q, 'x'),
        name: "screen bottom",
    },
    Binding {
        cmd: Cmd::DocStart,
        chord: Pref(Q, 'r'),
        name: "document top",
    },
    Binding {
        cmd: Cmd::DocEnd,
        chord: Pref(Q, 'c'),
        name: "document end",
    },
    Binding {
        cmd: Cmd::SentenceBack,
        chord: Pref(Q, ','),
        name: "sentence back",
    },
    Binding {
        cmd: Cmd::SentenceFwd,
        chord: Pref(Q, '.'),
        name: "sentence forward",
    },
    Binding {
        cmd: Cmd::ParaBack,
        chord: Pref(Q, '['),
        name: "paragraph back",
    },
    Binding {
        cmd: Cmd::ParaFwd,
        chord: Pref(Q, ']'),
        name: "paragraph forward",
    },
    Binding {
        cmd: Cmd::PrevPosition,
        chord: Pref(Q, 'p'),
        name: "previous position",
    },
    Binding {
        cmd: Cmd::Outline,
        chord: Pref(Q, 'o'),
        name: "outline / headings",
    },
    Binding {
        cmd: Cmd::NextMisspelling,
        chord: Pref(Q, 'n'),
        name: "next misspelling",
    },
    Binding {
        cmd: Cmd::FindIncremental,
        chord: Pref(Q, 'f'),
        name: "find",
    },
    Binding {
        cmd: Cmd::FindReplace,
        chord: Pref(Q, 'a'),
        name: "find & replace",
    },
    Binding {
        cmd: Cmd::DeleteToLineEnd,
        chord: Pref(Q, 'y'),
        name: "delete to line end",
    },
    Binding {
        cmd: Cmd::TransposeWords,
        chord: Pref(Q, 't'),
        name: "transpose words",
    },
    Binding {
        cmd: Cmd::TransposeChars,
        chord: Pref(Q, 'g'),
        name: "transpose chars",
    },
    Binding {
        cmd: Cmd::MacroRecord,
        chord: Pref(Q, 'm'),
        name: "record macro",
    },
    Binding {
        cmd: Cmd::MacroPlay,
        chord: Pref(Q, 'j'),
        name: "play macro",
    },
    Binding {
        cmd: Cmd::JumpBlockBegin,
        chord: Pref(Q, 'b'),
        name: "to block begin",
    },
    Binding {
        cmd: Cmd::JumpBlockEnd,
        chord: Pref(Q, 'k'),
        name: "to block end",
    },
    Binding {
        cmd: Cmd::JumpBlockSource,
        chord: Pref(Q, 'v'),
        name: "to block source",
    },
    // ^K Block & File
    Binding {
        cmd: Cmd::BlockBegin,
        chord: Pref(K, 'b'),
        name: "mark block begin",
    },
    Binding {
        cmd: Cmd::BlockEnd,
        chord: Pref(K, 'k'),
        name: "mark block end",
    },
    Binding {
        cmd: Cmd::BlockCopy,
        chord: Pref(K, 'c'),
        name: "copy block here",
    },
    Binding {
        cmd: Cmd::BlockMove,
        chord: Pref(K, 'v'),
        name: "move block here",
    },
    Binding {
        cmd: Cmd::BlockDelete,
        chord: Pref(K, 'y'),
        name: "delete block",
    },
    Binding {
        cmd: Cmd::BlockWrite,
        chord: Pref(K, 'w'),
        name: "write block to file",
    },
    Binding {
        cmd: Cmd::BlockRead,
        chord: Pref(K, 'r'),
        name: "read file here",
    },
    Binding {
        cmd: Cmd::BlockHide,
        chord: Pref(K, 'h'),
        name: "hide/show block",
    },
    Binding {
        cmd: Cmd::BlockPrev,
        chord: Pref(K, 'u'),
        name: "previous block",
    },
    Binding {
        cmd: Cmd::Put,
        chord: Pref(K, 'p'),
        name: "put (cycle clippings)",
    },
    Binding {
        cmd: Cmd::CopyFromOther,
        chord: Pref(K, 'a'),
        name: "copy block from other window",
    },
    Binding {
        cmd: Cmd::Save,
        chord: Pref(K, 'd'),
        name: "save",
    },
    Binding {
        cmd: Cmd::Save,
        chord: Pref(K, 's'),
        name: "save",
    },
    Binding {
        cmd: Cmd::SaveExit,
        chord: Pref(K, 'x'),
        name: "save & exit",
    },
    Binding {
        cmd: Cmd::Quit,
        chord: Pref(K, 'q'),
        name: "quit",
    },
    Binding {
        cmd: Cmd::ExportClean,
        chord: Pref(K, 'e'),
        name: "export (strip notes)",
    },
    Binding {
        cmd: Cmd::ExportManuscript,
        chord: Pref(K, 'm'),
        name: "export manuscript RTF",
    },
    Binding {
        cmd: Cmd::ExportDocx,
        chord: Pref(K, 'j'),
        name: "export DOCX",
    },
    Binding {
        cmd: Cmd::ExportEpub,
        chord: Pref(K, 'g'),
        name: "export EPUB",
    },
    Binding {
        cmd: Cmd::ExportHtml,
        chord: Pref(K, 'l'),
        name: "export HTML",
    },
    // ^O Onscreen
    Binding {
        cmd: Cmd::CycleTheme,
        chord: Pref(O, 'b'),
        name: "cycle theme",
    },
    Binding {
        cmd: Cmd::RevealCodes,
        chord: Pref(O, 'd'),
        name: "reveal codes",
    },
    Binding {
        cmd: Cmd::ToggleWrap,
        chord: Pref(O, 'w'),
        name: "word wrap on/off",
    },
    Binding {
        cmd: Cmd::SetWrapMargin,
        chord: Pref(O, 'r'),
        name: "set wrap margin",
    },
    Binding {
        cmd: Cmd::CycleHelpLevel,
        chord: Pref(O, 'h'),
        name: "cycle help level",
    },
    Binding {
        cmd: Cmd::ToggleSpellcheck,
        chord: Pref(O, 's'),
        name: "spellcheck on/off",
    },
    Binding {
        cmd: Cmd::AddToDictionary,
        chord: Pref(O, 'a'),
        name: "add word to dictionary",
    },
    Binding {
        cmd: Cmd::ToggleTypewriter,
        chord: Pref(O, 't'),
        name: "typewriter scrolling on/off",
    },
    Binding {
        cmd: Cmd::OtherWindow,
        chord: Pref(O, 'k'),
        name: "other window (open/switch)",
    },
    Binding {
        cmd: Cmd::WordCount,
        chord: Pref(O, 'c'),
        name: "toggle word count",
    },
    Binding {
        cmd: Cmd::SetGoal,
        chord: Pref(O, 'g'),
        name: "set session goal",
    },
    Binding {
        cmd: Cmd::StatsOverlay,
        chord: Pref(O, 'i'),
        name: "writing stats",
    },
    // ^P Project prefix
    Binding {
        cmd: Cmd::ProjectNew,
        chord: Pref(P, 'n'),
        name: "new project",
    },
    Binding {
        cmd: Cmd::ProjectOpen,
        chord: Pref(P, 'p'),
        name: "open project",
    },
    Binding {
        cmd: Cmd::BinderToggle,
        chord: Pref(P, 'b'),
        name: "toggle binder",
    },
    Binding {
        cmd: Cmd::BinderMoveUp,
        chord: Pref(P, 'e'),
        name: "binder: move doc up",
    },
    Binding {
        cmd: Cmd::BinderMoveDown,
        chord: Pref(P, 'x'),
        name: "binder: move doc down",
    },
    Binding {
        cmd: Cmd::ProjectAddDoc,
        chord: Pref(P, 'a'),
        name: "add document to project",
    },
    Binding {
        cmd: Cmd::ProjectRemoveDoc,
        chord: Pref(P, 'r'),
        name: "remove document from project",
    },
    Binding {
        cmd: Cmd::ExportProjectDocx,
        chord: Pref(P, 'd'),
        name: "export project DOCX",
    },
    Binding {
        cmd: Cmd::ExportProjectEpub,
        chord: Pref(P, 'f'),
        name: "export project EPUB",
    },
    Binding {
        cmd: Cmd::ExportProjectHtml,
        chord: Pref(P, 'h'),
        name: "export project HTML",
    },
    Binding {
        cmd: Cmd::ProjectFind,
        chord: Pref(P, 's'),
        name: "project search",
    },
    Binding {
        cmd: Cmd::ProjectReplace,
        chord: Pref(P, 'w'),
        name: "project replace",
    },
];

/// Human-readable chord for display in menus, the palette, and help.
pub fn chord_label(chord: Chord) -> String {
    match chord {
        Bare(c) => format!("^{}", c.to_ascii_uppercase()),
        Pref(p, c) => {
            let p = match p {
                K => 'K',
                Q => 'Q',
                O => 'O',
                P => 'P',
            };
            format!("^{p}{}", c.to_ascii_uppercase())
        }
    }
}

/// Palette entries whose names contain `query` (case-insensitive).
pub fn filtered_entries(query: &str) -> Vec<(Cmd, &'static str, String)> {
    let q = query.to_lowercase();
    palette_entries()
        .into_iter()
        .filter(|(_, name, chord)| {
            q.is_empty() || name.to_lowercase().contains(&q) || chord.to_lowercase().contains(&q)
        })
        .collect()
}

/// All commands for the palette/help, deduplicated, in table order.
pub fn palette_entries() -> Vec<(Cmd, &'static str, String)> {
    let mut seen = Vec::new();
    let mut out = Vec::new();
    for b in BINDINGS {
        if !seen.contains(&b.cmd) {
            seen.push(b.cmd);
            out.push((b.cmd, b.name, chord_label(b.chord)));
        }
    }
    out
}

pub fn lookup_bare(c: char) -> Option<Cmd> {
    BINDINGS.iter().find_map(|b| match b.chord {
        Bare(k) if k == c => Some(b.cmd),
        _ => None,
    })
}

pub fn lookup_prefixed(prefix: Prefix, c: char) -> Option<Cmd> {
    BINDINGS.iter().find_map(|b| match b.chord {
        Pref(p, k) if p == prefix && k == c => Some(b.cmd),
        _ => None,
    })
}

/// Menu entries for a prefix, in table order, deduplicated by command.
pub fn menu_entries(prefix: Prefix) -> Vec<(char, &'static str)> {
    let mut seen = Vec::new();
    let mut out = Vec::new();
    for b in BINDINGS {
        if let Pref(p, k) = b.chord {
            if p == prefix && !seen.contains(&b.cmd) {
                seen.push(b.cmd);
                out.push((k, b.name));
            }
        }
    }
    out
}

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

    #[test]
    fn project_prefix_has_a_label() {
        assert_eq!(Prefix::P.label(), "^P Project");
    }

    #[test]
    fn project_prefix_has_commands() {
        // Task 1.2: ^PP opens a project manifest. The menu should show it.
        assert!(!menu_entries(Prefix::P).is_empty());
        assert_eq!(lookup_prefixed(Prefix::P, 'p'), Some(Cmd::ProjectOpen));
    }

    #[test]
    fn existing_prefixes_still_populated() {
        // Guard against the new prefix accidentally shadowing the others.
        assert!(!menu_entries(Prefix::K).is_empty());
        assert!(!menu_entries(Prefix::Q).is_empty());
        assert!(!menu_entries(Prefix::O).is_empty());
    }

    #[test]
    fn chord_label_renders_p_prefix() {
        assert_eq!(chord_label(Pref(P, 'b')), "^PB");
    }

    #[test]
    fn palette_entries_are_deduplicated() {
        // Save is bound to both ^KD and ^KS; it must appear once.
        let saves = palette_entries()
            .into_iter()
            .filter(|(cmd, _, _)| *cmd == Cmd::Save)
            .count();
        assert_eq!(saves, 1);
    }
}