From 55bebbb9b3a86f37f877d6365569fad1aa9c7150 Mon Sep 17 00:00:00 2001 From: einfachIrgendwer0815 <85333734+einfachIrgendwer0815@users.noreply.github.com> Date: Mon, 18 Mar 2024 18:26:53 +0100 Subject: [PATCH] Fix spacing issue with git feature disabled --- src/decorations.rs | 30 +++++++++++ src/printer.rs | 23 ++++++++ tests/integration_tests.rs | 29 +++++----- .../output/grid_header_numbers.snapshot.txt | 54 +++++++++---------- .../output/grid_numbers.snapshot.txt | 50 ++++++++--------- 5 files changed, 120 insertions(+), 66 deletions(-) diff --git a/src/decorations.rs b/src/decorations.rs index 5b7846c30d..7f0dca0acd 100644 --- a/src/decorations.rs +++ b/src/decorations.rs @@ -156,3 +156,33 @@ impl Decoration for GridBorderDecoration { self.cached.width } } + +pub(crate) struct PlaceholderDecoration { + cached: DecorationText, +} + +impl PlaceholderDecoration { + pub(crate) fn new(length: usize) -> Self { + Self { + cached: DecorationText { + text: " ".repeat(length), + width: length, + }, + } + } +} + +impl Decoration for PlaceholderDecoration { + fn generate( + &self, + _line_number: usize, + _continuation: bool, + _printer: &InteractivePrinter, + ) -> DecorationText { + self.cached.clone() + } + + fn width(&self) -> usize { + self.cached.width + } +} diff --git a/src/printer.rs b/src/printer.rs index 9559c1b8fe..af00e7b18f 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -24,6 +24,7 @@ use crate::config::Config; use crate::decorations; #[cfg(feature = "git")] use crate::decorations::LineChangesDecoration; +use crate::decorations::PlaceholderDecoration; use crate::decorations::{Decoration, GridBorderDecoration, LineNumberDecoration}; #[cfg(feature = "git")] use crate::diff::LineChanges; @@ -257,6 +258,28 @@ impl<'a> InteractivePrinter<'a> { } } + let insert_placeholder = { + let git_feature_enabled = cfg!(feature = "git"); + let changes_component; + #[cfg(feature = "git")] + { + changes_component = config.style_components.changes(); + } + #[cfg(not(feature = "git"))] + { + changes_component = false; + } + + let soft_limit_active = config.soft_line_limit.is_some(); + let numbers_and_grid = + config.style_components.grid() && config.style_components.numbers(); + + (!git_feature_enabled || !changes_component) && numbers_and_grid && soft_limit_active + }; + if insert_placeholder { + decorations.push(Box::new(PlaceholderDecoration::new(1))) + } + let mut panel_width: usize = decorations.len() + decorations.iter().fold(0, |a, x| a + x.width()); diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 67d2f762e6..91e3a13ad6 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -1308,11 +1308,11 @@ fn bom_stripped_when_no_color_and_not_loop_through() { .success() .stdout( "\ -─────┬────────────────────────────────────────────────────────────────────────── - │ File: test_BOM.txt -─────┼────────────────────────────────────────────────────────────────────────── - 1 │ hello world -─────┴────────────────────────────────────────────────────────────────────────── +───────┬──────────────────────────────────────────────────────────────────────── + │ File: test_BOM.txt +───────┼──────────────────────────────────────────────────────────────────────── + 1 │ hello world +───────┴──────────────────────────────────────────────────────────────────────── ", ); } @@ -1560,15 +1560,16 @@ fn header_narrow_terminal() { .success() .stdout( "\ -─────┬──────────────────────── - │ File: this-file-path-is - │ -really-long-and-would- - │ have-broken-the-layout- - │ of-the-header.txt -─────┼──────────────────────── - 1 │ The header is not broke - │ n -─────┴──────────────────────── +───────┬────────────────────── + │ File: this-file-path- + │ is-really-long-and-wo + │ uld-have-broken-the-l + │ ayout-of-the-header.t + │ xt +───────┼────────────────────── + 1 │ The header is not bro + │ ken +───────┴────────────────────── ", ) .stderr(""); diff --git a/tests/snapshots/output/grid_header_numbers.snapshot.txt b/tests/snapshots/output/grid_header_numbers.snapshot.txt index dfbb934bb7..c298980cea 100644 --- a/tests/snapshots/output/grid_header_numbers.snapshot.txt +++ b/tests/snapshots/output/grid_header_numbers.snapshot.txt @@ -1,27 +1,27 @@ -─────┬────────────────────────────────────────────────────────────────────────── - │ File: sample.rs -─────┼────────────────────────────────────────────────────────────────────────── - 1 │ /// A rectangle. First line is changed to prevent a regression of #1869 - 2 │ struct Rectangle { - 3 │ width: u32, - 4 │ height: u32, - 5 │ } - 6 │ - 7 │ fn main() { - 8 │ let rect1 = Rectangle { width: 30, height: 50 }; - 9 │ - 10 │ println!( - 11 │ "The perimeter of the rectangle is {} pixels.", - 12 │ perimeter(&rect1) - 13 │ ); - 14 │ println!(r#"This line contains invalid utf8: "�����"#; - 15 │ } - 16 │ - 17 │ fn area(rectangle: &Rectangle) -> u32 { - 18 │ rectangle.width * rectangle.height - 19 │ } - 20 │ - 21 │ fn perimeter(rectangle: &Rectangle) -> u32 { - 22 │ (rectangle.width + rectangle.height) * 2 - 23 │ } -─────┴────────────────────────────────────────────────────────────────────────── +───────┬──────────────────────────────────────────────────────────────────────── + │ File: sample.rs +───────┼──────────────────────────────────────────────────────────────────────── + 1 │ /// A rectangle. First line is changed to prevent a regression of #1869 + 2 │ struct Rectangle { + 3 │ width: u32, + 4 │ height: u32, + 5 │ } + 6 │ + 7 │ fn main() { + 8 │ let rect1 = Rectangle { width: 30, height: 50 }; + 9 │ + 10 │ println!( + 11 │ "The perimeter of the rectangle is {} pixels.", + 12 │ perimeter(&rect1) + 13 │ ); + 14 │ println!(r#"This line contains invalid utf8: "�����"#; + 15 │ } + 16 │ + 17 │ fn area(rectangle: &Rectangle) -> u32 { + 18 │ rectangle.width * rectangle.height + 19 │ } + 20 │ + 21 │ fn perimeter(rectangle: &Rectangle) -> u32 { + 22 │ (rectangle.width + rectangle.height) * 2 + 23 │ } +───────┴──────────────────────────────────────────────────────────────────────── diff --git a/tests/snapshots/output/grid_numbers.snapshot.txt b/tests/snapshots/output/grid_numbers.snapshot.txt index 83ee57cdca..b8b86bdbb0 100644 --- a/tests/snapshots/output/grid_numbers.snapshot.txt +++ b/tests/snapshots/output/grid_numbers.snapshot.txt @@ -1,25 +1,25 @@ -─────┬────────────────────────────────────────────────────────────────────────── - 1 │ /// A rectangle. First line is changed to prevent a regression of #1869 - 2 │ struct Rectangle { - 3 │ width: u32, - 4 │ height: u32, - 5 │ } - 6 │ - 7 │ fn main() { - 8 │ let rect1 = Rectangle { width: 30, height: 50 }; - 9 │ - 10 │ println!( - 11 │ "The perimeter of the rectangle is {} pixels.", - 12 │ perimeter(&rect1) - 13 │ ); - 14 │ println!(r#"This line contains invalid utf8: "�����"#; - 15 │ } - 16 │ - 17 │ fn area(rectangle: &Rectangle) -> u32 { - 18 │ rectangle.width * rectangle.height - 19 │ } - 20 │ - 21 │ fn perimeter(rectangle: &Rectangle) -> u32 { - 22 │ (rectangle.width + rectangle.height) * 2 - 23 │ } -─────┴────────────────────────────────────────────────────────────────────────── +───────┬──────────────────────────────────────────────────────────────────────── + 1 │ /// A rectangle. First line is changed to prevent a regression of #1869 + 2 │ struct Rectangle { + 3 │ width: u32, + 4 │ height: u32, + 5 │ } + 6 │ + 7 │ fn main() { + 8 │ let rect1 = Rectangle { width: 30, height: 50 }; + 9 │ + 10 │ println!( + 11 │ "The perimeter of the rectangle is {} pixels.", + 12 │ perimeter(&rect1) + 13 │ ); + 14 │ println!(r#"This line contains invalid utf8: "�����"#; + 15 │ } + 16 │ + 17 │ fn area(rectangle: &Rectangle) -> u32 { + 18 │ rectangle.width * rectangle.height + 19 │ } + 20 │ + 21 │ fn perimeter(rectangle: &Rectangle) -> u32 { + 22 │ (rectangle.width + rectangle.height) * 2 + 23 │ } +───────┴────────────────────────────────────────────────────────────────────────