| 1 | use std::io::Write; |
| 2 | |
| 3 | use itertools::Itertools; |
| 4 | |
| 5 | use crate::cli; |
| 6 | use crate::config; |
| 7 | use crate::features::side_by_side::{Left, Right}; |
| 8 | use crate::minusplus::*; |
| 9 | use crate::paint::BgFillMethod; |
| 10 | use crate::style; |
| 11 | use crate::utils::bat::output::PagingMode; |
| 12 | |
| 13 | pub fn show_config(config: &config::Config, writer: &mut dyn Write) -> std::io::Result<()> { |
| 14 | // styles first |
| 15 | writeln!( |
| 16 | writer, |
| 17 | " commit-style = {commit_style} |
| 18 | file-style = {file_style} |
| 19 | hunk-header-style = {hunk_header_style} |
| 20 | minus-style = {minus_style} |
| 21 | minus-non-emph-style = {minus_non_emph_style} |
| 22 | minus-emph-style = {minus_emph_style} |
| 23 | minus-empty-line-marker-style = {minus_empty_line_marker_style} |
| 24 | zero-style = {zero_style} |
| 25 | plus-style = {plus_style} |
| 26 | plus-non-emph-style = {plus_non_emph_style} |
| 27 | plus-emph-style = {plus_emph_style} |
| 28 | plus-empty-line-marker-style = {plus_empty_line_marker_style} |
| 29 | grep-file-style = {grep_file_style} |
| 30 | grep-line-number-style = {grep_line_number_style} |
| 31 | whitespace-error-style = {whitespace_error_style} |
| 32 | blame-palette = {blame_palette}", |
| 33 | blame_palette = config |
| 34 | .blame_palette |
| 35 | .iter() |
| 36 | .map(|s| style::paint_color_string(s, config.true_color, config.git_config())) |
| 37 | .join(" "), |
| 38 | commit_style = config.commit_style.to_painted_string(), |
| 39 | file_style = config.file_style.to_painted_string(), |
| 40 | hunk_header_style = config.hunk_header_style.to_painted_string(), |
| 41 | minus_emph_style = config.minus_emph_style.to_painted_string(), |
| 42 | minus_empty_line_marker_style = config.minus_empty_line_marker_style.to_painted_string(), |
| 43 | minus_non_emph_style = config.minus_non_emph_style.to_painted_string(), |
| 44 | minus_style = config.minus_style.to_painted_string(), |
| 45 | plus_emph_style = config.plus_emph_style.to_painted_string(), |
| 46 | plus_empty_line_marker_style = config.plus_empty_line_marker_style.to_painted_string(), |
| 47 | plus_non_emph_style = config.plus_non_emph_style.to_painted_string(), |
| 48 | plus_style = config.plus_style.to_painted_string(), |
| 49 | grep_file_style = config.grep_file_style.to_painted_string(), |
| 50 | grep_line_number_style = config.grep_line_number_style.to_painted_string(), |
| 51 | whitespace_error_style = config.whitespace_error_style.to_painted_string(), |
| 52 | zero_style = config.zero_style.to_painted_string(), |
| 53 | )?; |
| 54 | // Everything else |
| 55 | writeln!( |
| 56 | writer, |
| 57 | " true-color = {true_color} |
| 58 | file-added-label = {file_added_label} |
| 59 | file-modified-label = {file_modified_label} |
| 60 | file-removed-label = {file_removed_label} |
| 61 | file-renamed-label = {file_renamed_label} |
| 62 | right-arrow = {right_arrow}", |
| 63 | true_color = config.true_color, |
| 64 | file_added_label = format_option_value(&config.file_added_label), |
| 65 | file_modified_label = format_option_value(&config.file_modified_label), |
| 66 | file_removed_label = format_option_value(&config.file_removed_label), |
| 67 | file_renamed_label = format_option_value(&config.file_renamed_label), |
| 68 | right_arrow = format_option_value(&config.right_arrow), |
| 69 | )?; |
| 70 | writeln!( |
| 71 | writer, |
| 72 | " hyperlinks = {hyperlinks}", |
| 73 | hyperlinks = config.hyperlinks |
| 74 | )?; |
| 75 | if config.hyperlinks { |
| 76 | writeln!( |
| 77 | writer, |
| 78 | " hyperlinks-file-link-format = {hyperlinks_file_link_format}", |
| 79 | hyperlinks_file_link_format = format_option_value(&config.hyperlinks_file_link_format), |
| 80 | )? |
| 81 | } |
| 82 | writeln!( |
| 83 | writer, |
| 84 | " inspect-raw-lines = {inspect_raw_lines} |
| 85 | keep-plus-minus-markers = {keep_plus_minus_markers}", |
| 86 | inspect_raw_lines = match config.inspect_raw_lines { |
| 87 | cli::InspectRawLines::True => "true", |
| 88 | cli::InspectRawLines::False => "false", |
| 89 | }, |
| 90 | keep_plus_minus_markers = config.keep_plus_minus_markers, |
| 91 | )?; |
| 92 | writeln!( |
| 93 | writer, |
| 94 | " line-numbers = {line_numbers}", |
| 95 | line_numbers = config.line_numbers |
| 96 | )?; |
| 97 | if config.line_numbers { |
| 98 | writeln!( |
| 99 | writer, |
| 100 | " line-numbers-minus-style = {line_numbers_minus_style} |
| 101 | line-numbers-zero-style = {line_numbers_zero_style} |
| 102 | line-numbers-plus-style = {line_numbers_plus_style} |
| 103 | line-numbers-left-style = {line_numbers_left_style} |
| 104 | line-numbers-right-style = {line_numbers_right_style} |
| 105 | line-numbers-left-format = {line_numbers_left_format} |
| 106 | line-numbers-right-format = {line_numbers_right_format}", |
| 107 | line_numbers_minus_style = |
| 108 | config.line_numbers_style_minusplus[Minus].to_painted_string(), |
| 109 | line_numbers_zero_style = config.line_numbers_zero_style.to_painted_string(), |
| 110 | line_numbers_plus_style = config.line_numbers_style_minusplus[Plus].to_painted_string(), |
| 111 | line_numbers_left_style = config.line_numbers_style_leftright[Left].to_painted_string(), |
| 112 | line_numbers_right_style = |
| 113 | config.line_numbers_style_leftright[Right].to_painted_string(), |
| 114 | line_numbers_left_format = format_option_value(&config.line_numbers_format[Left]), |
| 115 | line_numbers_right_format = format_option_value(&config.line_numbers_format[Right]), |
| 116 | )? |
| 117 | } |
| 118 | writeln!( |
| 119 | writer, |
| 120 | " max-line-distance = {max_line_distance} |
| 121 | max-line-length = {max_line_length} |
| 122 | diff-stat-align-width = {diff_stat_align_width} |
| 123 | line-fill-method = {line_fill_method} |
| 124 | navigate = {navigate} |
| 125 | navigate-regex = {navigate_regex} |
| 126 | pager = {pager} |
| 127 | paging = {paging_mode} |
| 128 | side-by-side = {side_by_side} |
| 129 | syntax-theme = {syntax_theme} |
| 130 | width = {width} |
| 131 | tabs = {tab_width} |
| 132 | word-diff-regex = {tokenization_regex}", |
| 133 | diff_stat_align_width = config.diff_stat_align_width, |
| 134 | max_line_distance = config.max_line_distance, |
| 135 | max_line_length = config.max_line_length, |
| 136 | line_fill_method = match config.line_fill_method { |
| 137 | BgFillMethod::TryAnsiSequence => "ansi", |
| 138 | BgFillMethod::Spaces => "spaces", |
| 139 | }, |
| 140 | navigate = config.navigate, |
| 141 | navigate_regex = match &config.navigate_regex { |
| 142 | None => "".to_string(), |
| 143 | Some(s) => format_option_value(s), |
| 144 | }, |
| 145 | pager = config.pager.clone().unwrap_or_else(|| "none".to_string()), |
| 146 | paging_mode = match config.paging_mode { |
| 147 | PagingMode::Always => "always", |
| 148 | PagingMode::Never => "never", |
| 149 | PagingMode::QuitIfOneScreen => "auto", |
| 150 | PagingMode::Capture => unreachable!("capture can not be set"), |
| 151 | }, |
| 152 | side_by_side = config.side_by_side, |
| 153 | syntax_theme = config |
| 154 | .syntax_theme |
| 155 | .clone() |
| 156 | .map(|t| t.name.unwrap_or_else(|| "none".to_string())) |
| 157 | .unwrap_or_else(|| "none".to_string()), |
| 158 | width = match config.decorations_width { |
| 159 | cli::Width::Fixed(width) => width.to_string(), |
| 160 | cli::Width::Variable => "variable".to_string(), |
| 161 | }, |
| 162 | tab_width = config.tab_cfg.width(), |
| 163 | tokenization_regex = format_option_value(config.tokenization_regex.to_string()), |
| 164 | )?; |
| 165 | Ok(()) |
| 166 | } |
| 167 | |
| 168 | // Heuristics determining whether to quote string option values when printing values intended for |
| 169 | // git config. |
| 170 | fn format_option_value<S>(s: S) -> String |
| 171 | where |
| 172 | S: AsRef<str>, |
| 173 | { |
| 174 | let s = s.as_ref(); |
| 175 | if s.ends_with(' ') |
| 176 | || s.starts_with(' ') |
| 177 | || s.contains(&['\\', '{', '}', ':'][..]) |
| 178 | || s.is_empty() |
| 179 | { |
| 180 | format!("'{s}'") |
| 181 | } else { |
| 182 | s.to_string() |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | #[cfg(test)] |
| 187 | mod tests { |
| 188 | use crate::tests::integration_test_utils; |
| 189 | |
| 190 | use super::*; |
| 191 | use crate::ansi; |
| 192 | use std::io::{Cursor, Read, Seek}; |
| 193 | |
| 194 | #[test] |
| 195 | fn test_show_config() { |
| 196 | let config = integration_test_utils::make_config_from_args(&[]); |
| 197 | let mut writer = Cursor::new(vec![0; 1024]); |
| 198 | show_config(&config, &mut writer).unwrap(); |
| 199 | let mut s = String::new(); |
| 200 | writer.rewind().unwrap(); |
| 201 | writer.read_to_string(&mut s).unwrap(); |
| 202 | let s = ansi::strip_ansi_codes(&s); |
| 203 | assert!(s.contains(" commit-style = raw\n")); |
| 204 | assert!(s.contains(r" word-diff-regex = '\w+'")); |
| 205 | } |
| 206 | } |