visualize empty line in diff better (closes #1359)

This commit is contained in:
extrawurst 2023-08-11 16:44:50 +02:00
parent 9eb8d470a4
commit 6ec647710d
4 changed files with 55 additions and 42 deletions

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
**visualize empty lines in diff better**
![diff-empty-line](assets/diff-empty-line.png)
### Breaking Changes
* Do you use a custom theme?
@ -22,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* switched from textwrap to bwrap for text wrapping [[@TheBlackSheep3](https://github.com/TheBlackSheep3/)] ([#1762](https://github.com/extrawurst/gitui/issues/1762))
* more logging diagnostics when a repo cannot be opened
* added to [anaconda](https://anaconda.org/conda-forge/gitui) [[@TheBlackSheep3](https://github.com/TheBlackSheep3/)] ([#1626](https://github.com/extrawurst/gitui/issues/1626))
* visualize empty line substituted with content in diff better ([#1359](https://github.com/extrawurst/gitui/issues/1359))
### Fixes
* fix commit dialog char count for multibyte characters ([#1726](https://github.com/extrawurst/gitui/issues/1726))

BIN
assets/diff-empty-line.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 220 KiB

View File

@ -335,47 +335,12 @@ impl DiffComponent {
}
fn get_text(&self, width: u16, height: u16) -> Vec<Line> {
let mut res: Vec<Line> = Vec::new();
if let Some(diff) = &self.diff {
if diff.hunks.is_empty() {
let is_positive = diff.size_delta >= 0;
let delta_byte_size =
ByteSize::b(diff.size_delta.unsigned_abs());
let sign = if is_positive { "+" } else { "-" };
res.extend(vec![Line::from(vec![
Span::raw(Cow::from("size: ")),
Span::styled(
Cow::from(format!(
"{}",
ByteSize::b(diff.sizes.0)
)),
self.theme.text(false, false),
),
Span::raw(Cow::from(" -> ")),
Span::styled(
Cow::from(format!(
"{}",
ByteSize::b(diff.sizes.1)
)),
self.theme.text(false, false),
),
Span::raw(Cow::from(" (")),
Span::styled(
Cow::from(format!(
"{sign}{delta_byte_size:}"
)),
self.theme.diff_line(
if is_positive {
DiffLineType::Add
} else {
DiffLineType::Delete
},
false,
),
),
Span::raw(Cow::from(")")),
])]);
return if diff.hunks.is_empty() {
self.get_text_binary(diff)
} else {
let mut res: Vec<Line> = Vec::new();
let min = self.vertical_scroll.get_top();
let max = min + height as usize;
@ -426,9 +391,44 @@ impl DiffComponent {
line_cursor += hunk_len;
}
}
}
res
};
}
res
vec![]
}
fn get_text_binary(&self, diff: &FileDiff) -> Vec<Line> {
let is_positive = diff.size_delta >= 0;
let delta_byte_size =
ByteSize::b(diff.size_delta.unsigned_abs());
let sign = if is_positive { "+" } else { "-" };
vec![Line::from(vec![
Span::raw(Cow::from("size: ")),
Span::styled(
Cow::from(format!("{}", ByteSize::b(diff.sizes.0))),
self.theme.text(false, false),
),
Span::raw(Cow::from(" -> ")),
Span::styled(
Cow::from(format!("{}", ByteSize::b(diff.sizes.1))),
self.theme.text(false, false),
),
Span::raw(Cow::from(" (")),
Span::styled(
Cow::from(format!("{sign}{delta_byte_size:}")),
self.theme.diff_line(
if is_positive {
DiffLineType::Add
} else {
DiffLineType::Delete
},
false,
),
),
Span::raw(Cow::from(")")),
])]
}
fn get_line_to_add<'a>(
@ -442,6 +442,9 @@ impl DiffComponent {
) -> Line<'a> {
let style = theme.diff_hunk_marker(selected_hunk);
let is_content_line =
matches!(line.line_type, DiffLineType::None);
let left_side_of_line = if end_of_hunk {
Span::styled(Cow::from(symbols::line::BOTTOM_LEFT), style)
} else {
@ -458,7 +461,11 @@ impl DiffComponent {
};
let content =
tabs_to_spaces(line.content.as_ref().to_string());
if !is_content_line && line.content.as_ref().is_empty() {
String::from(strings::symbol::LINE_BREAK)
} else {
tabs_to_spaces(line.content.as_ref().to_string())
};
let content = trim_offset(&content, scrolled_right);
let filled = if selected {

View File

@ -38,6 +38,7 @@ pub mod symbol {
pub const CHECKMARK: &str = "\u{2713}"; //✓
pub const SPACE: &str = "\u{02FD}"; //˽
pub const EMPTY_SPACE: &str = " ";
pub const LINE_BREAK: &str = "";
pub const FOLDER_ICON_COLLAPSED: &str = "\u{25b8}"; //▸
pub const FOLDER_ICON_EXPANDED: &str = "\u{25be}"; //▾
pub const EMPTY_STR: &str = "";