delta-gitu 0.1.17

An experimental fork of gitu (a git client inspired by Magit), published as a library for delta to embed
Documentation
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
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
use super::{Action, OpTrait};
use crate::{
    Res,
    app::{App, PromptParams, State},
    config::ColorMode,
    diff_renderer::Theme,
    error::Error,
    item_data::ItemData,
    menu::PendingMenu,
    picker::{Picked, PickerData, PickerItem, PickerState},
    screen::NavMode,
    term::Term,
};
use std::cell::RefCell;
use std::rc::Rc;

pub(crate) struct Quit;
impl OpTrait for Quit {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, term| {
            let menu = app
                .state
                .pending_menu
                .as_ref()
                .map(|pending_menu| pending_menu.menu);

            if menu == app.base_menu() {
                if app.state.screens.len() == 1 {
                    if app.state.config.general.confirm_quit.enabled {
                        app.confirm(term, "Really quit? [y/n]")?;
                    };

                    app.state.quit = true;
                } else {
                    app.state.screens.pop();
                    app.screen_mut().come_back()?;
                }
            }

            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Quit/Close".into()
    }
}

/// Folds the section the user unfolded on this screen that the cursor is on or
/// inside. Otherwise closes the screen on top, as quit does, but on the first
/// screen does nothing rather than leave gitu.
pub(crate) struct FoldOrBack;
impl OpTrait for FoldOrBack {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            let menu = app
                .state
                .pending_menu
                .as_ref()
                .map(|pending_menu| pending_menu.menu);

            if menu != app.base_menu() {
                return Ok(());
            }
            if !app.screen_mut().fold_unfolded_here()? && app.state.screens.len() > 1 {
                app.state.screens.pop();
                app.screen_mut().come_back()?;
            }

            Ok(())
        }))
    }

    fn display(&self, state: &State) -> String {
        if state.screens.last().unwrap().has_unfolded_here() {
            "Fold".into()
        } else {
            "Back".into()
        }
    }
}

pub(crate) struct OpenMenu(pub crate::menu::Menu);
impl OpTrait for OpenMenu {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        let submenu = self.0;
        Some(Rc::new(move |app, _term| {
            app.state.pending_menu = Some(PendingMenu::init(submenu));
            app.inhibit_close_menu();
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Submenu".into()
    }
}

pub(crate) struct Suspend;
impl OpTrait for Suspend {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, term| {
            term.suspend(&app.state.config).map_err(Error::Term)
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Suspend".into()
    }
}

pub(crate) struct Refresh;
impl OpTrait for Refresh {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| app.update_screens()))
    }

    fn display(&self, _state: &State) -> String {
        "Refresh".into()
    }
}

pub(crate) struct ToggleArg(pub String);
impl OpTrait for ToggleArg {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        let arg_name = self.0.clone();
        Some(Rc::new(move |app, term| {
            let mut need_prompt = None;
            let mut default = None;

            let maybe_entry = if let Some(menu) = &mut app.state.pending_menu {
                Some(menu.args.entry(arg_name.clone().into()))
            } else {
                None
            };

            if let Some(entry) = maybe_entry {
                entry.and_modify(|arg| {
                    if arg.is_active() {
                        arg.unset();
                    } else if arg.expects_value() {
                        default = arg.default_as_string();
                        need_prompt = Some(arg.display);
                    } else {
                        arg.set("").expect("Should succeed");
                    }
                });
            }

            let arg_name = arg_name.clone();
            let parse_and_set_arg =
                Box::new(move |app: &mut App, _term: &mut Term, value: &str| {
                    if let Some(menu) = &mut app.state.pending_menu
                        && let Some(entry) = menu.args.get_mut(arg_name.as_str())
                    {
                        return entry.set(value);
                    }

                    Ok(())
                });

            if let Some(display) = need_prompt {
                let arg = app.prompt(
                    term,
                    &PromptParams {
                        prompt: display,
                        create_default_value: Box::new(move |_| default.clone()),
                        hide_menu: false,
                        prefill: false,
                        history: None,
                        bare: false,
                        on_edit: None,
                    },
                )?;

                parse_and_set_arg(app, term, &arg)?;
            }

            app.inhibit_close_menu();
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        self.0.clone()
    }
}

pub(crate) struct ToggleSection;
impl OpTrait for ToggleSection {
    fn get_action(&self, target: &ItemData) -> Option<Action> {
        if target.is_section() {
            Some(Rc::new(|app, _term| {
                app.screen_mut().toggle_section()?;
                Ok(())
            }))
        } else {
            None
        }
    }

    fn is_target_op(&self) -> bool {
        true
    }

    fn display(&self, state: &State) -> String {
        let item = state.screens.last().unwrap().get_selected_item();
        if state.screens.last().unwrap().is_collapsed(item) {
            "Unfold".into()
        } else {
            "Fold".into()
        }
    }
}

/// Ask git for a different amount of the file around each change: a number of
/// lines, or the whole enclosing function.
pub(crate) struct DiffContext;
impl OpTrait for DiffContext {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app: &mut App, term: &mut Term| {
            let current = app.state.context.clone();
            let answer = app.prompt(
                term,
                &PromptParams {
                    prompt: "",
                    create_default_value: Box::new(move |_| {
                        current.as_deref().map(|flag| match flag {
                            "-W" => "W".to_owned(),
                            flag => flag.trim_start_matches("-U").to_owned(),
                        })
                    }),
                    hide_menu: false,
                    prefill: true,
                    history: None,
                    bare: false,
                    on_edit: None,
                },
            )?;

            let Some(flag) = context_flag(&answer) else {
                app.display_error(format!("Not a number of lines, nor W: {answer}"));
                return Ok(());
            };

            app.state.context = flag;
            app.rerender_screens()
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Diff context".into()
    }
}

/// Widen (`true`) or narrow the context git is asked for, by a power of two: to the next power of
/// two above the context, or below it, down to none. From the whole function, there is nothing
/// to step from.
pub(crate) struct StepDiffContext(pub(crate) bool);
impl OpTrait for StepDiffContext {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        let wider = self.0;
        Some(Rc::new(move |app: &mut App, _term: &mut Term| {
            let lines = match app.state.context.as_deref() {
                Some("-W") => {
                    app.display_error("The context is the whole function; U sets a number");
                    return Ok(());
                }
                Some(flag) => flag.trim_start_matches("-U").parse().unwrap_or(3),
                // git's own context, as configured or by default.
                None => app
                    .state
                    .repo
                    .config()
                    .and_then(|config| config.get_i32("diff.context"))
                    .map_or(3, |lines| lines.max(0) as u32),
            };
            let lines = if wider {
                (lines + 1).next_power_of_two()
            } else if lines <= 1 {
                0
            } else {
                // The largest power of two below `lines`.
                1 << (u32::BITS - 1 - (lines - 1).leading_zeros())
            };
            app.state.context = Some(Rc::from(format!("-U{lines}").as_str()));
            app.rerender_screens()
        }))
    }

    fn display(&self, _state: &State) -> String {
        if self.0 {
            "Wider context".into()
        } else {
            "Narrower context".into()
        }
    }
}

/// Edit the question git is being asked. Everything a view is — the revs, what
/// is compared with what, which paths, how much context — is in that command,
/// so this is the general case of which [`DiffContext`] is one canned edit.
pub(crate) struct EditGitCommand;
impl OpTrait for EditGitCommand {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app: &mut App, term: &mut Term| {
            let Some(git) = app.screen().git_command.clone() else {
                app.display_error(NOT_ASKED_FOR);
                return Ok(());
            };

            let line = git.borrow().line(app.state.context.as_deref());
            let answer = app.prompt(
                term,
                &PromptParams {
                    prompt: "",
                    create_default_value: Box::new(move |_| Some(line.clone())),
                    prefill: true,
                    history: Some(crate::prompt::HistoryKind::GitCommand),
                    ..Default::default()
                },
            )?;

            let edited = git.borrow().edited(&answer);
            match edited {
                Ok(edited) => *git.borrow_mut() = edited,
                Err(err) => {
                    app.display_error(err.to_string());
                    return Ok(());
                }
            }

            // The edit said what context it wants, in the line it was given.
            app.state.context = None;
            app.rerender_screens()
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Edit git command".into()
    }
}

/// Limit the view to some of the files it covers, or drop some of them. Both
/// are edits of the same command, and so of the same kind as [`EditGitCommand`]
/// — this only saves writing the pathspec magic out.
pub(crate) struct FilePatterns;
impl OpTrait for FilePatterns {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app: &mut App, term: &mut Term| {
            let Some(git) = app.screen().git_command.clone() else {
                app.display_error(NOT_ASKED_FOR);
                return Ok(());
            };

            let patterns = git.borrow().file_patterns().join(" ");
            let answer = app.prompt(
                term,
                &PromptParams {
                    prompt: "File filters",
                    create_default_value: Box::new(move |_| Some(patterns.clone())),
                    prefill: true,
                    history: Some(crate::prompt::HistoryKind::FilePatterns),
                    ..Default::default()
                },
            )?;

            let patterns = match shell_words::split(&answer) {
                Ok(patterns) => patterns,
                Err(_) => {
                    app.display_error(Error::EditedCommandQuotes.to_string());
                    return Ok(());
                }
            };

            ask_for(app, &git, &patterns)
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Files".into()
    }
}

/// Drop the file whose header is under the cursor from the view, as if it were
/// not in the patch. The frequent case, and the one worth a keystroke: the noise is in
/// front of you and it goes away.
pub(crate) struct HideFile;
impl OpTrait for HideFile {
    fn get_action(&self, target: &ItemData) -> Option<Action> {
        let path = file_of(target)?;

        Some(Rc::new(move |app: &mut App, _term: &mut Term| {
            let Some(git) = app.screen().git_command.clone() else {
                app.display_error(NOT_ASKED_FOR);
                return Ok(());
            };

            let mut patterns = git.borrow().file_patterns();
            patterns.push(crate::calling_process::excluding(&path));

            ask_for(app, &git, &patterns)
        }))
    }

    fn is_target_op(&self) -> bool {
        true
    }

    fn display(&self, _state: &State) -> String {
        "Hide file".into()
    }
}

const NOT_ASKED_FOR: &str = "This view is not something gitu asked git for";

/// Put the held command again, for these paths, and remember it as if typed
/// at `:`.
fn ask_for(
    app: &mut App,
    git: &Rc<RefCell<crate::calling_process::GitCommand>>,
    patterns: &[String],
) -> Res<()> {
    let asking_for = git.borrow().asking_for(patterns);
    app.state.prompt.remember(
        crate::prompt::HistoryKind::GitCommand,
        &asking_for.line(app.state.context.as_deref()),
    )?;
    *git.borrow_mut() = asking_for;
    app.rerender_screens()
}

/// The file a row is the header of.
fn file_of(target: &ItemData) -> Option<String> {
    let ItemData::Delta { diff, file_i, .. } = target else {
        return None;
    };

    let header = &diff.file_diffs[*file_i].header;
    let path = match header.status {
        crate::gitu_diff::Status::Deleted => &header.old_file,
        _ => &header.new_file,
    };

    Some(path.fmt(&diff.text).into_owned())
}

/// The git flag an answer asks for: `None` for no answer, which is to pass no
/// flag at all and leave the context to the command and git's config. An
/// answer that is neither a count nor the whole function isn't one, since a
/// flag that leaves no hunks leaves nothing to act on.
fn context_flag(answer: &str) -> Option<Option<Rc<str>>> {
    match answer {
        "" => Some(None),
        "W" | "w" => Some(Some(Rc::from("-W"))),
        lines if !lines.is_empty() && lines.chars().all(|c| c.is_ascii_digit()) => {
            Some(Some(Rc::from(format!("-U{lines}").as_str())))
        }
        _ => None,
    }
}

/// Fold the whole view down to its headings, or open all of it.
pub(crate) struct ToggleAllSections;
impl OpTrait for ToggleAllSections {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            app.screen_mut().toggle_all_sections();
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Fold all".into()
    }
}

/// Show or hide the list of what a screen's own keymap does.
pub(crate) struct ToggleMenu;
impl OpTrait for ToggleMenu {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app: &mut App, _term: &mut Term| {
            let screen = app.screen_mut();
            screen.show_menu = !screen.show_menu;
            Ok(())
        }))
    }

    fn display(&self, state: &State) -> String {
        if state.screens.last().unwrap().show_menu {
            "Hide keys".into()
        } else {
            "Show keys".into()
        }
    }
}

/// Turn one of the configured renderer features on or off, re-rendering with
/// it. The diff is what changes, not the position in it: a row's identity is
/// its place in the patch, which no amount of re-rendering moves.
pub(crate) struct RendererFeatures;
impl OpTrait for RendererFeatures {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app: &mut App, term: &mut Term| {
            let offered = crate::diff_renderer::offered_features(
                &app.state.config.general.diff_renderer.features,
                &app.state.repo.config().map_err(Error::ReadGitConfig)?,
            )?;
            let general = &app.state.config.general;
            let commands: Vec<String> = general.commands.keys().cloned().collect();
            let views: Vec<String> = general.views.keys().cloned().collect();
            if offered.is_empty() && commands.is_empty() && views.is_empty() {
                app.display_error("No renderer features are configured");
                return Ok(());
            }
            // A column each: a feature's key toggles it, a command's runs it,
            // and a view's opens it.
            let names: Vec<&String> = offered.iter().chain(&commands).chain(&views).collect();
            let columns: Vec<usize> = [(0, offered.len()), (1, commands.len()), (2, views.len())]
                .into_iter()
                .flat_map(|(column, len)| std::iter::repeat_n(column, len))
                .collect();
            let is_command = |name: &str| commands.iter().any(|command| command == name);
            let is_view = |name: &str| views.iter().any(|view| view == name);

            let mut cursor = 0;
            loop {
                let items = feature_items(app, &offered)
                    .into_iter()
                    .chain(named_items(app, &commands))
                    .chain(named_items(app, &views))
                    .collect();
                let picker = PickerState::new("Features", items, false)
                    .assigning_keys()
                    .in_columns(vec!["Features", "Commands", "Views"], columns.clone())
                    .with_cursor(cursor);
                match app.pick_or_set_key(term, picker)? {
                    None => return Ok(()),
                    Some(Picked::Item(picked)) if is_command(picked.display()) => {
                        return crate::ops::command::run(app, term, picked.display());
                    }
                    Some(Picked::Item(picked)) if is_view(picked.display()) => {
                        return crate::ops::command::open_view(app, term, picked.display());
                    }
                    Some(Picked::Item(picked)) => return toggle_feature(app, picked.display()),
                    Some(Picked::KeySet(key, picked)) => {
                        let name = picked.display();
                        cursor = names.iter().position(|held| *held == name).unwrap_or(0);
                        // Space enables or disables a feature and keeps the list open; any
                        // other key becomes the feature's, command's or view's key.
                        if key != ' ' {
                            set_feature_key(app, name, key);
                        } else if !is_command(name) && !is_view(name) {
                            toggle_feature(app, name)?;
                        }
                    }
                }
            }
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Hotkeys".into()
    }
}

/// Move through the themes defined in git config, rendering the screen in view
/// with each, shown in one row so that the screen stays in view. Enter keeps
/// the theme and saves it; cancelling restores the one there was.
pub(crate) struct RendererThemes;
impl OpTrait for RendererThemes {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app: &mut App, term: &mut Term| {
            // The git config the renderer reads, since a theme it cannot find would change
            // nothing.
            let dir = app.state.repo.workdir().unwrap_or(app.state.repo.path());
            let git_config = match app.state.config.general.diff_renderer.git_config.open(dir) {
                Some(Some(git_config)) => git_config,
                Some(None) => git2::Config::new().map_err(Error::ReadGitConfig)?,
                None => app.state.repo.config().map_err(Error::ReadGitConfig)?,
            };
            let themes = crate::diff_renderer::themes(
                &git_config,
                &app.state.config.general.diff_renderer.builtin_themes,
            )?;
            if themes.is_empty() {
                app.display_error("No themes are defined in git config");
                return Ok(());
            }

            // Those for the mode delta renders in first, the others saying which
            // mode they are for, in the list only. A theme that does not say is
            // for either.
            let mode = app.state.config.general.diff_renderer.color_mode;
            let other =
                |theme: &Theme| mode.is_some() && theme.mode.is_some_and(|m| Some(m) != mode);
            let (others, matching): (Vec<Theme>, Vec<Theme>) =
                themes.into_iter().partition(|theme| other(theme));
            let themes: Vec<Theme> = matching.into_iter().chain(others).collect();

            let entered = app.state.theme.clone();
            let cursor = entered
                .as_ref()
                .and_then(|theme| themes.iter().position(|held| &held.name == theme))
                .map_or(0, |i| i + 1);
            // The first item is no theme: the user's own configuration as it is.
            let items = std::iter::once(PickerItem::new(NO_THEME, PickerData::Item(String::new())))
                .chain(themes.iter().map(|theme| {
                    let display = match theme.mode {
                        Some(ColorMode::Dark) if other(theme) => format!("{} (dark)", theme.name),
                        Some(ColorMode::Light) if other(theme) => format!("{} (light)", theme.name),
                        _ => theme.name.clone(),
                    };
                    let item = PickerItem::new(display, PickerData::Item(theme.name.clone()));
                    if other(theme) {
                        item.only_in_list()
                    } else {
                        item
                    }
                }))
                .collect();
            let picker = PickerState::new("Theme", items, false)
                .one_row()
                .with_cursor(cursor);

            let picked = app.pick_previewing(term, picker, &mut |app, data| {
                app.state.theme = theme_of(data);
                app.rerender_screen()
            })?;

            match picked {
                Some(data) => {
                    app.state.theme = theme_of(&data);
                    let dir = app.state.repo.workdir().unwrap_or(app.state.repo.path());
                    if !app.state.read_only
                        && let Err(e) =
                            crate::config_edit::set_theme(dir, app.state.theme.as_deref())
                    {
                        app.display_error(e.to_string());
                    }
                }
                None => app.state.theme = entered,
            }
            app.rerender_screens()
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Themes".into()
    }
}

const NO_THEME: &str = "(no theme)";

fn theme_of(data: &PickerData) -> Option<String> {
    Some(data.display().to_owned()).filter(|theme| !theme.is_empty())
}

/// Each feature, marked if it is enabled, beside its key if it has one.
fn feature_items(app: &App, offered: &[String]) -> Vec<PickerItem> {
    let enabled = enabled_by_config(app);
    offered
        .iter()
        .map(|feature| {
            let mark = if is_enabled(app, feature, &enabled) {
                '●'
            } else {
                ' '
            };
            let key = app.state.feature_keys.get(feature).copied().unwrap_or(' ');
            PickerItem::new(
                format!("{mark} {key} {feature}"),
                PickerData::Item(feature.clone()),
            )
        })
        .collect()
}

/// Each of `names`, a command or view, beside its key if it has one.
fn named_items(app: &App, names: &[String]) -> Vec<PickerItem> {
    names
        .iter()
        .map(|name| {
            let key = app.state.feature_keys.get(name).copied().unwrap_or(' ');
            PickerItem::new(format!("{key} {name}"), PickerData::Item(name.clone()))
        })
        .collect()
}

pub(crate) fn toggle_feature(app: &mut App, feature: &str) -> Res<()> {
    let enabled = enabled_by_config(app);
    app.state.features = toggled(
        &app.state.features,
        feature,
        is_enabled_by_config(feature, &enabled),
    );
    app.rerender_screens()
}

/// Whether `feature` is enabled: enabled in-session, or enabled by the user's own
/// configuration (`enabled`) and not disabled in-session. Whether it is then in
/// effect is delta's to decide, from the rest of the user's configuration, such
/// as a minimum width.
fn is_enabled(app: &App, feature: &str, enabled: &[String]) -> bool {
    let held = |entry: &str| app.state.features.iter().any(|held| held == entry);
    held(feature) || (is_enabled_by_config(feature, enabled) && !held(&format!("-{feature}")))
}

/// Whether `feature` is among `enabled`, which are by delta's name.
fn is_enabled_by_config(feature: &str, enabled: &[String]) -> bool {
    let deltas_name = crate::diff_renderer::deltas_name(feature);
    enabled.contains(&deltas_name)
}

/// The features the user's own configuration enables now, by delta's name.
fn enabled_by_config(app: &App) -> Vec<String> {
    let dir = app.state.repo.workdir().unwrap_or(app.state.repo.path());
    app.state
        .config
        .general
        .diff_renderer
        .enabled_features
        .get(dir)
}

/// Make `key` toggle `feature`, or run the command of that name, at the top
/// level, in this session and, by saving it to the config file, the next.
/// `feature`'s own key unsets it, and a key bound already is refused.
fn set_feature_key(app: &mut App, feature: &str, key: char) {
    let key = if app.state.feature_keys.get(feature) == Some(&key) {
        None
    } else if app.state.config.bindings.binds_at_top_level(key) {
        app.display_error(format!("{key} is bound already"));
        return;
    } else {
        Some(key)
    };

    app.state
        .feature_keys
        .retain(|name, held| name != feature && Some(*held) != key);
    if let Some(key) = key {
        app.state.feature_keys.insert(feature.to_owned(), key);
    }

    if app.state.read_only {
        return;
    }
    let dir = app.state.repo.workdir().unwrap_or(app.state.repo.path());
    if let Err(e) = crate::config_edit::set_feature_keys(dir, &app.state.feature_keys) {
        app.display_error(e.to_string());
    }
}

/// `features` with `feature` toggled. Its entry enables it, or, if the user's own
/// configuration enables it (`enabled_by_config`), disables it as `-feature`; the
/// entry is removed if present, returning to the user's configuration, and
/// appended if not. Order is otherwise kept, since delta resolves a conflict
/// between two features in favour of the later one.
fn toggled(features: &[String], feature: &str, enabled_by_config: bool) -> Rc<[String]> {
    let entry = if enabled_by_config {
        format!("-{feature}")
    } else {
        feature.to_owned()
    };
    if features.contains(&entry) {
        return features
            .iter()
            .filter(|held| **held != entry)
            .cloned()
            .collect();
    }

    features.iter().cloned().chain([entry]).collect()
}

/// Name the commit under the cursor as the one [`App::pick_commit`] was after.
pub(crate) struct SelectCommit;
impl OpTrait for SelectCommit {
    fn get_action(&self, target: &ItemData) -> Option<Action> {
        let ItemData::Commit { oid, .. } = target else {
            return None;
        };
        let oid = oid.clone();

        Some(Rc::new(move |app: &mut App, _term: &mut Term| {
            app.state.picked_commit = Some(oid.clone());
            Ok(())
        }))
    }

    fn is_target_op(&self) -> bool {
        true
    }

    fn display(&self, _state: &State) -> String {
        "Select".into()
    }
}

pub(crate) struct MoveUp;
impl OpTrait for MoveUp {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            app.screen_mut().select_previous(NavMode::Normal);
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Up".into()
    }
}

pub(crate) struct MoveDown;
impl OpTrait for MoveDown {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            app.screen_mut().select_next(NavMode::Normal);
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Down".into()
    }
}

pub(crate) struct MoveDownLine;
impl OpTrait for MoveDownLine {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            app.screen_mut().select_next(NavMode::IncludeSubLines);
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Down line".into()
    }
}

pub(crate) struct MoveUpLine;
impl OpTrait for MoveUpLine {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            app.screen_mut().select_previous(NavMode::IncludeSubLines);
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Up line".into()
    }
}

/// Reach the selection out over another line, towards the end of the hunk or
/// back towards its start.
pub(crate) struct ExtendSelection(pub bool);
impl OpTrait for ExtendSelection {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        let forwards = self.0;
        Some(Rc::new(move |app: &mut App, _term: &mut Term| {
            app.screen_mut().extend_selection(forwards);
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        if self.0 {
            "Select down".into()
        } else {
            "Select up".into()
        }
    }
}

pub(crate) struct MoveToScreenLine(pub usize);
impl OpTrait for MoveToScreenLine {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        let screen_line = self.0;
        Some(Rc::new(move |app, _term| {
            app.screen_mut().move_cursor_to_screen_line(screen_line);
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Move to line".into()
    }
}

pub(crate) struct MoveNextSection;
impl OpTrait for MoveNextSection {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            let depth = app.screen().get_selected_item().depth;
            app.screen_mut().select_next(NavMode::Siblings { depth });
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Next section".into()
    }
}

pub(crate) struct MovePrevSection;
impl OpTrait for MovePrevSection {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            let depth = app.screen().get_selected_item().depth;
            app.screen_mut()
                .select_previous(NavMode::Siblings { depth });
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Prev section".into()
    }
}

pub(crate) struct MoveParentSection;
impl OpTrait for MoveParentSection {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            let depth = app.screen().get_selected_item().depth.saturating_sub(1);
            app.screen_mut()
                .select_previous(NavMode::Siblings { depth });
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Parent section".into()
    }
}

pub(crate) struct HalfPageUp;
impl OpTrait for HalfPageUp {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            app.screen_mut().half_page_up();
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Scroll half page up".into()
    }
}

pub(crate) struct HalfPageDown;
impl OpTrait for HalfPageDown {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            app.screen_mut().half_page_down();
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Scroll half page down".into()
    }
}

pub(crate) struct FullPageUp;
impl OpTrait for FullPageUp {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            app.screen_mut().full_page_up();
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Scroll page up".into()
    }
}

pub(crate) struct FullPageDown;
impl OpTrait for FullPageDown {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            app.screen_mut().full_page_down();
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Scroll page down".into()
    }
}

pub(crate) struct ScrollViewUp;
impl OpTrait for ScrollViewUp {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            app.close_menu();
            app.screen_mut().scroll_view_up(1);
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Scroll view up".into()
    }
}

pub(crate) struct ScrollViewDown;
impl OpTrait for ScrollViewDown {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            app.close_menu();
            app.screen_mut().scroll_view_down(1);
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Scroll view down".into()
    }
}

pub(crate) struct MoveTop;
impl OpTrait for MoveTop {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            app.screen_mut().move_cursor_to_top();
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Top".into()
    }
}

pub(crate) struct MoveBottom;
impl OpTrait for MoveBottom {
    fn get_action(&self, _target: &ItemData) -> Option<Action> {
        Some(Rc::new(|app, _term| {
            app.screen_mut().move_cursor_to_bottom();
            Ok(())
        }))
    }

    fn display(&self, _state: &State) -> String {
        "Bottom".into()
    }
}