summaryrefslogtreecommitdiffstats
path: root/src/subcommands/show_themes.rs
blob: ba530b6af526ff5cbf3205c456593086ac00916c (plain)
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
use std::io::{self, ErrorKind, Read};

use crate::cli;
use crate::config;
use crate::delta;
use crate::env::DeltaEnv;
use crate::git_config;
use crate::options::get::get_themes;
use crate::utils::bat::output::{OutputType, PagingMode};

pub fn show_themes(dark: bool, light: bool, computed_theme_is_light: bool) -> std::io::Result<()> {
    use std::io::BufReader;

    use bytelines::ByteLines;

    use super::sample_diff::DIFF;

    let mut input = DIFF.to_vec();

    if !atty::is(atty::Stream::Stdin) {
        let mut buf = Vec::new();
        io::stdin().lock().read_to_end(&mut buf)?;
        if !buf.is_empty() {
            input = buf;
        }
    };

    let env = DeltaEnv::default();
    let git_config = git_config::GitConfig::try_create(&env);
    let opt = cli::Opt::from_iter_and_git_config(
        env.clone(),
        &["delta", "--navigate", "--show-themes"],
        git_config,
    );
    let mut output_type =
        OutputType::from_mode(&env, PagingMode::Always, None, &config::Config::from(opt)).unwrap();
    let title_style = ansi_term::Style::new().bold();
    let writer = output_type.handle().unwrap();

    for theme in &get_themes(git_config::GitConfig::try_create(&env)) {
        let git_config = git_config::GitConfig::try_create(&env);
        let opt = cli::Opt::from_iter_and_git_config(
            env.clone(),
            &["delta", "--features", theme],
            git_config,
        );
        let is_dark_theme = opt.dark;
        let is_light_theme = opt.light;
        let config = config::Config::from(opt);

        if (!computed_theme_is_light && is_dark_theme)
            || (computed_theme_is_light && is_light_theme)
            || (dark && light)
        {
            writeln!(writer, "\n\nTheme: {}\n", title_style.paint(theme))?;

            if let Err(error) =
                delta::delta(ByteLines::new(BufReader::new(&input[0..])), writer, &config)
            {
                match error.kind() {
                    ErrorKind::BrokenPipe => std::process::exit(0),
                    _ => eprintln!("{error}"),
                }
            }
        }
    }

    Ok(())
}