Skip to content

Commit

Permalink
Fix spacing issue with git feature disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
einfachIrgendwer0815 committed Apr 7, 2024
1 parent cd4b880 commit 55bebbb
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 66 deletions.
30 changes: 30 additions & 0 deletions src/decorations.rs
Expand Up @@ -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
}
}
23 changes: 23 additions & 0 deletions src/printer.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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());

Expand Down
29 changes: 15 additions & 14 deletions tests/integration_tests.rs
Expand Up @@ -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
───────┴────────────────────────────────────────────────────────────────────────
",
);
}
Expand Down Expand Up @@ -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("");
Expand Down
54 changes: 27 additions & 27 deletions 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 │ }
───────┴────────────────────────────────────────────────────────────────────────
50 changes: 25 additions & 25 deletions 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 │ }
───────┴────────────────────────────────────────────────────────────────────────

0 comments on commit 55bebbb

Please sign in to comment.