Debian Codegraph @

src/subcommands/show_colors.rs

raw symbols SCIP
1 use crate::cli;
2 use crate::color;
3 use crate::colors;
4 use crate::config;
5 use crate::delta;
6 use crate::env::DeltaEnv;
7 use crate::paint;
8 use crate::paint::BgShouldFill;
9 use crate::style;
10 use crate::utils::bat::output::{OutputType, PagingMode};
11
12 #[cfg(not(tarpaulin_include))]
13 pub fn show_colors() -> std::io::Result<()> {
14 use crate::{delta::DiffType, utils};
15
16 let args = std::env::args_os().collect::<Vec<_>>();
17 let env = DeltaEnv::default();
18 let assets = utils::bat::assets::load_highlighting_assets();
19
20 let opt = match cli::Opt::from_args_and_git_config(args, &env, assets) {
21 (cli::Call::Delta(_), Some(opt)) => opt,
22 _ => panic!("non-Delta Call variant should not occur here"),
23 };
24
25 let config = config::Config::from(opt);
26 let pagercfg = (&config).into();
27
28 let mut output_type =
29 OutputType::from_mode(&env, PagingMode::QuitIfOneScreen, None, &pagercfg).unwrap();
30 let writer = output_type.handle().unwrap();
31
32 let mut painter = paint::Painter::new(writer, &config);
33 painter.set_syntax(Some("a.ts"));
34 painter.set_highlighter();
35
36 let title_style = ansi_term::Style::new().bold();
37 let mut style = style::Style {
38 is_syntax_highlighted: true,
39 ..style::Style::default()
40 };
41 for (group, color_names) in colors::color_groups() {
42 writeln!(painter.writer, "\n\n{}\n", title_style.paint(group))?;
43 for (color_name, hex) in color_names {
44 // Two syntax-highlighted lines without background color
45 style.ansi_term_style.background = None;
46 for line in [
47 r#"export function color(): string {{ return "none" }}"#,
48 r#"export function hex(): string {{ return "none" }}"#,
49 ] {
50 painter.syntax_highlight_and_paint_line(
51 line,
52 paint::StyleSectionSpecifier::Style(style),
53 delta::State::HunkZero(DiffType::Unified, None),
54 BgShouldFill::default(),
55 )
56 }
57 // Two syntax-highlighted lines with background color
58 let color =
59 color::parse_color(color_name, config.true_color, config.git_config()).unwrap();
60 style.ansi_term_style.background = Some(color);
61 for line in [
62 &format!(r#"export function color(): string {{ return "{color_name}" }}"#),
63 &format!(r#"export function hex(): string {{ return "{hex}" }}"#),
64 ] {
65 painter.syntax_highlight_and_paint_line(
66 line,
67 paint::StyleSectionSpecifier::Style(style),
68 delta::State::HunkZero(DiffType::Unified, None),
69 BgShouldFill::default(),
70 )
71 }
72 painter.emit()?;
73 }
74 }
75 Ok(())
76 }