ghtui 0.2.0

A keyboard-driven terminal client for GitHub, with a pull request diff viewer that goes further than GitHub's
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
//! Your gists: making one, changing one (its description and files),
//! and deleting one, as gist.github.com does.
//!
//! A new gist's form is a page (`gists::NewGist`), as is an edit's
//! (`gists::EditGist`): the description, then each file's name and
//! text, written in the composer. Making one asks whether it's secret or
//! public, as GitHub's two buttons do; GitHub's API can't change that
//! afterwards. Deleting asks first. Then the gist's page (or, once it's
//! deleted, your gists) follows GitHub until it shows the change.

use std::borrow::Cow;

use crate::api::browse::Gist;
use crate::api::gist::{FileChange, GistChange, MadeGist, NewGist};
use crate::route::{Route, app, gists, people};
use crate::ui::form::Row;
use crate::ui::page::{Link, Page};
use crate::ui::pages::{FormRow, Mark, TextBox, WriteFormView};

use crate::data::DataKey;
use crate::follow::{Expectation, Shown};
use crate::forms::Answer;
use crate::forms::{Fields, filling, say};
use crate::keymap::Action;
use crate::nav::{Doable, row};
use crate::picker::entry;
use crate::state::{Cmd, State};
use crate::work::{self, Cx, Failure, Job};

/// A gist being made or changed.
#[derive(Debug, Clone, Default)]
pub struct Form {
    /// What's changed, once it's loaded: its ID and owner.
    pub gist: Option<(String, String)>,
    /// Whether the gist changed is public.
    pub public: bool,
    pub description: String,
    pub files: Vec<File>,
    /// The files GitHub has that were removed, by name.
    pub removed: Vec<String>,
    /// Filled in (from the gist changed), and the composer opened once.
    pub started: bool,
}

impl Fields for Form {
    fn text(&mut self, part: usize) -> Option<&mut String> {
        Some(&mut self.files.get_mut(part)?.text)
    }

    fn title(&self, _: &Route, part: usize) -> String {
        let name = self.files.get(part).map(|f| f.name.as_str());
        match name.filter(|n| !n.is_empty()) {
            Some(name) => format!("Gist file {name}"),
            None => format!("Gist file {} (name it on the form)", part + 1),
        }
    }

    fn save(&self, route: &Route) -> &'static str {
        match route {
            Route::Gists(gists::NewGist) => "create gist…",
            _ => "save gist",
        }
    }
}

/// One of a gist's files, as written.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct File {
    pub name: String,
    pub text: String,
    /// Its name and text on GitHub, for a file it has.
    pub was: Option<(String, String)>,
    /// Why it can't be changed here, if it can't.
    pub locked: Option<String>,
}

/// Making the gist written on the form at `route`.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Create {
    pub route: Route,
    pub new: NewGist,
}

/// Changing a gist as its form at `route` has it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Update {
    pub route: Route,
    pub change: GistChange,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Delete {
    pub id: String,
    pub owner: String,
}

/// The gist form on screen (blank until it's started), and its page.
fn here(state: &State) -> Option<(Route, Cow<'_, Form>)> {
    let route = state.route().filter(|r| {
        matches!(
            r,
            Route::Gists(gists::NewGist) | Route::Gists(gists::EditGist { .. })
        )
    })?;
    Some((route.clone(), state.forms.shown(route)))
}

/// What gists add: making one, and changing and deleting yours on its
/// page.
pub const FEATURE: crate::features::Feature = crate::features::Feature {
    owns: &[Action::NewGist],
    takes: |state, action| {
        matches!(action, Action::EditComment | Action::DeleteComment)
            && matches!(state.route(), Some(Route::Gists(gists::Gist { .. })))
    },
    act: |state, action| match action {
        Action::NewGist => open_new(state),
        Action::EditComment => open_edit(state),
        _ => ask_delete(state),
    },
    offers: doables,
    ..crate::features::Feature::NONE
};

/// The gist on screen, as loaded.
fn shown(state: &State) -> Option<&Gist> {
    let Some(Route::Gists(gists::Gist { id, .. })) = state.route() else {
        return None;
    };
    state.picked::<Gist>(&DataKey::Gist(id.clone()))
}

/// Why the gist on screen isn't yours to change, if it isn't.
fn not_yours(state: &State, g: &Gist) -> Option<String> {
    match (&g.owner, &state.viewer) {
        (Some(owner), Some(me)) if owner.eq_ignore_ascii_case(me) => None,
        (Some(owner), _) => Some(format!("It's @{owner}'s gist: only they can change it")),
        (None, _) => Some("It has no owner GitHub names: no one can change it".into()),
    }
}

/// `,G`: a new gist's page, with what was written there before.
#[must_use]
pub fn open_new(state: &mut State) -> Vec<Cmd> {
    if state.route() == Some(&Route::Gists(gists::NewGist)) {
        return compose(state, 0);
    }
    let mut cmds = state.push(Route::Gists(gists::NewGist));
    cmds.extend(follow(state));
    cmds
}

/// `,e` on your gist: its form, filled in from it.
#[must_use]
pub fn open_edit(state: &mut State) -> Vec<Cmd> {
    let Some(g) = shown(state) else {
        return state.just_say("The gist hasn't loaded yet");
    };
    if let Some(why) = not_yours(state, g) {
        return state.just_say(why);
    }
    let route = Route::Gists(gists::EditGist {
        owner: g.owner.clone().unwrap_or_default(),
        id: g.id.clone(),
    });
    // Started again from what GitHub has now.
    state.forms.take::<Form>(&route);
    let mut cmds = state.push(route);
    cmds.extend(follow(state));
    cmds
}

/// Brings the form on screen up to date: an edit's filled in from the
/// gist once it's loaded, then the composer opens on its first file.
#[must_use]
fn follow(state: &mut State) -> Vec<Cmd> {
    let Some((route, form)) = here(state) else {
        return Vec::new();
    };
    if form.started {
        return Vec::new();
    }
    match &route {
        Route::Gists(gists::EditGist { id, .. }) => {
            let Some(g) = state.picked::<Gist>(&DataKey::Gist(id.clone())).cloned() else {
                return Vec::new();
            };
            if not_yours(state, &g).is_some() {
                return Vec::new();
            }
            *state.forms.edit(&route) = Form {
                gist: Some((g.id.clone(), g.owner.clone().unwrap_or_default())),
                public: g.public,
                description: g.description.clone(),
                files: (g.files.iter()).map(file_of).collect(),
                started: true,
                ..Form::default()
            };
            Vec::new()
        }
        _ => {
            let form = state.forms.edit::<Form>(&route);
            form.started = true;
            if form.files.is_empty() {
                form.files.push(File::default());
            }
            if state.overlay.is_none() {
                return compose(state, 0);
            }
            Vec::new()
        }
    }
}

/// A file of the gist, to change.
fn file_of(f: &crate::api::browse::GistFile) -> File {
    let locked = match &f.text {
        Some(_) if !f.truncated => None,
        _ => Some("Too big for GitHub's API to send whole: change it on github.com".to_owned()),
    };
    let text = f.text.clone().unwrap_or_default();
    File {
        name: f.name.clone(),
        was: Some((f.name.clone(), text.clone())),
        text,
        locked,
    }
}

/// The composer, on file `i` of the form on screen.
#[must_use]
fn compose(state: &mut State, i: usize) -> Vec<Cmd> {
    let Some((route, form)) = here(state) else {
        return Vec::new();
    };
    if let Some(why) = form.files.get(i).and_then(|f| f.locked.clone()) {
        return state.just_say(why);
    }
    crate::forms::compose::<Form>(state, &route, i, &[])
}

/// The gist's description, as written.
pub fn description(state: &State, route: &Route) -> Option<String> {
    let form = state.forms.get::<Form>(route)?;
    Some(form.description.clone())
}

/// File `i`'s name, as written.
pub fn file_name(state: &State, route: &Route, i: usize) -> Option<String> {
    let form = state.forms.get::<Form>(route)?;
    Some(form.files.get(i)?.name.clone())
}

/// What a gist's form does.
pub const FORM: crate::forms::Kind = crate::forms::Kind {
    compose,
    row: on_row,
    fill,
    submit,
    follow,
    offers,
};

/// Enter on one of the form's rows.
#[must_use]
fn on_row(state: &mut State, link: &Row) -> Vec<Cmd> {
    let Some((route, _)) = here(state) else {
        return Vec::new();
    };
    match link {
        Row::Description => state.open_picker(crate::picker::Kind::Description(route)),
        Row::Rename(i) => state.open_picker(crate::picker::Kind::FileName(route, Some(*i))),
        Row::AddFile => state.open_picker(crate::picker::Kind::FileName(route, None)),
        Row::Remove(i) => {
            remove(state, &route, *i);
            Vec::new()
        }
        _ => Vec::new(),
    }
}

/// Takes file `i` out of the form.
fn remove(state: &mut State, route: &Route, i: usize) {
    let form = state.forms.edit::<Form>(route);
    if form.files.len() < 2 {
        state.info("A gist needs a file: add another before removing this one");
        return;
    }
    if i >= form.files.len() {
        return;
    }
    let file = form.files.remove(i);
    if let Some((was, _)) = file.was {
        form.removed.push(was);
    }
    let name = if file.name.is_empty() {
        "the file".to_owned()
    } else {
        file.name
    };
    state.info(format!("Removed {name}: saving takes it out of the gist"));
}

pub fn description_rows(route: &Route, q: &str) -> crate::picker::Rows {
    let label = if q.is_empty() {
        "No description".to_owned()
    } else {
        format!("Describe it: “{q}”")
    };
    vec![entry(label, "↵", Some(filling(route, Row::Description, q)))]
}

/// Why `name` can't name file `i` of the form, if it can't.
fn name_problem(form: &Form, i: Option<usize>, name: &str) -> Option<String> {
    if name.is_empty() {
        return Some("Type the file's name".into());
    }
    if name.contains('/') {
        return Some("A gist's files can't be in folders: no /".into());
    }
    let taken = (form.files.iter().enumerate())
        .any(|(j, f)| Some(j) != i && f.name.eq_ignore_ascii_case(name));
    taken.then(|| format!("The gist has a file named {name} already"))
}

pub fn name_rows(
    state: &State,
    route: &Route,
    file: Option<usize>,
    q: &str,
) -> crate::picker::Rows {
    let form = state.forms.shown::<Form>(route);
    if let Some(why) = name_problem(&form, file, q) {
        return vec![entry(why, "", None)];
    }
    let label = match file {
        Some(_) => format!("Name it {q}"),
        None => format!("Add {q}"),
    };
    let row = file.map_or(Row::AddFile, Row::Rename);
    vec![entry(label, "↵", Some(filling(route, row, q)))]
}

/// Its description, or a file's name, chosen.
fn fill(state: &mut State, route: &Route, row: &Row, value: String) -> Vec<Cmd> {
    let form = state.forms.edit::<Form>(route);
    match row {
        Row::Description => form.description = value,
        Row::Rename(i) => {
            if let Some(f) = form.files.get_mut(*i) {
                f.name = value;
            }
        }
        Row::AddFile => {
            form.files.push(File {
                name: value,
                ..File::default()
            });
            let i = form.files.len().saturating_sub(1);
            return compose(state, i);
        }
        _ => {}
    }
    Vec::new()
}

/// The files to send: each named (GitHub's way, for a file without a
/// name: `gistfile1.txt`), or why they can't be sent.
fn named(form: &Form) -> Result<Vec<(String, String)>, String> {
    if form.files.iter().all(|f| f.text.trim().is_empty()) {
        return Err("A gist needs a file with something in it".into());
    }
    let mut files: Vec<(String, String)> = Vec::new();
    for (i, f) in form.files.iter().enumerate() {
        let name = match f.name.trim() {
            "" => format!("gistfile{}.txt", i + 1),
            name => name.to_owned(),
        };
        if f.text.trim().is_empty() {
            return Err(format!(
                "{name} is empty: write something in it, or remove it"
            ));
        }
        if let Some(why) = name_problem(form, Some(i), &name) {
            return Err(why);
        }
        if files.iter().any(|(n, _)| n.eq_ignore_ascii_case(&name)) {
            return Err(format!("Two files are named {name}"));
        }
        files.push((name, f.text.clone()));
    }
    Ok(files)
}

/// The change an edit makes: only the files that changed.
fn change(form: &Form, id: &str, files: &[(String, String)]) -> GistChange {
    let mut changed: Vec<FileChange> = (form.removed.iter())
        .map(|was| FileChange {
            was: was.clone(),
            now: None,
        })
        .collect();
    let mut added = Vec::new();
    for (f, (name, text)) in form.files.iter().zip(files) {
        match &f.was {
            // The composer drops a last newline: that isn't a change.
            Some((was, before)) if was == name && before.trim_end() == text.trim_end() => {}
            Some((was, _)) => changed.push(FileChange {
                was: was.clone(),
                now: Some((name.clone(), text.clone())),
            }),
            None => added.push((name.clone(), text.clone())),
        }
    }
    GistChange {
        id: id.to_owned(),
        description: form.description.clone(),
        changed,
        added,
    }
}

/// `,s`, ctrl-s or its button: asks whether a new gist is secret or
/// public; saves an edit.
#[must_use]
fn submit(state: &mut State) -> Vec<Cmd> {
    let Some((route, form)) = here(state) else {
        return Vec::new();
    };
    let files = match named(&form) {
        Ok(files) => files,
        Err(why) => {
            return say(state, why);
        }
    };
    if let Route::Gists(gists::EditGist { .. }) = route {
        let Some((id, _)) = form.gist.clone() else {
            return say(state, "The gist hasn't loaded yet".into());
        };
        let change = change(&form, &id, &files);
        crate::forms::form_sending(state, &route);
        state.info("Saving the gist…");
        return work::start(Update { route, change });
    }
    let names: Vec<&str> = files.iter().map(|(n, _)| n.as_str()).collect();
    let mut facts = vec![(names.join(", "), false)];
    if !form.description.is_empty() {
        facts.push((format!("“{}”", form.description), false));
    }
    facts.push((
        "A secret gist is hidden from search, not private: anyone with its link can see it".into(),
        false,
    ));
    crate::forms::ask(
        state,
        route,
        "Create the gist?".into(),
        facts,
        vec![
            ("Create secret gist".into(), Answer::Gist { public: false }),
            ("Create public gist".into(), Answer::Gist { public: true }),
        ],
    );
    Vec::new()
}

/// Makes the new gist, secret or public.
#[must_use]
pub fn create(state: &mut State, route: &Route, public: bool) -> Vec<Cmd> {
    let Some(form) = state.forms.get::<Form>(route) else {
        return Vec::new();
    };
    let files = match named(form) {
        Ok(files) => files,
        Err(why) => {
            return say(state, why);
        }
    };
    let new = NewGist {
        description: form.description.clone(),
        public,
        files,
    };
    crate::forms::form_sending(state, route);
    work::start(Create {
        route: route.clone(),
        new,
    })
}

/// `,D` on your gist: asks first, as GitHub does.
#[must_use]
pub fn ask_delete(state: &mut State) -> Vec<Cmd> {
    let Some(g) = shown(state) else {
        return state.just_say("The gist hasn't loaded yet");
    };
    if let Some(why) = not_yours(state, g) {
        return state.just_say(why);
    }
    let n = g.files.len();
    let files = format!("{n} {}", crate::ui::text::noun(n as u64, "file"));
    let what = if g.description.is_empty() {
        format!("Its {files}")
    } else {
        format!("“{}” and its {files}", g.description)
    };
    let Some(route) = state.route().cloned() else {
        return Vec::new();
    };
    crate::forms::ask(
        state,
        route,
        "Delete this gist?".into(),
        vec![(format!("{what}: it can't be brought back"), true)],
        vec![("Delete gist".into(), Answer::DeleteGist)],
    );
    Vec::new()
}

/// Deletes the gist on `route`'s page.
#[must_use]
pub fn delete(state: &mut State, route: &Route) -> Vec<Cmd> {
    let Route::Gists(gists::Gist { id, .. }) = route else {
        return Vec::new();
    };
    let Some(g) = state.picked::<Gist>(&DataKey::Gist(id.clone())) else {
        return Vec::new();
    };
    let owner = g.owner.clone().unwrap_or_default();
    crate::forms::sending(state);
    work::start(Delete {
        id: id.clone(),
        owner,
    })
}

/// What a gist, or its owner's gists, show once GitHub has a change.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Expected {
    /// A gist with this description and these files' names and text.
    Gist {
        id: String,
        description: String,
        files: Vec<(String, String)>,
    },
    /// A gist no longer among its owner's.
    GistGone(String, String),
}

impl Expectation for Expected {
    fn waiting(&self) -> &'static str {
        match self {
            Expected::Gist { .. } => "Waiting for GitHub to show the gist",
            Expected::GistGone(..) => "Waiting for GitHub to show the gist gone",
        }
    }

    fn shown(&self, state: &State, _: &Route) -> Shown {
        use crate::api::browse::{GistSummary, Results};
        match self {
            Expected::Gist {
                id,
                description,
                files,
            } => state
                .picked::<Gist>(&DataKey::Gist(id.clone()))
                .is_some_and(|g| {
                    let shown: Vec<(&str, Option<&str>)> = (g.files.iter())
                        .map(|f| (f.name.as_str(), f.text.as_deref().filter(|_| !f.truncated)))
                        .collect();
                    g.description == *description
                        && shown.len() == files.len()
                        && files.iter().all(|(name, text)| {
                            shown
                                .iter()
                                .any(|(n, t)| n == name && t.is_none_or(|t| t == text))
                        })
                }),
            Expected::GistGone(owner, id) => state
                .picked::<Results<GistSummary>>(&DataKey::Gists(owner.to_lowercase()))
                .is_some_and(|r| r.items.iter().all(|g| g.id != *id)),
        }
        .into()
    }
}

/// What a gist shows once GitHub has it as sent.
fn expected(id: String, description: String, files: Vec<(String, String)>) -> Expected {
    Expected::Gist {
        id,
        description,
        files,
    }
}

/// The gist's page follows GitHub until it shows it.
impl Job for Create {
    type Out = MadeGist;

    async fn run(&self, cx: &Cx) -> Result<MadeGist, Failure> {
        Ok(cx.gh.create_gist(&self.new).await?)
    }

    fn done(self, state: &mut State, result: Result<MadeGist, Failure>) -> Vec<Cmd> {
        let route = self.route;
        let made = match result {
            Ok(made) => made,
            Err(why) => return unsaved(state, &route, &why),
        };
        let form: Form = crate::forms::done(state, &route);
        let owner = made.owner.map(|o| o.login).or_else(|| state.viewer.clone());
        state.info("Created the gist");
        if state.route() != Some(&route) {
            return Vec::new();
        }
        let files = named(&form).unwrap_or_default();
        let page = Route::Gists(gists::Gist {
            owner,
            id: made.id.clone(),
        });
        crate::follow::expect(
            state,
            page.clone(),
            expected(made.id, form.description, files),
        );
        state.replace(page, true, None)
    }
}

/// The gist's page follows GitHub until it shows the change.
impl Job for Update {
    type Out = ();

    async fn run(&self, cx: &Cx) -> Result<(), Failure> {
        Ok(cx.gh.update_gist(&self.change).await?)
    }

    fn done(self, state: &mut State, result: Result<(), Failure>) -> Vec<Cmd> {
        let Update { route, change } = self;
        if let Err(why) = result {
            return unsaved(state, &route, &why);
        }
        let form: Form = crate::forms::done(state, &route);
        state.info("Saved the gist");
        let Route::Gists(gists::EditGist { owner, id }) = &route else {
            return Vec::new();
        };
        if state.route() != Some(&route) {
            return Vec::new();
        }
        let page = Route::Gists(gists::Gist {
            owner: Some(owner.clone()),
            id: id.clone(),
        });
        let files = named(&form).unwrap_or_default();
        crate::follow::expect(
            state,
            page.clone(),
            expected(change.id, change.description, files),
        );
        back_to(state, page)
    }
}

/// Once it's deleted, your gists follow GitHub until it's gone.
impl Job for Delete {
    type Out = ();

    async fn run(&self, cx: &Cx) -> Result<(), Failure> {
        Ok(cx.gh.delete_gist(&self.id).await?)
    }

    fn done(self, state: &mut State, result: Result<(), Failure>) -> Vec<Cmd> {
        let Delete { id, owner } = self;
        if let Err(why) = result {
            return say(state, format!("GitHub didn't delete the gist: {why}"));
        }
        state.overlay = None;
        state.info("Deleted the gist");
        let on_it = matches!(state.route(), Some(Route::Gists(gists::Gist { id: shown, .. })) if *shown == id);
        if !on_it {
            return Vec::new();
        }
        let page = Route::Gists(gists::Gists(owner.clone()));
        crate::follow::expect(state, page.clone(), Expected::GistGone(owner, id));
        state.replace(page, true, None)
    }
}

/// GitHub didn't save the gist on the form at `route`, and why.
fn unsaved(state: &mut State, route: &Route, why: &Failure) -> Vec<Cmd> {
    crate::forms::failed(state, route, format!("GitHub didn't save the gist: {why}"))
}

/// Back to `page` when it's the page before, else to it in place.
#[must_use]
fn back_to(state: &mut State, page: Route) -> Vec<Cmd> {
    let below = state.screens.below().and_then(|s| match s {
        crate::state::Screen::Page(p) => Some(&p.route),
        crate::state::Screen::Diff(_) => None,
    });
    if below == Some(&page) {
        let mut cmds = state.back();
        state.forward.clear();
        cmds.extend(state.ensure_route(&page, true));
        cmds
    } else {
        state.replace(page, true, None)
    }
}

/// The form's rows in the menu: writing it, and saving it.
fn offers(state: &State, out: &mut Vec<Doable>) {
    if let Some((route, form)) = here(state) {
        let save = match route {
            Route::Gists(gists::NewGist) => ("Create the gist (secret or public)", "create"),
            _ => ("Save the gist", "save"),
        };
        let write = Some("Write the selected file");
        crate::forms::offer(state, write, save, named(&form).err(), out);
    }
}

/// The menu's rows: a new gist anywhere a gist or profile is; changing
/// and deleting your gist on its page.
fn doables(state: &State, out: &mut Vec<Doable>) {
    let Some(route) = state.route().filter(|_| here(state).is_none()) else {
        return;
    };
    if let Route::Gists(gists::Gist { .. }) = route {
        let why = match shown(state) {
            Some(g) => not_yours(state, g),
            None => Some("it hasn't loaded yet".into()),
        };
        out.push(row(
            "Edit",
            Action::EditComment,
            "Edit the gist",
            "edit",
            why.clone(),
        ));
        out.push(row(
            "Edit",
            Action::DeleteComment,
            "Delete the gist",
            "",
            why,
        ));
    }
    if matches!(
        route,
        Route::Gists(gists::Gist { .. })
            | Route::Gists(gists::Gists(_))
            | Route::People(people::User { .. })
            | Route::App(app::Home)
    ) {
        out.push(row("New", Action::NewGist, "New gist", "", None));
    }
}

/// The form's page.
pub fn build(state: &State, page: &mut Page, route: &Route) {
    let form = state.forms.shown::<Form>(route);
    let composing = |i| crate::forms::composing(state, route, i);
    let editing = matches!(route, Route::Gists(gists::EditGist { .. }));
    let mut rows = vec![FormRow {
        name: "Description",
        value: if form.description.is_empty() {
            "Gist description…".into()
        } else {
            form.description.clone()
        },
        unset: form.description.is_empty(),
        chips: Vec::new(),
        link: Some(Row::Description),
    }];
    let mut status = Vec::new();
    if editing {
        let (visibility, why) = if form.public {
            ("Public", "GitHub doesn't let a public gist become secret")
        } else {
            (
                "Secret",
                "Making it public works on github.com: GitHub's API can't",
            )
        };
        rows.push(FormRow {
            name: "Visibility",
            value: format!("{visibility} · {why}"),
            unset: true,
            chips: Vec::new(),
            link: None,
        });
        if !form.removed.is_empty() {
            let text = format!("Saving removes {}", form.removed.join(", "));
            status.push((text, Mark::Waiting, None));
        }
    }
    let boxes: Vec<TextBox<'_>> = (form.files.iter().enumerate())
        .map(|(i, f)| {
            let mut actions = vec![("rename", Row::Rename(i))];
            if form.files.len() > 1 {
                actions.push(("remove", Row::Remove(i)));
            }
            let markdown = crate::ui::pages::is_markdown(&f.name);
            TextBox {
                title: f.name.clone(),
                untitled: "Filename including extension…",
                body: if composing(i) { "" } else { &f.text },
                markdown,
                empty: "Nothing in it yet.",
                hint: f
                    .locked
                    .as_deref()
                    .unwrap_or("Enter writes it in the composer (ctrl-e for $EDITOR)"),
                link: Link::Form(Row::Text(i)),
                actions,
            }
        })
        .collect();
    let after = vec![FormRow {
        name: "",
        value: "+ Add file".into(),
        unset: false,
        chips: Vec::new(),
        link: Some(Row::AddFile),
    }];
    let write = state.first_key(Action::Comment);
    let save_key = state.first_key(Action::Create);
    let view = WriteFormView {
        heading: if editing {
            "Editing gist".into()
        } else {
            "New gist".into()
        },
        base: None,
        rows,
        status,
        boxes,
        after,
        write: &write,
        button: if editing {
            "Update gist".into()
        } else {
            "Create gist…".into()
        },
        save_key: &save_key,
        why_not: named(&form).err(),
        error: state.forms.sent(route).error.as_deref(),
    };
    crate::ui::pages::write_form(page, &view);
}

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

    fn file(name: &str, text: &str, was: Option<(&str, &str)>) -> File {
        File {
            name: name.into(),
            text: text.into(),
            was: was.map(|(n, t)| (n.into(), t.into())),
            locked: None,
        }
    }

    /// An edit sends only what changed: renamed and rewritten files by
    /// the names GitHub has, new files, and removed ones as nothing.
    #[test]
    fn an_edit_sends_only_what_changed() {
        let form = Form {
            description: "Notes".into(),
            files: vec![
                file("a.md", "same", Some(("a.md", "same"))),
                file("b2.rs", "fn b() {}", Some(("b.rs", "fn b() {}"))),
                file("c.txt", "new text", Some(("c.txt", "old text"))),
                file("", "added", None),
            ],
            removed: vec!["gone.txt".into()],
            ..Form::default()
        };
        let files = named(&form).unwrap();
        assert_eq!(files[3].0, "gistfile4.txt");
        let change = change(&form, "abc", &files);
        let moved = |was: &str, name: &str, text: &str| FileChange {
            was: was.into(),
            now: Some((name.into(), text.into())),
        };
        assert_eq!(
            change.changed,
            vec![
                FileChange {
                    was: "gone.txt".into(),
                    now: None
                },
                moved("b.rs", "b2.rs", "fn b() {}"),
                moved("c.txt", "c.txt", "new text"),
            ]
        );
        assert_eq!(
            change.added,
            vec![("gistfile4.txt".to_owned(), "added".to_owned())]
        );
    }

    #[test]
    fn a_gist_needs_something_in_each_file_and_unique_names() {
        let form = |files| Form {
            files,
            ..Form::default()
        };
        named(&form(vec![file("a", " ", None)])).unwrap_err();
        let twice = form(vec![file("a.rs", "1", None), file("A.rs", "2", None)]);
        assert!(named(&twice).unwrap_err().contains("already"));
        let folder = form(vec![file("src/a.rs", "1", None)]);
        assert!(named(&folder).unwrap_err().contains("folders"));
    }
}