diff: Adjust visuals
4f226cb3e89b0f168a9edbb6c280e3c0b4b67d1d
1 parent
0b239bd6
crates/diff/src/renderer.rs
+20 -24
| 18 | 18 | ||
| 19 | 19 | /// Minimum width before the renderer attempts table-style diff rows. |
|
| 20 | 20 | const MIN_RENDER_COLS: usize = 12; |
|
| 21 | 21 | /// Width of old and new line number cells. |
|
| 22 | 22 | const LINE_NUMBER_WIDTH: usize = 5; |
|
| 23 | + | /// Foreground color used by old and new line number cells. |
|
| 24 | + | const LINE_NUMBER_FG: Color = colors::fixed::DIM; |
|
| 23 | 25 | /// Width of the hunk marker cell. |
|
| 24 | 26 | const MARKER_WIDTH: usize = 3; |
|
| 25 | 27 | /// Scrollbar track glyph (distinct from box-drawing borders so the column reads as a track). |
|
| 26 | 28 | const SCROLLBAR_TRACK: char = '🮘'; |
|
| 27 | 29 | /// Scrollbar thumb glyph. |
| 111 | 113 | } |
|
| 112 | 114 | ||
| 113 | 115 | /// Positive and negative colors used for diff chrome. |
|
| 114 | 116 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
|
| 115 | 117 | pub struct DiffPalette { |
|
| 116 | - | /// Background color for removed line-number cells. |
|
| 117 | - | pub negative_gutter_bg: Color, |
|
| 118 | - | /// Background color for added line-number cells. |
|
| 119 | - | pub positive_gutter_bg: Color, |
|
| 120 | 118 | /// Background color for removed source rows. |
|
| 121 | 119 | pub negative_row_bg: Color, |
|
| 122 | 120 | /// Background color for added source rows. |
|
| 123 | 121 | pub positive_row_bg: Color, |
|
| 124 | 122 | /// Foreground color for removed stats, markers, and labels. |
| 197 | 195 | } |
|
| 198 | 196 | ||
| 199 | 197 | impl Default for DiffPalette { |
|
| 200 | 198 | fn default() -> Self { |
|
| 201 | 199 | Self { |
|
| 202 | - | negative_gutter_bg: colors::rgb::NEGATIVE, |
|
| 203 | - | positive_gutter_bg: colors::rgb::POSITIVE, |
|
| 204 | 200 | negative_row_bg: colors::rgb::NEGATIVE_DARK, |
|
| 205 | 201 | positive_row_bg: colors::rgb::POSITIVE_DARK, |
|
| 206 | 202 | negative_fg: colors::rgb::NEGATIVE_LIGHT, |
|
| 207 | 203 | positive_fg: colors::rgb::POSITIVE_LIGHT, |
|
| 208 | 204 | } |
| 221 | 217 | .unwrap_or(palette.positive_fg); |
|
| 222 | 218 | palette.negative_fg = |
|
| 223 | 219 | Self::theme_color(&theme.colors, "gitDecoration.deletedResourceForeground") |
|
| 224 | 220 | .or_else(|| Self::theme_color(&theme.colors, "editorGutter.deletedBackground")) |
|
| 225 | 221 | .unwrap_or(palette.negative_fg); |
|
| 226 | - | palette.positive_gutter_bg = |
|
| 227 | - | Self::theme_color(&theme.colors, "diffEditor.insertedTextBackground") |
|
| 228 | - | .unwrap_or(palette.positive_gutter_bg); |
|
| 229 | - | palette.negative_gutter_bg = |
|
| 230 | - | Self::theme_color(&theme.colors, "diffEditor.removedTextBackground") |
|
| 231 | - | .unwrap_or(palette.negative_gutter_bg); |
|
| 232 | 222 | palette.positive_row_bg = |
|
| 233 | 223 | Self::theme_color(&theme.colors, "diffEditor.insertedLineBackground") |
|
| 234 | 224 | .unwrap_or(palette.positive_row_bg); |
|
| 235 | 225 | palette.negative_row_bg = |
|
| 236 | 226 | Self::theme_color(&theme.colors, "diffEditor.removedLineBackground") |
| 840 | 830 | meta: DiffLineMeta, |
|
| 841 | 831 | line: &DiffLine, |
|
| 842 | 832 | old_line: u32, |
|
| 843 | 833 | source_cols: usize, |
|
| 844 | 834 | ) -> RenderRow { |
|
| 845 | - | let (cells, source, style) = match line.kind { |
|
| 835 | + | let (cells, source, row_style, source_style) = match line.kind { |
|
| 846 | 836 | DiffLineKind::Addition => ( |
|
| 847 | 837 | [ |
|
| 848 | - | Self::blank_number(self.options.diff_palette.positive_gutter_bg), |
|
| 838 | + | Self::blank_number(self.options.diff_palette.positive_row_bg), |
|
| 849 | 839 | Self::number_cell( |
|
| 850 | 840 | meta.source_line, |
|
| 851 | - | self.options.diff_palette.positive_fg, |
|
| 852 | - | self.options.diff_palette.positive_gutter_bg, |
|
| 841 | + | LINE_NUMBER_FG, |
|
| 842 | + | self.options.diff_palette.positive_row_bg, |
|
| 853 | 843 | ), |
|
| 854 | 844 | Self::marker_cell(" + ", self.options.diff_palette.positive_fg), |
|
| 855 | 845 | ], |
|
| 856 | 846 | self.highlighted_or_plain(&meta, source_cols), |
|
| 857 | 847 | Style::default().bg(self.options.diff_palette.positive_row_bg), |
|
| 848 | + | Style::default().bg(self.options.diff_palette.positive_row_bg), |
|
| 858 | 849 | ), |
|
| 859 | 850 | DiffLineKind::Deletion => ( |
|
| 860 | 851 | [ |
|
| 861 | 852 | Self::number_cell( |
|
| 862 | 853 | meta.source_line, |
|
| 863 | - | self.options.diff_palette.negative_fg, |
|
| 864 | - | self.options.diff_palette.negative_gutter_bg, |
|
| 854 | + | LINE_NUMBER_FG, |
|
| 855 | + | self.options.diff_palette.negative_row_bg, |
|
| 865 | 856 | ), |
|
| 866 | - | Self::blank_number(self.options.diff_palette.negative_gutter_bg), |
|
| 857 | + | Self::blank_number(self.options.diff_palette.negative_row_bg), |
|
| 867 | 858 | Self::marker_cell(" - ", self.options.diff_palette.negative_fg), |
|
| 868 | 859 | ], |
|
| 869 | 860 | self.highlighted_or_plain(&meta, source_cols), |
|
| 861 | + | Style::default().bg(self.options.diff_palette.negative_row_bg), |
|
| 870 | 862 | Style::default() |
|
| 871 | 863 | .bg(self.options.diff_palette.negative_row_bg) |
|
| 872 | 864 | .dim(), |
|
| 873 | 865 | ), |
|
| 874 | 866 | DiffLineKind::Context => ( |
|
| 875 | 867 | [ |
|
| 876 | - | Self::number_cell(old_line, colors::rgb::DIM, colors::rgb::FAINT), |
|
| 877 | - | Self::number_cell(meta.source_line, colors::rgb::DIM, colors::rgb::FAINT), |
|
| 868 | + | Self::number_cell(old_line, LINE_NUMBER_FG, Color::Unset), |
|
| 869 | + | Self::number_cell(meta.source_line, LINE_NUMBER_FG, Color::Unset), |
|
| 878 | 870 | Self::marker_cell(" ", Color::Unset), |
|
| 879 | 871 | ], |
|
| 880 | 872 | self.highlighted_or_plain(&meta, source_cols), |
|
| 881 | 873 | Style::default(), |
|
| 874 | + | Style::default(), |
|
| 882 | 875 | ), |
|
| 883 | 876 | DiffLineKind::NoNewline => ( |
|
| 884 | 877 | [ |
|
| 885 | 878 | Line::blank().padded(LINE_NUMBER_WIDTH), |
|
| 886 | 879 | Line::blank().padded(LINE_NUMBER_WIDTH), |
|
| 887 | 880 | Self::marker_cell(" ", Color::Unset), |
|
| 888 | 881 | ], |
|
| 889 | 882 | Line::new(NO_NEWLINE_MARKER).style(Style::default().fg(colors::rgb::DIM)), |
|
| 890 | 883 | Style::default(), |
|
| 884 | + | Style::default(), |
|
| 891 | 885 | ), |
|
| 892 | 886 | }; |
|
| 893 | - | let rendered = self.hunk_line(cells, source, source_cols, style); |
|
| 887 | + | let rendered = self.hunk_line(cells, source, source_cols, row_style, source_style); |
|
| 894 | 888 | ||
| 895 | 889 | RenderRow { |
|
| 896 | 890 | line: rendered, |
|
| 897 | 891 | diff: Some(meta), |
|
| 898 | 892 | } |
| 902 | 896 | fn hunk_line( |
|
| 903 | 897 | &self, |
|
| 904 | 898 | cells: [Line; 3], |
|
| 905 | 899 | mut source: Line, |
|
| 906 | 900 | source_cols: usize, |
|
| 907 | - | style: Style, |
|
| 901 | + | row_style: Style, |
|
| 902 | + | source_style: Style, |
|
| 908 | 903 | ) -> Line { |
|
| 909 | 904 | let inner = source_cols.saturating_add(LINE_NUMBER_WIDTH * 2 + MARKER_WIDTH); |
|
| 910 | 905 | Line::truncate(&mut source, source_cols, "…"); |
|
| 911 | 906 | Line::pad(&mut source, source_cols); |
|
| 907 | + | let source = source.style(source_style); |
|
| 912 | 908 | let mut line = Line::blank() |
|
| 913 | 909 | .extend(cells[0].clone()) |
|
| 914 | 910 | .extend(cells[1].clone()) |
|
| 915 | 911 | .extend(cells[2].clone()) |
|
| 916 | 912 | .extend(source); |
|
| 917 | 913 | Line::pad(&mut line, inner); |
|
| 918 | 914 | Line::truncate(&mut line, inner, "…"); |
|
| 919 | - | line.style(style) |
|
| 915 | + | line.style(row_style) |
|
| 920 | 916 | } |
|
| 921 | 917 | ||
| 922 | 918 | /// Returns a highlighted line from cache or plain text when no result has arrived. |
|
| 923 | 919 | fn highlighted_or_plain(&self, meta: &DiffLineMeta, cols: usize) -> Line { |
|
| 924 | 920 | let key = HighlightKey::new(HighlightKeyInput { |
a.rs
+0 -0
a.rs
+0 -0
b.rs
+0 -0
a.rs
+0 -0
crates/diff/tests/golden.rs
+1 -1
| 3 | 3 | use std::collections::BTreeSet; |
|
| 4 | 4 | ||
| 5 | 5 | use rustik_diff::{ |
|
| 6 | 6 | Decode, DiffAction, DiffDocument, DiffOptions, DiffViewer, FileId, HighlightResult, |
|
| 7 | 7 | }; |
|
| 8 | - | use rustik_tui::{Buffer, Color, Label, Line, Rect, Scroll}; |
|
| 8 | + | use rustik_tui::{Buffer, Color, Label, Line, Rect, Scroll, Style, colors}; |
|
| 9 | 9 | ||
| 10 | 10 | #[test] |
|
| 11 | 11 | fn expanded_file_card_matches_golden_frame() { |
|
| 12 | 12 | assert_golden( |
|
| 13 | 13 | "expanded_file_card_64x9.txt", |