delta-gitu 0.1.14

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
use std::{
    collections::BTreeMap,
    path::{Path, PathBuf},
    sync::Arc,
};

use crate::config_git::{self, FEATURE_KEYS};
use crate::style::{Color, Modifier, Style};
use crate::{Bindings, Res, error::Error, key_parser, menu::Menu, ops::Op};
use crossterm::event::{KeyCode, KeyModifiers};
use etcetera::{BaseStrategy, choose_base_strategy};
use figment::{
    Figment,
    providers::{Format, Serialized, Toml},
};
use serde::Deserialize;

const DEFAULT_CONFIG: &str = include_str!("default_config.toml");

pub struct Config {
    pub general: GeneralConfig,
    pub style: StyleConfig,
    pub bindings: Bindings,
    pub picker_bindings: PickerBindings,
}

#[derive(Default, Deserialize)]
pub(crate) struct PickerBindingsConfig {
    #[serde(default)]
    pub next: Vec<String>,
    #[serde(default)]
    pub previous: Vec<String>,
    #[serde(default)]
    pub done: Vec<String>,
    #[serde(default)]
    pub cancel: Vec<String>,
    #[serde(default)]
    pub toggle_list: Vec<String>,
    #[serde(default)]
    pub quit: Vec<String>,
}

#[derive(Default, Deserialize)]
pub(crate) struct BindingsConfig {
    #[serde(flatten)]
    pub menus: BTreeMap<Menu, BTreeMap<Op, Vec<String>>>,
    #[serde(default)]
    pub picker: PickerBindingsConfig,
}

#[derive(Default, Deserialize)]
/// Only used to deserialise configurations with `figment`. This should be
/// parsed to be turned into a useful [`Config`].
pub(crate) struct FigmentConfig {
    pub general: GeneralConfig,
    pub style: StyleConfig,
    pub bindings: BindingsConfig,
}

#[derive(Default, Debug, Deserialize)]
pub struct GeneralConfig {
    pub always_show_help: BoolConfigEntry,
    pub confirm_quit: BoolConfigEntry,
    pub refresh_on_file_change: BoolConfigEntry,
    pub confirm_discard: ConfirmDiscardOption,
    pub collapsed_sections: Vec<String>,
    pub stash_list_limit: usize,
    pub recent_commits_limit: usize,
    pub log_author_width: usize,
    pub mouse_support: bool,
    pub mouse_scroll_lines: usize,
    /// The most input gitu will read, as a pager or when putting a command
    /// again. It is read in one pass before anything is drawn, so this is what
    /// stops a `git log -p` over a whole history from showing nothing for
    /// minutes. What is past the limit is dropped, not deferred.
    pub max_input_bytes: u64,
    /// Whether the branch section is 'Branch: {name}' over a line placing it
    /// relative to its upstream, or the branch name alone.
    pub verbose_branch_status: bool,
    /// Globs whose files are dropped from a paged view, as if the patch never
    /// had them. Generated code and fixtures recur, so hiding them is worth
    /// saying once rather than per session.
    #[serde(default)]
    pub hide: Vec<String>,
    /// Whether the cursor stops on a diff's unchanged lines, as reading a patch
    /// line by line wants. Nothing can be staged there.
    #[serde(default)]
    pub visit_context_lines: bool,
    /// Whether the status screen opens with the cursor on the first unstaged
    /// file, or the first staged one when nothing is unstaged.
    #[serde(default)]
    pub status_cursor_on_changes: bool,
    #[serde(default)]
    pub diff_renderer: DiffRendererConfig,
    /// Arguments given to the `git log` gitu runs for the log view.
    #[serde(default)]
    pub log_args: Vec<String>,
    /// Arguments given to the `git show` gitu runs for a commit it opens.
    #[serde(default)]
    pub show_args: Vec<String>,
    #[serde(default)]
    pub file_header: FileHeader,
    /// Whether a patch of more than one file that does not fit on one screen
    /// starts with its files folded: `git diff`, `git show`, `git log -p`, and a
    /// commit opened from the log.
    #[serde(default)]
    pub start_folded: bool,
    /// Whether gitu leaves the repository and git config as it finds them.
    #[serde(default)]
    pub read_only: ReadOnly,
}

/// Whether gitu runs commands that change the repository (stage, commit,
/// rebase, fetch, ...) and saves what is chosen in-session (a theme, feature
/// keys, prompt history).
#[derive(Default, Debug, PartialEq, Eq, Clone, Copy)]
pub enum ReadOnly {
    /// Read-only in a repository jj also manages (one with a `.jj` directory),
    /// where a git command can contradict jj's own state.
    #[default]
    Auto,
    On,
    Off,
}

impl ReadOnly {
    pub fn is_on(self, repo: &git2::Repository) -> bool {
        match self {
            ReadOnly::Auto => repo.workdir().is_some_and(|dir| dir.join(".jj").is_dir()),
            ReadOnly::On => true,
            ReadOnly::Off => false,
        }
    }
}

/// A boolean, or `auto`. Git config gives a boolean as a string, written as
/// git accepts one.
impl<'de> Deserialize<'de> for ReadOnly {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        #[derive(Deserialize)]
        #[serde(untagged)]
        enum Raw {
            Bool(bool),
            Str(String),
        }
        match Raw::deserialize(deserializer)? {
            Raw::Bool(true) => Ok(ReadOnly::On),
            Raw::Bool(false) => Ok(ReadOnly::Off),
            Raw::Str(value) => match value.to_lowercase().as_str() {
                "auto" => Ok(ReadOnly::Auto),
                "true" | "yes" | "on" | "1" | "" => Ok(ReadOnly::On),
                "false" | "no" | "off" | "0" => Ok(ReadOnly::Off),
                _ => Err(serde::de::Error::custom(format!(
                    "read_only: '{value}' is neither a boolean nor auto"
                ))),
            },
        }
    }
}

/// What heads each file of a patch, folded or not.
#[derive(Default, Debug, Deserialize, PartialEq, Eq, Clone, Copy)]
#[serde(rename_all = "lowercase")]
pub enum FileHeader {
    /// The renderer's file header.
    #[default]
    Delta,
    /// The file's line of a diffstat, counted by gitu from the patch.
    Stat,
}

#[derive(Default, Debug, Deserialize)]
pub struct DiffRendererConfig {
    /// Renderer command as `[executable, ...args]`, set by `delta --gitu` to
    /// itself rather than read from config. It receives a unified diff on stdin
    /// and emits ANSI-colored output carrying OSC-1717 diff-line metadata.
    #[serde(skip)]
    pub command: Vec<String>,
    /// Named renderer features offered by `renderer_features`, to turn on and
    /// off while viewing a diff.
    #[serde(default)]
    pub features: Vec<String>,
    /// Keys that toggle a feature from `renderer_features` before anything is
    /// typed to filter it, by feature.
    #[serde(default)]
    pub feature_keys: BTreeMap<String, char>,
    /// The theme overlaid on the user's own delta configuration: a feature that
    /// sets `dark` or `light`, as delta defines a theme.
    #[serde(default)]
    pub theme: Option<String>,
    /// delta's builtin features to offer as themes after those in git config,
    /// for either mode, such as `diff-so-fancy`.
    #[serde(default)]
    pub builtin_themes: Vec<String>,
    /// The mode delta renders in, set by `delta --gitu`, which settles it before
    /// gitu starts. `None` when gitu is not run by delta.
    #[serde(skip)]
    pub color_mode: Option<ColorMode>,
    /// Which features the user's own configuration enables, by delta's name, asked each time the
    /// answer is needed, so that a change to the user's git config during a session counts. Set
    /// by `delta --gitu`; without it, the user's configuration enables none.
    #[serde(skip)]
    pub enabled_features: EnabledFeatures,
    /// The git config the renderer reads in the repository at a path, which gitu uses for what it
    /// offers from git config, such as themes. Set by `delta --gitu`; without it, gitu reads the
    /// repository's own.
    #[serde(skip)]
    pub git_config: RendererGitConfig,
}

/// Opens the git config the renderer reads in the repository at a path.
#[derive(Clone, Default)]
pub struct RendererGitConfig(Option<Arc<OpenGitConfig>>);

type OpenGitConfig = dyn Fn(&Path) -> Option<git2::Config> + Send + Sync;

impl RendererGitConfig {
    pub fn new(open: impl Fn(&Path) -> Option<git2::Config> + Send + Sync + 'static) -> Self {
        Self(Some(Arc::new(open)))
    }

    /// The renderer's git config at `dir`, or `None` if the renderer has not said how it opens it.
    pub fn open(&self, dir: &Path) -> Option<Option<git2::Config>> {
        self.0.as_ref().map(|open| open(dir))
    }
}

impl std::fmt::Debug for RendererGitConfig {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(if self.0.is_some() {
            "RendererGitConfig(..)"
        } else {
            "RendererGitConfig(none)"
        })
    }
}

/// Asks the renderer which features the user's own configuration, as it is now, enables in the
/// repository at a path.
#[derive(Clone, Default)]
pub struct EnabledFeatures(Option<Arc<AskEnabledFeatures>>);

type AskEnabledFeatures = dyn Fn(&Path) -> Vec<String> + Send + Sync;

impl EnabledFeatures {
    pub fn new(ask: impl Fn(&Path) -> Vec<String> + Send + Sync + 'static) -> Self {
        Self(Some(Arc::new(ask)))
    }

    pub fn get(&self, dir: &Path) -> Vec<String> {
        self.0.as_ref().map(|ask| ask(dir)).unwrap_or_default()
    }
}

impl std::fmt::Debug for EnabledFeatures {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(if self.0.is_some() {
            "EnabledFeatures(..)"
        } else {
            "EnabledFeatures(none)"
        })
    }
}

/// Whether delta renders for a dark or a light terminal background.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorMode {
    Dark,
    Light,
}

#[derive(Default, Debug, Deserialize)]
pub struct BoolConfigEntry {
    #[serde(default)]
    pub enabled: bool,
}

#[derive(Default, Debug, Deserialize, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "lowercase")]
pub enum ConfirmDiscardOption {
    #[default]
    Line,
    Hunk,
    File,
    Never,
}

#[derive(Default, Debug, Deserialize)]
pub struct StyleConfig {
    pub separator: StyleConfigEntry,

    pub info_msg: StyleConfigEntry,
    pub error_msg: StyleConfigEntry,
    pub command: StyleConfigEntry,

    #[serde(default)]
    pub menu: MenuStyleConfig,

    pub prompt: StyleConfigEntry,

    pub section_header: StyleConfigEntry,
    pub file_header: StyleConfigEntry,
    pub hunk_header: StyleConfigEntry,

    #[serde(default)]
    pub picker: PickerStyleConfig,

    pub cursor: SymbolStyleConfigEntry,
    pub selection_bar: SymbolStyleConfigEntry,
    pub selection_line: StyleConfigEntry,
    pub selection_area: StyleConfigEntry,

    #[serde(default)]
    pub search_match: StyleConfigEntry,

    pub hash: StyleConfigEntry,
    /// The instruction keywords in the interactive rebase todo view.
    #[serde(default)]
    pub rebase_todo: RebaseTodoStyleConfig,
    pub branch: StyleConfigEntry,
    pub remote: StyleConfigEntry,
    pub tag: StyleConfigEntry,
    pub author: StyleConfigEntry,
    pub age: StyleConfigEntry,

    #[serde(default)]
    pub blame: BlameStyleConfig,
}

#[derive(Default, Debug, Deserialize)]
pub struct RebaseTodoStyleConfig {
    #[serde(default)]
    pub reword: StyleConfigEntry,
    #[serde(default)]
    pub edit: StyleConfigEntry,
    #[serde(default)]
    pub squash: StyleConfigEntry,
    #[serde(default)]
    pub fixup: StyleConfigEntry,
    #[serde(default)]
    pub drop: StyleConfigEntry,
}

#[derive(Default, Debug, Deserialize)]
pub struct BlameStyleConfig {
    #[serde(default)]
    pub line_num: StyleConfigEntry,
    #[serde(default)]
    pub code_line: StyleConfigEntry,
}

#[derive(Default, Debug, Deserialize)]
pub struct MenuStyleConfig {
    #[serde(default)]
    pub heading: StyleConfigEntry,
    #[serde(default)]
    pub key: StyleConfigEntry,
    /// Active argument value display (e.g., "--interactive")
    #[serde(default)]
    pub active_arg: StyleConfigEntry,
    /// Inactive argument value display
    #[serde(default)]
    pub inactive_arg: StyleConfigEntry,
}

#[derive(Default, Debug, Deserialize)]
pub struct PickerStyleConfig {
    #[serde(default)]
    pub prompt: StyleConfigEntry,
    #[serde(default)]
    pub info: StyleConfigEntry,
    #[serde(default)]
    pub selection_line: StyleConfigEntry,
    #[serde(default)]
    pub matched: StyleConfigEntry,
}

#[derive(Default, Debug, Deserialize)]
pub struct StyleConfigEntry {
    #[serde(default)]
    fg: Option<Color>,
    #[serde(default)]
    bg: Option<Color>,
    #[serde(default)]
    mods: Option<Modifier>,
}

#[derive(Default, Debug, Deserialize)]
pub struct SymbolStyleConfigEntry {
    #[serde(default)]
    pub symbol: char,
    #[serde(default)]
    fg: Option<Color>,
    #[serde(default)]
    bg: Option<Color>,
    #[serde(default)]
    mods: Option<Modifier>,
}

impl From<&StyleConfigEntry> for Style {
    fn from(val: &StyleConfigEntry) -> Self {
        Style {
            fg: val.fg,
            bg: val.bg,
            add_modifier: val.mods.unwrap_or(Modifier::empty()),
            sub_modifier: Modifier::empty(),
        }
    }
}

impl From<&SymbolStyleConfigEntry> for Style {
    fn from(val: &SymbolStyleConfigEntry) -> Self {
        Style {
            fg: val.fg,
            bg: val.bg,
            add_modifier: val.mods.unwrap_or(Modifier::empty()),
            sub_modifier: Modifier::empty(),
        }
    }
}

pub struct PickerBindings {
    pub next: Vec<Vec<(KeyModifiers, KeyCode)>>,
    pub previous: Vec<Vec<(KeyModifiers, KeyCode)>>,
    pub done: Vec<Vec<(KeyModifiers, KeyCode)>>,
    pub cancel: Vec<Vec<(KeyModifiers, KeyCode)>>,
    pub toggle_list: Vec<Vec<(KeyModifiers, KeyCode)>>,
    pub quit: Vec<Vec<(KeyModifiers, KeyCode)>>,
}

impl TryFrom<PickerBindingsConfig> for PickerBindings {
    type Error = crate::error::Error;

    fn try_from(config: PickerBindingsConfig) -> Result<Self, Self::Error> {
        let mut bad_bindings = Vec::new();

        let next = parse_picker_keys(&config.next, "picker.next", &mut bad_bindings);
        let previous = parse_picker_keys(&config.previous, "picker.previous", &mut bad_bindings);
        let done = parse_picker_keys(&config.done, "picker.done", &mut bad_bindings);
        let cancel = parse_picker_keys(&config.cancel, "picker.cancel", &mut bad_bindings);
        let toggle_list =
            parse_picker_keys(&config.toggle_list, "picker.toggle_list", &mut bad_bindings);
        let quit = parse_picker_keys(&config.quit, "picker.quit", &mut bad_bindings);

        if !bad_bindings.is_empty() {
            return Err(Error::Bindings {
                bad_key_bindings: bad_bindings,
            });
        }

        Ok(Self {
            next,
            previous,
            done,
            cancel,
            toggle_list,
            quit,
        })
    }
}

fn parse_picker_keys(
    raw_keys: &[String],
    action_name: &str,
    bad_bindings: &mut Vec<String>,
) -> Vec<Vec<(KeyModifiers, KeyCode)>> {
    raw_keys
        .iter()
        .filter_map(|keys| {
            if let Ok(("", parsed)) = key_parser::parse_config_keys(keys) {
                Some(parsed)
            } else {
                bad_bindings.push(format!("- {} = {}", action_name, keys));
                None
            }
        })
        .collect()
}

pub fn init_config(path: Option<PathBuf>) -> Res<Config> {
    let git_config = git2::Repository::open_from_env()
        .and_then(|repo| repo.config())
        .or_else(|_| git2::Config::open_default())
        .ok();
    init_config_from(path.unwrap_or_else(config_path), git_config.as_ref())
}

/// The config in the gitu config file at `config_path`, under what `git_config`
/// holds in its `delta-tui` section.
pub(crate) fn init_config_from(
    config_path: PathBuf,
    git_config: Option<&git2::Config>,
) -> Res<Config> {
    if config_path.exists() {
        log::info!("Loading config file at {config_path:?}");
    } else {
        log::info!("No config file at {config_path:?}");
    }

    let defaults = Figment::from(Toml::string(DEFAULT_CONFIG));
    let file = Figment::from(Toml::file(&config_path));
    let git = match git_config {
        Some(git_config) => Figment::from(Serialized::defaults(config_git::read(
            git_config, &defaults,
        )?)),
        None => Figment::new(),
    };
    let FigmentConfig {
        mut general,
        style,
        bindings: bindings_config,
    } = defaults
        .merge(file.clone())
        .merge(git.clone())
        .extract()
        .map_err(Box::new)
        .map_err(Error::Config)?;
    if let Some(feature_keys) = users_feature_keys(&git)?.or(users_feature_keys(&file)?) {
        general.diff_renderer.feature_keys = feature_keys;
    }
    let bindings = Bindings::try_from(bindings_config.menus)?;
    let picker_bindings = PickerBindings::try_from(bindings_config.picker)?;
    check_feature_keys(&general.diff_renderer.feature_keys, &bindings)?;

    Ok(Config {
        general,
        style,
        bindings,
        picker_bindings,
    })
}

/// The feature keys in one layer of the user's config, which are all of them
/// rather than additions to those under it, so that a key can be unset.
fn users_feature_keys(figment: &Figment) -> Res<Option<BTreeMap<String, char>>> {
    if !figment.contains(FEATURE_KEYS) {
        return Ok(None);
    }
    figment
        .extract_inner(FEATURE_KEYS)
        .map(Some)
        .map_err(Box::new)
        .map_err(Error::Config)
}

/// A feature's key is a top-level key, so one that is bound already would do two
/// things.
fn check_feature_keys(feature_keys: &BTreeMap<String, char>, bindings: &Bindings) -> Res<()> {
    let taken: Vec<String> = feature_keys
        .iter()
        .filter(|(_, key)| bindings.binds_at_top_level(**key))
        .map(|(feature, key)| {
            format!("- general.diff_renderer.feature_keys.{feature} = {key} (bound already)")
        })
        .collect();
    if !taken.is_empty() {
        return Err(Error::Bindings {
            bad_key_bindings: taken,
        });
    }
    Ok(())
}

pub fn config_path() -> PathBuf {
    choose_base_strategy()
        .expect("Unable to find the config directory!")
        .config_dir()
        .join("gitu/config.toml")
}

#[cfg(test)]
pub(crate) const TEST_SEARCH_HIGHLIGHT_BG: Color = Color::LightYellow;

#[cfg(test)]
pub(crate) fn init_test_config() -> Res<Config> {
    let FigmentConfig {
        mut general,
        mut style,
        bindings: bindings_config,
    } = Figment::new()
        .merge(Toml::string(DEFAULT_CONFIG))
        .extract()
        .map_err(Box::new)
        .map_err(Error::Config)?;

    general.always_show_help.enabled = false;
    general.refresh_on_file_change.enabled = false;
    general.status_cursor_on_changes = false;
    general.diff_renderer.command = crate::tests::helpers::ui::renderer();

    style.search_match = StyleConfigEntry {
        fg: None,
        bg: Some(TEST_SEARCH_HIGHLIGHT_BG),
        mods: None,
    };

    Ok(Config {
        general,
        style,
        bindings: Bindings::try_from(bindings_config.menus).unwrap(),
        picker_bindings: PickerBindings::try_from(bindings_config.picker).unwrap(),
    })
}

#[cfg(test)]
mod tests {
    use std::fs;

    use crate::style::{Color, Modifier};
    use figment::{
        Figment,
        providers::{Format, Toml},
    };
    use temp_dir::TempDir;

    use super::{Config, DEFAULT_CONFIG, FigmentConfig, init_config_from};

    #[test]
    fn config_merges() {
        let config: FigmentConfig = Figment::new()
            .merge(Toml::string(DEFAULT_CONFIG))
            .merge(Toml::string(
                r#"
                [style]
                hunk_header.bg = "light green"
                "#,
            ))
            .extract()
            .unwrap();

        assert_eq!(config.style.hunk_header.bg, Some(Color::LightGreen));
        assert_eq!(config.style.hunk_header.fg, Some(Color::Blue));
    }

    /// The config read from a gitu config file holding `toml` and git config
    /// holding `gitconfig`.
    fn configured(toml: &str, gitconfig: &str) -> Config {
        let dir = TempDir::new().unwrap();
        let toml_path = dir.path().join("config.toml");
        fs::write(&toml_path, toml).unwrap();
        let git_path = dir.path().join("gitconfig");
        fs::write(&git_path, gitconfig).unwrap();
        let git_config = git2::Config::open(&git_path).unwrap();
        init_config_from(toml_path, Some(&git_config)).unwrap()
    }

    fn config_error(gitconfig: &str) -> String {
        let dir = TempDir::new().unwrap();
        let git_path = dir.path().join("gitconfig");
        fs::write(&git_path, gitconfig).unwrap();
        let git_config = git2::Config::open(&git_path).unwrap();
        match init_config_from(dir.path().join("config.toml"), Some(&git_config)) {
            Ok(_) => panic!("{gitconfig} was accepted"),
            Err(e) => e.to_string(),
        }
    }

    #[test]
    fn git_config_sets_general_settings() {
        let config = configured(
            "",
            "[delta-tui]\n\
             \tstart-folded = true\n\
             \tfile-header = stat\n\
             \tstash-list-limit = 3\n",
        );
        assert!(config.general.start_folded);
        assert_eq!(config.general.file_header, super::FileHeader::Stat);
        assert_eq!(config.general.stash_list_limit, 3);
    }

    /// As git reads booleans and integers.
    #[test]
    fn git_config_values_are_read_as_git_reads_them() {
        let config = configured(
            "",
            "[delta-tui]\n\
             \tstart-folded\n\
             \tvisit-context-lines = no\n\
             \tmax-input-bytes = 4m\n",
        );
        assert!(config.general.start_folded);
        assert!(!config.general.visit_context_lines);
        assert_eq!(config.general.max_input_bytes, 4 * 1024 * 1024);
    }

    /// A list is a key given once per item, as git's own lists are.
    #[test]
    fn git_config_lists_are_repeated_keys() {
        let config = configured(
            "",
            "[delta-tui]\n\
             \tlog-args = --stat\n\
             \tlog-args = --pretty=format:%h %s\n\
             \tshow-args = --stat\n",
        );
        assert_eq!(config.general.log_args, ["--stat", "--pretty=format:%h %s"]);
        assert_eq!(config.general.show_args, ["--stat"]);
    }

    #[test]
    fn git_config_overrides_the_gitu_config_file() {
        let config = configured(
            "[general]\nstart_folded = true\nstash_list_limit = 3\n",
            "[delta-tui]\n\tstart-folded = false\n",
        );
        assert!(!config.general.start_folded);
        assert_eq!(config.general.stash_list_limit, 3);
    }

    #[test]
    fn git_config_subsections_reach_nested_settings() {
        let config = configured(
            "",
            "[delta-tui \"general.diff-renderer\"]\n\
             \tfeatures = side-by-side\n\
             \tfeatures = my-*\n",
        );
        assert_eq!(
            config.general.diff_renderer.features,
            ["side-by-side", "my-*"]
        );
    }

    /// Builtin themes are a list, one key per item, as other lists are.
    #[test]
    fn git_config_lists_builtin_themes() {
        let config = configured(
            "",
            "[delta-tui \"general.diff-renderer\"]\n\tbuiltin-themes = diff-so-fancy\n",
        );
        assert_eq!(
            config.general.diff_renderer.builtin_themes,
            ["diff-so-fancy"]
        );
    }

    /// A feature key is keyed by the feature's own name, dashes and all, and
    /// as in the gitu config file they replace the default ones.
    #[test]
    fn git_config_sets_feature_keys_by_feature_name() {
        let config = configured(
            "",
            "[delta-tui \"general.diff-renderer.feature-keys\"]\n\
             \tmy-feature = x\n",
        );
        assert_eq!(
            config
                .general
                .diff_renderer
                .feature_keys
                .into_iter()
                .collect::<Vec<_>>(),
            [("my-feature".to_string(), 'x')]
        );
    }

    #[test]
    fn git_config_sets_bindings() {
        let config = configured(
            "",
            "[delta-tui \"bindings.root\"]\n\
             \tquit = x\n\
             \tquit = esc\n",
        );
        assert!(config.bindings.binds_at_top_level('x'));
        assert!(!config.bindings.binds_at_top_level('q'));
    }

    /// A style is written as delta writes one: attributes, then a foreground
    /// and a background color. A color it leaves out is unset.
    #[test]
    fn git_config_styles_are_delta_styles() {
        let config = configured(
            "",
            "[delta-tui \"style\"]\n\
             \thunk-header = bold italic \"#ff0000\" 237\n\
             \tfile-header = ul\n\
             \tsection-header = normal blue\n",
        );
        let style = &config.style;
        assert_eq!(style.hunk_header.fg, Some(Color::Rgb(255, 0, 0)));
        assert_eq!(style.hunk_header.bg, Some(Color::Indexed(237)));
        assert_eq!(
            style.hunk_header.mods,
            Some(Modifier::BOLD | Modifier::ITALIC)
        );
        assert_eq!(style.file_header.fg, None);
        assert_eq!(style.file_header.mods, Some(Modifier::UNDERLINED));
        assert_eq!(style.section_header.fg, None);
        assert_eq!(style.section_header.bg, Some(Color::Blue));
    }

    #[test]
    fn a_git_config_style_keeps_its_symbol() {
        let config = configured("", "[delta-tui \"style\"]\n\tcursor = red\n");
        assert_eq!(config.style.cursor.symbol, '▌');
        assert_eq!(config.style.cursor.fg, Some(Color::Red));
    }

    #[test]
    fn a_git_config_style_inside_a_group_of_styles() {
        let config = configured("", "[delta-tui \"style.menu\"]\n\tkey = green\n");
        assert_eq!(config.style.menu.key.fg, Some(Color::Green));
    }

    #[test]
    fn a_bad_git_config_value_is_an_error_naming_the_key() {
        assert!(
            config_error("[delta-tui]\n\tstart-folded = maybe\n")
                .contains("delta-tui.start-folded")
        );
        assert!(
            config_error("[delta-tui \"style\"]\n\thunk-header = bold sky\n")
                .contains("delta-tui.style.hunk-header")
        );
    }
}