codevis/render/
mod.rs

1use image::Rgb;
2use syntect::highlighting::Style;
3
4/// Determine the foreground pixel color.
5#[derive(clap::ValueEnum, Clone, Copy, Debug)]
6pub enum FgColor {
7    /// Use the style of the syntax to color the foreground pixel.
8    Style,
9    /// Encode the ascii value into the brightness of the style color
10    StyleAsciiBrightness,
11}
12
13/// Determine the background pixel color.
14#[derive(clap::ValueEnum, Clone, Copy, Debug, Eq, PartialEq)]
15pub enum BgColor {
16    /// Use the style of the syntax to color the background pixel.
17    Style,
18    /// Use the style of the syntax to color the background pixel and modulate it in an even-odd pattern
19    /// to make file borders visible.
20    StyleCheckerboardDarken,
21    /// Use the style of the syntax to color the background pixel and modulate it in an even-odd pattern
22    /// to make file borders visible.
23    StyleCheckerboardBrighten,
24    /// The purple color of the Helix Editor.
25    HelixEditor,
26}
27
28impl BgColor {
29    pub fn to_rgb(&self, style: Style, file_index: usize, color_modulation: f32) -> Rgb<u8> {
30        match self {
31            BgColor::Style => Rgb([style.background.r, style.background.g, style.background.b]),
32            BgColor::HelixEditor => Rgb([59, 34, 76]),
33            BgColor::StyleCheckerboardDarken | BgColor::StyleCheckerboardBrighten => {
34                let m = if self == &BgColor::StyleCheckerboardBrighten {
35                    if file_index % 2 == 0 {
36                        1.0 + color_modulation
37                    } else {
38                        1.0
39                    }
40                } else {
41                    (file_index % 2 == 0)
42                        .then_some(1.0)
43                        .unwrap_or_else(|| (1.0_f32 - color_modulation).max(0.0))
44                };
45                Rgb([
46                    (style.background.r as f32 * m).min(255.0) as u8,
47                    (style.background.g as f32 * m).min(255.0) as u8,
48                    (style.background.b as f32 * m).min(255.0) as u8,
49                ])
50            }
51        }
52    }
53}
54
55/// Configure how to render an image.
56#[derive(Debug, Copy, Clone)]
57pub struct Options<'a> {
58    /// How many characters wide each column is.
59    pub column_width: u32,
60    /// How many pixels high each line is.
61    pub line_height: u32,
62    /// Whether to render the image in a readable way.
63    pub readable: bool,
64
65    /// Whether or not to write the file path and name at the top of each file.
66    pub show_filenames: bool,
67
68    pub target_aspect_ratio: f64,
69
70    /// The number of threads to use for rendering.
71    pub threads: usize,
72    pub highlight_truncated_lines: bool,
73
74    pub fg_color: FgColor,
75    pub bg_color: BgColor,
76    /// The color theme to use.
77    pub theme: &'a str,
78
79    /// Sacrifice aspect ratio to fill the image with full columns.
80    pub force_full_columns: bool,
81    /// Whether to ignore files without syntactic highlighting.
82    pub ignore_files_without_syntax: bool,
83    pub plain: bool,
84    pub display_to_be_processed_file: bool,
85    pub color_modulation: f32,
86    /// The number of spaces to use for a tab character.
87    pub tab_spaces: u32,
88    pub line_nums: bool,
89}
90
91impl Default for Options<'_> {
92    fn default() -> Self {
93        Options {
94            column_width: 100,
95            line_height: 2,
96            readable: false,
97            show_filenames: false,
98            target_aspect_ratio: 16. / 9.,
99            threads: num_cpus::get(),
100            highlight_truncated_lines: false,
101            fg_color: FgColor::StyleAsciiBrightness,
102            bg_color: BgColor::Style,
103            theme: "Solarized (dark)",
104            force_full_columns: true,
105            ignore_files_without_syntax: false,
106            plain: false,
107            display_to_be_processed_file: false,
108            color_modulation: 0.3,
109            tab_spaces: 4,
110            line_nums: false,
111        }
112    }
113}
114
115mod highlight;
116use highlight::Cache;
117
118pub(crate) mod function;
119
120mod chunk;
121
122mod dimension;
123use dimension::Dimension;