summaryrefslogtreecommitdiffstats
path: root/src/subcommands/show_colors.rs
blob: fd19437718cdd9e65da6a02ec3de4f9aef66bb12 (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
70
71
72
73
use crate::cli;
use crate::color;
use crate::colors;
use crate::config;
use crate::delta;
use crate::git_config;
use crate::paint;
use crate::paint::BgShouldFill;
use crate::style;
use crate::utils::bat::assets::HighlightingAssets;
use crate::utils::bat::output::{OutputType, PagingMode};

#[cfg(not(tarpaulin_include))]
pub fn show_colors() -> std::io::Result<()> {
    use itertools::Itertools;

    let assets = HighlightingAssets::new();
    let opt = cli::Opt::from_args_and_git_config(git_config::GitConfig::try_create(), assets);
    let config = config::Config::from(opt);

    let mut output_type =
        OutputType::from_mode(PagingMode::QuitIfOneScreen, None, &config).unwrap();
    let writer = output_type.handle().unwrap();

    let mut painter = paint::Painter::new(writer, &config);
    painter.set_syntax(Some("ts"));
    painter.set_highlighter();

    let title_style = ansi_term::Style::new().bold();
    let mut style = style::Style {
        is_syntax_highlighted: true,
        ..style::Style::default()
    };
    for (group, color_names) in colors::color_groups().iter().sorted() {
        writeln!(painter.writer, "\n\n{}\n", title_style.paint(group))?;
        for (color_name, hex) in color_names {
            // Two syntax-highlighted lines without background color
            style.ansi_term_style.background = None;
            for line in [
                r#"export function color(): string {{ return "none" }}"#,
                r#"export function hex(): string {{ return "none" }}"#,
            ] {
                painter.syntax_highlight_and_paint_line(
                    line,
                    paint::StyleSectionSpecifier::Style(style),
                    delta::State::HunkZero,
                    BgShouldFill::default(),
                )
            }
            // Two syntax-highlighted lines with background color
            let color =
                color::parse_color(color_name, config.true_color, config.git_config.as_ref())
                    .unwrap();
            style.ansi_term_style.background = Some(color);
            for line in [
                &format!(
                    r#"export function color(): string {{ return "{}" }}"#,
                    color_name
                ),
                &format!(r#"export function hex(): string {{ return "{}" }}"#, hex),
            ] {
                painter.syntax_highlight_and_paint_line(
                    line,
                    paint::StyleSectionSpecifier::Style(style),
                    delta::State::HunkZero,
                    BgShouldFill::default(),
                )
            }
            painter.emit()?;
        }
    }
    Ok(())
}