| 1 |
use itertools::Itertools; |
| 2 |
use syntect::highlighting::Style as SyntectStyle; |
| 3 |
use unicode_width::UnicodeWidthStr; |
| 4 |
|
| 5 |
use crate::ansi; |
| 6 |
use crate::cli; |
| 7 |
use crate::config::{self, delta_unreachable, Config}; |
| 8 |
use crate::delta::DiffType; |
| 9 |
use crate::delta::State; |
| 10 |
use crate::edits; |
| 11 |
use crate::features::{line_numbers, OptionValueFunction}; |
| 12 |
use crate::minusplus::*; |
| 13 |
use crate::paint::{BgFillMethod, BgShouldFill, LineSections, Painter}; |
| 14 |
use crate::style::Style; |
| 15 |
use crate::wrapping::{wrap_minusplus_block, wrap_zero_block}; |
| 16 |
|
| 17 |
pub fn make_feature() -> Vec<(String, OptionValueFunction)> { |
| 18 |
builtin_feature!([ |
| 19 |
( |
| 20 |
"side-by-side", |
| 21 |
bool, |
| 22 |
None, |
| 23 |
_opt => true |
| 24 |
), |
| 25 |
("features", bool, None, _opt => "line-numbers"), |
| 26 |
("line-numbers-left-format", String, None, _opt => "│{nm:^4}│".to_string()), |
| 27 |
("line-numbers-right-format", String, None, _opt => "│{np:^4}│".to_string()) |
| 28 |
]) |
| 29 |
} |
| 30 |
|
| 31 |
// Aliases for Minus/Plus because Left/Right and PanelSide makes |
| 32 |
// more sense in a side-by-side context. |
| 33 |
pub use crate::minusplus::MinusPlusIndex as PanelSide; |
| 34 |
pub use MinusPlusIndex::Minus as Left; |
| 35 |
pub use MinusPlusIndex::Plus as Right; |
| 36 |
|
| 37 |
use super::line_numbers::LineNumbersData; |
| 38 |
|
| 39 |
#[derive(Debug, Clone)] |
| 40 |
pub struct Panel { |
| 41 |
pub width: usize, |
| 42 |
} |
| 43 |
|
| 44 |
pub type LeftRight<T> = MinusPlus<T>; |
| 45 |
|
| 46 |
pub type SideBySideData = LeftRight<Panel>; |
| 47 |
|
| 48 |
impl SideBySideData { |
| 49 |
/// Create a [`LeftRight<Panel>`](LeftRight<Panel>) named [`SideBySideData`]. |
| 50 |
pub fn new_sbs(decorations_width: &cli::Width, available_terminal_width: &usize) -> Self { |
| 51 |
let panel_width = match decorations_width { |
| 52 |
cli::Width::Fixed(w) => w / 2, |
| 53 |
_ => available_terminal_width / 2, |
| 54 |
}; |
| 55 |
SideBySideData::new(Panel { width: panel_width }, Panel { width: panel_width }) |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
pub fn available_line_width( |
| 60 |
config: &Config, |
| 61 |
data: &line_numbers::LineNumbersData, |
| 62 |
) -> line_numbers::SideBySideLineWidth { |
| 63 |
let line_numbers_width = data.formatted_width(); |
| 64 |
|
| 65 |
// The width can be reduced by the line numbers and/or |
| 66 |
// a possibly added/restored 1-wide "+/-/ " prefix. |
| 67 |
let line_width = |side: PanelSide| { |
| 68 |
config.side_by_side_data[side] |
| 69 |
.width |
| 70 |
.saturating_sub(line_numbers_width[side]) |
| 71 |
.saturating_sub(config.keep_plus_minus_markers as usize) |
| 72 |
}; |
| 73 |
|
| 74 |
LeftRight::new(line_width(Left), line_width(Right)) |
| 75 |
} |
| 76 |
|
| 77 |
pub fn line_is_too_long(line: &str, line_width: usize) -> bool { |
| 78 |
debug_assert!(line.ends_with('\n')); |
| 79 |
|
| 80 |
// graphemes will take care of newlines |
| 81 |
line.width() > line_width |
| 82 |
} |
| 83 |
|
| 84 |
/// Return whether any of the input lines is too long, and a data |
| 85 |
/// structure indicating which of the input lines are too long. This avoids |
| 86 |
/// recalculating the length later. |
| 87 |
pub fn has_long_lines( |
| 88 |
lines: &LeftRight<&Vec<(String, State)>>, |
| 89 |
line_width: &line_numbers::SideBySideLineWidth, |
| 90 |
) -> (bool, LeftRight<Vec<bool>>) { |
| 91 |
let mut wrap_any = LeftRight::default(); |
| 92 |
let mut wrapping_lines = LeftRight::default(); |
| 93 |
|
| 94 |
let mut check_if_too_long = |side| { |
| 95 |
let lines_side: &[(String, State)] = lines[side]; |
| 96 |
wrapping_lines[side] = lines_side |
| 97 |
.iter() |
| 98 |
.map(|(line, _)| line_is_too_long(line, line_width[side])) |
| 99 |
.inspect(|b| wrap_any[side] |= b) |
| 100 |
.collect(); |
| 101 |
}; |
| 102 |
|
| 103 |
check_if_too_long(Left); |
| 104 |
check_if_too_long(Right); |
| 105 |
|
| 106 |
(wrap_any[Left] || wrap_any[Right], wrapping_lines) |
| 107 |
} |
| 108 |
|
| 109 |
#[allow(clippy::too_many_arguments)] |
| 110 |
pub fn paint_minus_and_plus_lines_side_by_side( |
| 111 |
lines: LeftRight<&Vec<(String, State)>>, |
| 112 |
syntax_sections: LeftRight<Vec<LineSections<SyntectStyle>>>, |
| 113 |
diff_sections: LeftRight<Vec<LineSections<Style>>>, |
| 114 |
lines_have_homolog: LeftRight<Vec<bool>>, |
| 115 |
line_alignment: Vec<(Option<usize>, Option<usize>)>, |
| 116 |
line_numbers_data: &mut Option<LineNumbersData>, |
| 117 |
output_buffer: &mut String, |
| 118 |
config: &config::Config, |
| 119 |
) { |
| 120 |
let line_states = LeftRight::new( |
| 121 |
lines[Left].iter().map(|(_, state)| state.clone()).collect(), |
| 122 |
lines[Right] |
| 123 |
.iter() |
| 124 |
.map(|(_, state)| state.clone()) |
| 125 |
.collect(), |
| 126 |
); |
| 127 |
|
| 128 |
let line_numbers_data = line_numbers_data |
| 129 |
.as_mut() |
| 130 |
.unwrap_or_else(|| delta_unreachable("side-by-side requires Some(line_numbers_data)")); |
| 131 |
|
| 132 |
let bg_should_fill = LeftRight::new( |
| 133 |
// Using an ANSI sequence to fill the left panel would not work. |
| 134 |
BgShouldFill::With(BgFillMethod::Spaces), |
| 135 |
// Use what is configured for the right side. |
| 136 |
BgShouldFill::With(config.line_fill_method), |
| 137 |
); |
| 138 |
|
| 139 |
// Only set `should_wrap` to true if wrapping is wanted and lines which are |
| 140 |
// too long are found. |
| 141 |
// If so, remember the calculated line width and which of the lines are too |
| 142 |
// long for later re-use. |
| 143 |
let (should_wrap, line_width, long_lines) = { |
| 144 |
if config.wrap_config.max_lines == 1 { |
| 145 |
(false, LeftRight::default(), LeftRight::default()) |
| 146 |
} else { |
| 147 |
let line_width = available_line_width(config, line_numbers_data); |
| 148 |
|
| 149 |
let (should_wrap, long_lines) = has_long_lines(&lines, &line_width); |
| 150 |
|
| 151 |
(should_wrap, line_width, long_lines) |
| 152 |
} |
| 153 |
}; |
| 154 |
|
| 155 |
let (line_alignment, line_states, syntax_sections, diff_sections) = if should_wrap { |
| 156 |
// Calculated for syntect::highlighting::style::Style and delta::Style |
| 157 |
wrap_minusplus_block( |
| 158 |
config, |
| 159 |
syntax_sections, |
| 160 |
diff_sections, |
| 161 |
&line_alignment, |
| 162 |
&line_width, |
| 163 |
&long_lines, |
| 164 |
) |
| 165 |
} else { |
| 166 |
(line_alignment, line_states, syntax_sections, diff_sections) |
| 167 |
}; |
| 168 |
let lines_have_homolog = if should_wrap { |
| 169 |
edits::make_lines_have_homolog(&line_alignment) |
| 170 |
} else { |
| 171 |
lines_have_homolog |
| 172 |
}; |
| 173 |
|
| 174 |
for (minus_line_index, plus_line_index) in line_alignment { |
| 175 |
let left_state = match minus_line_index { |
| 176 |
Some(i) => &line_states[Left][i], |
| 177 |
None => &State::HunkMinus(DiffType::Unified, None), |
| 178 |
}; |
| 179 |
output_buffer.push_str(&paint_left_panel_minus_line( |
| 180 |
minus_line_index, |
| 181 |
&syntax_sections[Left], |
| 182 |
&diff_sections[Left], |
| 183 |
&lines_have_homolog[Left], |
| 184 |
left_state, |
| 185 |
&mut Some(line_numbers_data), |
| 186 |
bg_should_fill[Left], |
| 187 |
config, |
| 188 |
)); |
| 189 |
|
| 190 |
let right_state = match plus_line_index { |
| 191 |
Some(i) => &line_states[Right][i], |
| 192 |
None => &State::HunkPlus(DiffType::Unified, None), |
| 193 |
}; |
| 194 |
output_buffer.push_str(&paint_right_panel_plus_line( |
| 195 |
plus_line_index, |
| 196 |
&syntax_sections[Right], |
| 197 |
&diff_sections[Right], |
| 198 |
&lines_have_homolog[Right], |
| 199 |
right_state, |
| 200 |
&mut Some(line_numbers_data), |
| 201 |
bg_should_fill[Right], |
| 202 |
config, |
| 203 |
)); |
| 204 |
output_buffer.push('\n'); |
| 205 |
|
| 206 |
// HACK: The left line number is not getting incremented in `linenumbers_and_styles()` |
| 207 |
// when the alignment matches a minus with a plus line, so fix that here and take |
| 208 |
// wrapped lines into account. |
| 209 |
// Similarly an increment happens when it should not, so undo that. |
| 210 |
// TODO: Pass this information down into `paint_line()` to set `increment` accordingly. |
| 211 |
match (left_state, right_state, minus_line_index, plus_line_index) { |
| 212 |
(State::HunkMinusWrapped, State::HunkPlus(_, _), Some(_), None) => { |
| 213 |
line_numbers_data.line_number[Left] = |
| 214 |
line_numbers_data.line_number[Left].saturating_sub(1) |
| 215 |
} |
| 216 |
// Duplicating the logic from `linenumbers_and_styles()` a bit: |
| 217 |
(State::HunkMinusWrapped | State::HunkPlusWrapped, _, _, _) => {} |
| 218 |
(_, _, Some(_), Some(_)) => line_numbers_data.line_number[Left] += 1, |
| 219 |
_ => {} |
| 220 |
} |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
#[allow(clippy::too_many_arguments)] |
| 225 |
pub fn paint_zero_lines_side_by_side<'a>( |
| 226 |
line: &str, |
| 227 |
syntax_style_sections: Vec<LineSections<'a, SyntectStyle>>, |
| 228 |
diff_style_sections: Vec<LineSections<'a, Style>>, |
| 229 |
output_buffer: &mut String, |
| 230 |
config: &Config, |
| 231 |
line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>, |
| •232 |
painted_prefix: Option<ansi_term::AnsiString>, |
| 233 |
background_color_extends_to_terminal_width: BgShouldFill, |
| 234 |
) { |
| 235 |
let states = vec![State::HunkZero(DiffType::Unified, None)]; |
| 236 |
|
| 237 |
let (states, syntax_style_sections, diff_style_sections) = wrap_zero_block( |
| 238 |
config, |
| 239 |
line, |
| 240 |
states, |
| 241 |
syntax_style_sections, |
| 242 |
diff_style_sections, |
| 243 |
line_numbers_data, |
| 244 |
); |
| 245 |
|
| 246 |
for (line_index, ((syntax_sections, diff_sections), state)) in syntax_style_sections |
| 247 |
.into_iter() |
| 248 |
.zip_eq(diff_style_sections.iter()) |
| 249 |
.zip_eq(states.into_iter()) |
| 250 |
.enumerate() |
| 251 |
{ |
| 252 |
for panel_side in &[Left, Right] { |
| 253 |
let (mut panel_line, panel_line_is_empty) = Painter::paint_line( |
| 254 |
&syntax_sections, |
| 255 |
diff_sections, |
| 256 |
&state, |
| 257 |
line_numbers_data, |
| 258 |
Some(*panel_side), |
| 259 |
painted_prefix.clone(), |
| 260 |
config, |
| 261 |
); |
| 262 |
pad_panel_line_to_width( |
| 263 |
&mut panel_line, |
| 264 |
panel_line_is_empty, |
| 265 |
Some(line_index), |
| 266 |
&diff_style_sections, |
| 267 |
None, |
| 268 |
&state, |
| 269 |
*panel_side, |
| 270 |
background_color_extends_to_terminal_width, |
| 271 |
config, |
| 272 |
); |
| 273 |
output_buffer.push_str(&panel_line); |
| 274 |
} |
| 275 |
output_buffer.push('\n'); |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
#[allow(clippy::too_many_arguments)] |
| 280 |
fn paint_left_panel_minus_line<'a>( |
| 281 |
line_index: Option<usize>, |
| 282 |
syntax_style_sections: &[LineSections<'a, SyntectStyle>], |
| 283 |
diff_style_sections: &[LineSections<'a, Style>], |
| 284 |
lines_have_homolog: &[bool], |
| 285 |
state: &'a State, |
| 286 |
line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>, |
| 287 |
background_color_extends_to_terminal_width: BgShouldFill, |
| 288 |
config: &Config, |
| 289 |
) -> String { |
| 290 |
let (mut panel_line, panel_line_is_empty) = paint_minus_or_plus_panel_line( |
| 291 |
line_index, |
| 292 |
syntax_style_sections, |
| 293 |
diff_style_sections, |
| 294 |
state, |
| 295 |
line_numbers_data, |
| 296 |
Left, |
| 297 |
config, |
| 298 |
); |
| 299 |
pad_panel_line_to_width( |
| 300 |
&mut panel_line, |
| 301 |
panel_line_is_empty, |
| 302 |
line_index, |
| 303 |
diff_style_sections, |
| 304 |
Some(lines_have_homolog), |
| 305 |
state, |
| 306 |
Left, |
| 307 |
background_color_extends_to_terminal_width, |
| 308 |
config, |
| 309 |
); |
| 310 |
|
| 311 |
panel_line |
| 312 |
} |
| 313 |
|
| 314 |
#[allow(clippy::too_many_arguments)] |
| 315 |
fn paint_right_panel_plus_line<'a>( |
| 316 |
line_index: Option<usize>, |
| 317 |
syntax_style_sections: &[LineSections<'a, SyntectStyle>], |
| 318 |
diff_style_sections: &[LineSections<'a, Style>], |
| 319 |
lines_have_homolog: &[bool], |
| 320 |
state: &'a State, |
| 321 |
line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>, |
| 322 |
background_color_extends_to_terminal_width: BgShouldFill, |
| 323 |
config: &Config, |
| 324 |
) -> String { |
| 325 |
let (mut panel_line, panel_line_is_empty) = paint_minus_or_plus_panel_line( |
| 326 |
line_index, |
| 327 |
syntax_style_sections, |
| 328 |
diff_style_sections, |
| 329 |
state, |
| 330 |
line_numbers_data, |
| 331 |
Right, |
| 332 |
config, |
| 333 |
); |
| 334 |
|
| 335 |
pad_panel_line_to_width( |
| 336 |
&mut panel_line, |
| 337 |
panel_line_is_empty, |
| 338 |
line_index, |
| 339 |
diff_style_sections, |
| 340 |
Some(lines_have_homolog), |
| 341 |
state, |
| 342 |
Right, |
| 343 |
background_color_extends_to_terminal_width, |
| 344 |
config, |
| 345 |
); |
| 346 |
|
| 347 |
panel_line |
| 348 |
} |
| 349 |
|
| 350 |
#[allow(clippy::too_many_arguments)] |
| 351 |
fn get_right_fill_style_for_panel( |
| 352 |
line_is_empty: bool, |
| 353 |
line_index: Option<usize>, |
| 354 |
diff_style_sections: &[LineSections<'_, Style>], |
| 355 |
lines_have_homolog: Option<&[bool]>, |
| 356 |
state: &State, |
| 357 |
panel_side: PanelSide, |
| 358 |
background_color_extends_to_terminal_width: BgShouldFill, |
| 359 |
config: &Config, |
| 360 |
) -> (Option<BgFillMethod>, Style) { |
| 361 |
// If in the left panel then it must be filled with spaces. |
| 362 |
let none_or_override = if panel_side == Left { |
| 363 |
Some(BgFillMethod::Spaces) |
| 364 |
} else { |
| 365 |
None |
| 366 |
}; |
| 367 |
|
| 368 |
match (line_is_empty, line_index) { |
| 369 |
(true, _) => (none_or_override, config.null_style), |
| 370 |
(false, None) => (none_or_override, config.null_style), |
| 371 |
(false, Some(index)) => { |
| 372 |
let (bg_fill_mode, fill_style) = |
| 373 |
Painter::get_should_right_fill_background_color_and_fill_style( |
| 374 |
&diff_style_sections[index], |
| 375 |
lines_have_homolog.map(|h| h[index]), |
| 376 |
state, |
| 377 |
background_color_extends_to_terminal_width, |
| 378 |
config, |
| 379 |
); |
| 380 |
|
| 381 |
match bg_fill_mode { |
| 382 |
None => (none_or_override, config.null_style), |
| 383 |
_ if panel_side == Left => (Some(BgFillMethod::Spaces), fill_style), |
| 384 |
_ => (bg_fill_mode, fill_style), |
| 385 |
} |
| 386 |
} |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
/// Construct half of a minus or plus line under side-by-side mode, i.e. the half line that |
| 391 |
/// goes in one or other panel. Return a tuple `(painted_half_line, is_empty)`. |
| 392 |
// Suppose the line being displayed is a minus line with a paired plus line. Then both times |
| 393 |
// this function is called, `line_index` will be `Some`. This case proceeds as one would |
| 394 |
// expect: on the first call, we are constructing the left panel line, and we are passed |
| 395 |
// `(Some(index), HunkMinus, Left)`. We pass `(HunkMinus, Left)` to |
| 396 |
// `paint_line`. This has two consequences: |
| 397 |
// 1. `format_and_paint_line_numbers` will increment the minus line number. |
| 398 |
// 2. `format_and_paint_line_numbers` will emit the left line number field, and not the right. |
| 399 |
// |
| 400 |
// The second call does the analogous thing for the plus line to be displayed in the right panel: |
| 401 |
// we are passed `(Some(index), HunkPlus, Right)` and we pass `(HunkPlus, Right)` to `paint_line`, |
| 402 |
// causing it to increment the plus line number and emit the right line number field. |
| 403 |
// |
| 404 |
// Now consider the case where the line being displayed is a minus line with no paired plus line. |
| 405 |
// The first call is as before. On the second call, we are passed `(None, HunkPlus, Right)` and we |
| 406 |
// wish to display the right panel, with its line number container, but without any line number |
| 407 |
// (and without any line contents). We do this by passing (HunkMinus, Right) to `paint_line`, since |
| 408 |
// what this will do is set the line number pair in that function to `(Some(minus_number), None)`, |
| 409 |
// and then only emit the right field (which has a None number, i.e. blank). |
| 410 |
#[allow(clippy::too_many_arguments)] |
| 411 |
fn paint_minus_or_plus_panel_line<'a>( |
| 412 |
line_index: Option<usize>, |
| 413 |
syntax_style_sections: &[LineSections<'a, SyntectStyle>], |
| 414 |
diff_style_sections: &[LineSections<'a, Style>], |
| 415 |
state: &State, |
| 416 |
line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>, |
| 417 |
panel_side: PanelSide, |
| 418 |
config: &Config, |
| 419 |
) -> (String, bool) { |
| 420 |
let (empty_line_syntax_sections, empty_line_diff_sections) = (Vec::new(), Vec::new()); |
| 421 |
|
| 422 |
let (line_syntax_sections, line_diff_sections, state_for_line_numbers_field) = |
| 423 |
if let Some(index) = line_index { |
| 424 |
( |
| 425 |
&syntax_style_sections[index], |
| 426 |
&diff_style_sections[index], |
| 427 |
state.clone(), |
| 428 |
) |
| 429 |
} else { |
| 430 |
let opposite_state = match state { |
| 431 |
State::HunkMinus(DiffType::Unified, s) => { |
| 432 |
State::HunkPlus(DiffType::Unified, s.clone()) |
| 433 |
} |
| 434 |
State::HunkPlus(DiffType::Unified, s) => { |
| 435 |
State::HunkMinus(DiffType::Unified, s.clone()) |
| 436 |
} |
| 437 |
_ => unreachable!(), |
| 438 |
}; |
| 439 |
( |
| 440 |
&empty_line_syntax_sections, |
| 441 |
&empty_line_diff_sections, |
| 442 |
opposite_state, |
| 443 |
) |
| 444 |
}; |
| 445 |
|
| 446 |
let painted_prefix = match (config.keep_plus_minus_markers, panel_side, state) { |
| 447 |
(true, _, State::HunkPlusWrapped) => Some(config.plus_style.paint(" ")), |
| 448 |
(true, _, State::HunkMinusWrapped) => Some(config.minus_style.paint(" ")), |
| 449 |
(true, Left, _) => Some(config.minus_style.paint("-")), |
| 450 |
(true, Right, _) => Some(config.plus_style.paint("+")), |
| 451 |
_ => None, |
| 452 |
}; |
| 453 |
|
| 454 |
let (line, line_is_empty) = Painter::paint_line( |
| 455 |
line_syntax_sections, |
| 456 |
line_diff_sections, |
| 457 |
&state_for_line_numbers_field, |
| 458 |
line_numbers_data, |
| 459 |
Some(panel_side), |
| 460 |
painted_prefix, |
| 461 |
config, |
| 462 |
); |
| 463 |
|
| 464 |
(line, line_is_empty) |
| 465 |
} |
| 466 |
|
| 467 |
/// Right-fill the background color of a line in a panel. If in the left panel this is always |
| 468 |
/// done with spaces. The right panel can be filled with spaces or using ANSI sequences |
| 469 |
/// instructing the terminal emulator to fill the background color rightwards. |
| 470 |
#[allow(clippy::too_many_arguments, clippy::comparison_chain)] |
| 471 |
fn pad_panel_line_to_width( |
| 472 |
panel_line: &mut String, |
| 473 |
panel_line_is_empty: bool, |
| 474 |
line_index: Option<usize>, |
| 475 |
diff_style_sections: &[LineSections<'_, Style>], |
| 476 |
lines_have_homolog: Option<&[bool]>, |
| 477 |
state: &State, |
| 478 |
panel_side: PanelSide, |
| 479 |
background_color_extends_to_terminal_width: BgShouldFill, |
| 480 |
config: &Config, |
| 481 |
) { |
| 482 |
// Emit empty line marker if the panel line is empty but not empty-by-construction. IOW if the |
| 483 |
// other panel contains a real line, and we are currently emitting an empty counterpart panel |
| 484 |
// to form the other half of the line, then don't emit the empty line marker. |
| 485 |
if panel_line_is_empty && line_index.is_some() { |
| 486 |
match state { |
| 487 |
State::HunkMinus(_, _) => Painter::mark_empty_line( |
| 488 |
&config.minus_empty_line_marker_style, |
| 489 |
panel_line, |
| 490 |
Some(" "), |
| 491 |
), |
| 492 |
State::HunkPlus(_, _) => Painter::mark_empty_line( |
| 493 |
&config.plus_empty_line_marker_style, |
| 494 |
panel_line, |
| 495 |
Some(" "), |
| 496 |
), |
| 497 |
State::HunkZero(_, _) => {} |
| 498 |
_ => unreachable!(), |
| 499 |
}; |
| 500 |
}; |
| 501 |
|
| 502 |
let text_width = ansi::measure_text_width(panel_line); |
| 503 |
let panel_width = config.side_by_side_data[panel_side].width; |
| 504 |
|
| 505 |
if text_width > panel_width { |
| 506 |
*panel_line = |
| 507 |
ansi::truncate_str(panel_line, panel_width, &config.truncation_symbol).to_string(); |
| 508 |
} |
| 509 |
|
| 510 |
let (bg_fill_mode, fill_style) = get_right_fill_style_for_panel( |
| 511 |
panel_line_is_empty, |
| 512 |
line_index, |
| 513 |
diff_style_sections, |
| 514 |
lines_have_homolog, |
| 515 |
state, |
| 516 |
panel_side, |
| 517 |
background_color_extends_to_terminal_width, |
| 518 |
config, |
| 519 |
); |
| 520 |
|
| 521 |
match bg_fill_mode { |
| 522 |
Some(BgFillMethod::TryAnsiSequence) => { |
| 523 |
Painter::right_fill_background_color(panel_line, fill_style) |
| 524 |
} |
| 525 |
Some(BgFillMethod::Spaces) if text_width >= panel_width => (), |
| 526 |
Some(BgFillMethod::Spaces) => panel_line.push_str( |
| 527 |
#[allow(clippy::unnecessary_to_owned)] |
| 528 |
&fill_style |
| 529 |
.paint(" ".repeat(panel_width - text_width)) |
| 530 |
.to_string(), |
| 531 |
), |
| 532 |
None => (), |
| 533 |
} |
| 534 |
} |
| 535 |
|
| 536 |
pub mod ansifill { |
| 537 |
use super::SideBySideData; |
| 538 |
use crate::config::Config; |
| 539 |
use crate::paint::BgFillMethod; |
| 540 |
|
| 541 |
pub const ODD_PAD_CHAR: char = ' '; |
| 542 |
|
| 543 |
// Panels in side-by-side mode always sum up to an even number, so when the terminal |
| 544 |
// has an odd width an extra column is left over. |
| 545 |
// If the background color is extended with an ANSI sequence (which only knows "fill |
| 546 |
// this row until the end") instead of spaces (see `BgFillMethod`), then the coloring |
| 547 |
// extends into that column. This becomes noticeable when the displayed content reaches |
| 548 |
// the right side of the right panel to be truncated or wrapped. |
| 549 |
// However using an ANSI sequence instead of spaces is generally preferable because |
| 550 |
// small changes to the terminal width are less noticeable. |
| 551 |
|
| 552 |
/// The solution in this case is to add `ODD_PAD_CHAR` before the first line number in |
| 553 |
/// the right panel and increasing its width by one, thus using the full terminal width |
| 554 |
/// with the two panels. |
| 555 |
/// This also means line numbers can not be disabled in side-by-side mode, but they may |
| 556 |
/// not actually paint numbers. |
| 557 |
#[derive(Clone, Debug)] |
| 558 |
pub struct UseFullPanelWidth(pub bool); |
| 559 |
impl UseFullPanelWidth { |
| 560 |
pub fn new(config: &Config) -> Self { |
| 561 |
Self( |
| 562 |
config.side_by_side |
| 563 |
&& Self::is_odd_with_ansi(&config.decorations_width, &config.line_fill_method), |
| 564 |
) |
| 565 |
} |
| 566 |
pub fn sbs_odd_fix( |
| 567 |
width: &crate::cli::Width, |
| 568 |
method: &BgFillMethod, |
| 569 |
sbs_data: SideBySideData, |
| 570 |
) -> SideBySideData { |
| 571 |
if Self::is_odd_with_ansi(width, method) { |
| 572 |
Self::adapt_sbs_data(sbs_data) |
| 573 |
} else { |
| 574 |
sbs_data |
| 575 |
} |
| 576 |
} |
| 577 |
pub fn pad_width(&self) -> bool { |
| 578 |
self.0 |
| 579 |
} |
| 580 |
fn is_odd_with_ansi(width: &crate::cli::Width, method: &BgFillMethod) -> bool { |
| 581 |
method == &BgFillMethod::TryAnsiSequence |
| 582 |
&& matches!(&width, crate::cli::Width::Fixed(width) if width % 2 == 1) |
| 583 |
} |
| 584 |
fn adapt_sbs_data(mut sbs_data: SideBySideData) -> SideBySideData { |
| 585 |
sbs_data[super::Right].width += 1; |
| 586 |
sbs_data |
| 587 |
} |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
#[cfg(test)] |
| 592 |
pub mod tests { |
| 593 |
use crate::ansi::strip_ansi_codes; |
| 594 |
use crate::features::line_numbers::tests::*; |
| 595 |
use crate::tests::integration_test_utils::{make_config_from_args, run_delta, DeltaTest}; |
| 596 |
use insta::assert_snapshot; |
| 597 |
|
| 598 |
#[test] |
| 599 |
fn test_two_fitting_minus_lines() { |
| 600 |
// rustfmt ignores the assert macro arguments, so do the setup outside |
| 601 |
let result = DeltaTest::with_args(&["--side-by-side"]) |
| 602 |
.with_input(TWO_MINUS_LINES_DIFF) |
| 603 |
.skip_header(); |
| 604 |
assert_snapshot!(result, @r###" |
| 605 |
│ 1 │a = 1 │ │ |
| 606 |
│ 2 │b = 23456 │ │ |
| 607 |
"### |
| 608 |
); |
| 609 |
} |
| 610 |
|
| 611 |
#[test] |
| 612 |
fn test_two_minus_lines_truncated() { |
| 613 |
DeltaTest::with_args(&[ |
| 614 |
"--side-by-side", |
| 615 |
"--wrap-max-lines", |
| 616 |
"0", |
| 617 |
"--width", |
| 618 |
"28", |
| 619 |
"--line-fill-method=spaces", |
| 620 |
]) |
| 621 |
.set_config(|cfg| cfg.truncation_symbol = ">".into()) |
| 622 |
.with_input(TWO_MINUS_LINES_DIFF) |
| 623 |
.expect_after_header( |
| 624 |
r#" |
| 625 |
│ 1 │a = 1 │ │ |
| 626 |
│ 2 │b = 234>│ │"#, |
| 627 |
); |
| 628 |
} |
| 629 |
|
| 630 |
#[test] |
| 631 |
fn test_two_plus_lines() { |
| 632 |
DeltaTest::with_args(&[ |
| 633 |
"--side-by-side", |
| 634 |
"--width", |
| 635 |
"41", |
| 636 |
"--line-fill-method=spaces", |
| 637 |
]) |
| 638 |
.with_input(TWO_PLUS_LINES_DIFF) |
| 639 |
.expect_after_header( |
| 640 |
r#" |
| 641 |
│ │ │ 1 │a = 1 |
| 642 |
│ │ │ 2 │b = 234567 "#, |
| 643 |
); |
| 644 |
} |
| 645 |
|
| 646 |
#[test] |
| 647 |
fn test_two_plus_lines_spaces_and_ansi() { |
| 648 |
DeltaTest::with_args(&[ |
| 649 |
"--side-by-side", |
| 650 |
"--width", |
| 651 |
"41", |
| 652 |
"--line-fill-method=spaces", |
| 653 |
]) |
| 654 |
.explain_ansi() |
| 655 |
.with_input(TWO_PLUS_LINES_DIFF) |
| 656 |
.expect_after_header(r#" |
| 657 |
(blue)│(88) (blue)│(normal) (blue)│(28) 1 (blue)│(231 22)a (203)=(231) (141)1(normal 22) (normal) |
| 658 |
(blue)│(88) (blue)│(normal) (blue)│(28) 2 (blue)│(231 22)b (203)=(231) (141)234567(normal 22) (normal)"#); |
| 659 |
|
| 660 |
DeltaTest::with_args(&[ |
| 661 |
"--side-by-side", |
| 662 |
"--width", |
| 663 |
"41", |
| 664 |
"--line-fill-method=ansi", |
| 665 |
]) |
| 666 |
.explain_ansi() |
| 667 |
.with_input(TWO_PLUS_LINES_DIFF) |
| 668 |
.expect_after_header(r#" |
| 669 |
(blue)│(88) (blue)│(normal) (blue) │(28) 1 (blue)│(231 22)a (203)=(231) (141)1(normal) |
| 670 |
(blue)│(88) (blue)│(normal) (blue) │(28) 2 (blue)│(231 22)b (203)=(231) (141)234567(normal)"#); |
| 671 |
} |
| 672 |
|
| 673 |
#[test] |
| 674 |
fn test_two_plus_lines_truncated() { |
| 675 |
let mut config = make_config_from_args(&[ |
| 676 |
"--side-by-side", |
| 677 |
"--wrap-max-lines", |
| 678 |
"0", |
| 679 |
"--width", |
| 680 |
"30", |
| 681 |
"--line-fill-method=spaces", |
| 682 |
]); |
| 683 |
config.truncation_symbol = ">".into(); |
| 684 |
|
| 685 |
let output = run_delta(TWO_PLUS_LINES_DIFF, &config); |
| 686 |
let mut lines = output.lines().skip(crate::config::HEADER_LEN); |
| 687 |
let (line_1, line_2) = (lines.next().unwrap(), lines.next().unwrap()); |
| 688 |
assert_eq!("│ │ │ 1 │a = 1 ", strip_ansi_codes(line_1)); |
| 689 |
assert_eq!("│ │ │ 2 │b = 2345>", strip_ansi_codes(line_2)); |
| 690 |
} |
| 691 |
|
| 692 |
#[test] |
| 693 |
fn test_two_plus_lines_exact_fit() { |
| 694 |
let config = |
| 695 |
make_config_from_args(&["--side-by-side", "--width", "33", "--line-fill-method=ansi"]); |
| 696 |
let output = run_delta(TWO_PLUS_LINES_DIFF, &config); |
| 697 |
let mut lines = output.lines().skip(crate::config::HEADER_LEN); |
| 698 |
let (line_1, line_2) = (lines.next().unwrap(), lines.next().unwrap()); |
| 699 |
let sac = strip_ansi_codes; // alias to help with `cargo fmt`-ing: |
| 700 |
assert_eq!("│ │ │ 1 │a = 1", sac(line_1)); |
| 701 |
assert_eq!("│ │ │ 2 │b = 234567", sac(line_2)); |
| 702 |
} |
| 703 |
|
| 704 |
#[test] |
| 705 |
fn test_one_minus_one_plus_line() { |
| 706 |
DeltaTest::with_args(&[ |
| 707 |
"--side-by-side", |
| 708 |
"--width", |
| 709 |
"40", |
| 710 |
"--line-fill-method=spaces", |
| 711 |
]) |
| 712 |
.with_input(ONE_MINUS_ONE_PLUS_LINE_DIFF) |
| 713 |
.expect_after_header( |
| 714 |
r#" |
| 715 |
│ 1 │a = 1 │ 1 │a = 1 |
| 716 |
│ 2 │b = 2 │ 2 │bb = 2 "#, |
| 717 |
); |
| 718 |
} |
| 719 |
|
| 720 |
#[test] |
| 721 |
fn test_two_minus_lines_unicode_truncated() { |
| 722 |
DeltaTest::with_args(&[ |
| 723 |
"--side-by-side", |
| 724 |
"--wrap-max-lines", |
| 725 |
"2", |
| 726 |
"--width", |
| 727 |
"16", |
| 728 |
"--line-fill-method=spaces", |
| 729 |
]) |
| 730 |
.set_config(|cfg| cfg.truncation_symbol = ">".into()) |
| 731 |
.with_input(TWO_MINUS_LINES_UNICODE_DIFF) |
| 732 |
.expect_after_header( |
| 733 |
r#" |
| 734 |
│ 1 │↵ │ │ |
| 735 |
│ │↵ │ │ |
| 736 |
│ │ >│ │"#, |
| 737 |
); |
| 738 |
|
| 739 |
DeltaTest::with_args(&[ |
| 740 |
"--side-by-side", |
| 741 |
"--wrap-max-lines", |
| 742 |
"2", |
| 743 |
"--width", |
| 744 |
"17", |
| 745 |
"--line-fill-method=spaces", |
| 746 |
]) |
| 747 |
.set_config(|cfg| cfg.truncation_symbol = ">".into()) |
| 748 |
.with_input(TWO_MINUS_LINES_UNICODE_DIFF) |
| 749 |
.expect_after_header( |
| 750 |
r#" |
| 751 |
│ 1 │↵ │ │ |
| 752 |
│ │↵ │ │ |
| 753 |
│ │ >│ │"#, |
| 754 |
); |
| 755 |
|
| 756 |
DeltaTest::with_args(&[ |
| 757 |
"--side-by-side", |
| 758 |
"--wrap-max-lines", |
| 759 |
"2", |
| 760 |
"--width", |
| 761 |
"18", |
| 762 |
"--line-fill-method=spaces", |
| 763 |
]) |
| 764 |
.set_config(|cfg| cfg.truncation_symbol = ">".into()) |
| 765 |
.with_input(TWO_MINUS_LINES_UNICODE_DIFF) |
| 766 |
.expect_after_header( |
| 767 |
r#" |
| 768 |
│ 1 │一↵│ │ |
| 769 |
│ │二↵│ │ |
| 770 |
│ │三 │ │"#, |
| 771 |
); |
| 772 |
} |
| 773 |
} |