Skip to content

Commit 6ec6477

Browse files
committed
visualize empty line in diff better (closes gitui-org#1359)
1 parent 9eb8d47 commit 6ec6477

File tree

4 files changed

+55
-42
lines changed

4 files changed

+55
-42
lines changed

Diff for: CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
**visualize empty lines in diff better**
11+
12+
![diff-empty-line](assets/diff-empty-line.png)
13+
1014
### Breaking Changes
1115
* Do you use a custom theme?
1216

@@ -22,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2226
* switched from textwrap to bwrap for text wrapping [[@TheBlackSheep3](https://github.com/TheBlackSheep3/)] ([#1762](https://github.com/extrawurst/gitui/issues/1762))
2327
* more logging diagnostics when a repo cannot be opened
2428
* added to [anaconda](https://anaconda.org/conda-forge/gitui) [[@TheBlackSheep3](https://github.com/TheBlackSheep3/)] ([#1626](https://github.com/extrawurst/gitui/issues/1626))
29+
* visualize empty line substituted with content in diff better ([#1359](https://github.com/extrawurst/gitui/issues/1359))
2530

2631
### Fixes
2732
* fix commit dialog char count for multibyte characters ([#1726](https://github.com/extrawurst/gitui/issues/1726))

Diff for: assets/diff-empty-line.png

220 KB
Loading

Diff for: src/components/diff.rs

+49-42
Original file line numberDiff line numberDiff line change
@@ -335,47 +335,12 @@ impl DiffComponent {
335335
}
336336

337337
fn get_text(&self, width: u16, height: u16) -> Vec<Line> {
338-
let mut res: Vec<Line> = Vec::new();
339338
if let Some(diff) = &self.diff {
340-
if diff.hunks.is_empty() {
341-
let is_positive = diff.size_delta >= 0;
342-
let delta_byte_size =
343-
ByteSize::b(diff.size_delta.unsigned_abs());
344-
let sign = if is_positive { "+" } else { "-" };
345-
res.extend(vec![Line::from(vec![
346-
Span::raw(Cow::from("size: ")),
347-
Span::styled(
348-
Cow::from(format!(
349-
"{}",
350-
ByteSize::b(diff.sizes.0)
351-
)),
352-
self.theme.text(false, false),
353-
),
354-
Span::raw(Cow::from(" -> ")),
355-
Span::styled(
356-
Cow::from(format!(
357-
"{}",
358-
ByteSize::b(diff.sizes.1)
359-
)),
360-
self.theme.text(false, false),
361-
),
362-
Span::raw(Cow::from(" (")),
363-
Span::styled(
364-
Cow::from(format!(
365-
"{sign}{delta_byte_size:}"
366-
)),
367-
self.theme.diff_line(
368-
if is_positive {
369-
DiffLineType::Add
370-
} else {
371-
DiffLineType::Delete
372-
},
373-
false,
374-
),
375-
),
376-
Span::raw(Cow::from(")")),
377-
])]);
339+
return if diff.hunks.is_empty() {
340+
self.get_text_binary(diff)
378341
} else {
342+
let mut res: Vec<Line> = Vec::new();
343+
379344
let min = self.vertical_scroll.get_top();
380345
let max = min + height as usize;
381346

@@ -426,9 +391,44 @@ impl DiffComponent {
426391
line_cursor += hunk_len;
427392
}
428393
}
429-
}
394+
395+
res
396+
};
430397
}
431-
res
398+
399+
vec![]
400+
}
401+
402+
fn get_text_binary(&self, diff: &FileDiff) -> Vec<Line> {
403+
let is_positive = diff.size_delta >= 0;
404+
let delta_byte_size =
405+
ByteSize::b(diff.size_delta.unsigned_abs());
406+
let sign = if is_positive { "+" } else { "-" };
407+
vec![Line::from(vec![
408+
Span::raw(Cow::from("size: ")),
409+
Span::styled(
410+
Cow::from(format!("{}", ByteSize::b(diff.sizes.0))),
411+
self.theme.text(false, false),
412+
),
413+
Span::raw(Cow::from(" -> ")),
414+
Span::styled(
415+
Cow::from(format!("{}", ByteSize::b(diff.sizes.1))),
416+
self.theme.text(false, false),
417+
),
418+
Span::raw(Cow::from(" (")),
419+
Span::styled(
420+
Cow::from(format!("{sign}{delta_byte_size:}")),
421+
self.theme.diff_line(
422+
if is_positive {
423+
DiffLineType::Add
424+
} else {
425+
DiffLineType::Delete
426+
},
427+
false,
428+
),
429+
),
430+
Span::raw(Cow::from(")")),
431+
])]
432432
}
433433

434434
fn get_line_to_add<'a>(
@@ -442,6 +442,9 @@ impl DiffComponent {
442442
) -> Line<'a> {
443443
let style = theme.diff_hunk_marker(selected_hunk);
444444

445+
let is_content_line =
446+
matches!(line.line_type, DiffLineType::None);
447+
445448
let left_side_of_line = if end_of_hunk {
446449
Span::styled(Cow::from(symbols::line::BOTTOM_LEFT), style)
447450
} else {
@@ -458,7 +461,11 @@ impl DiffComponent {
458461
};
459462

460463
let content =
461-
tabs_to_spaces(line.content.as_ref().to_string());
464+
if !is_content_line && line.content.as_ref().is_empty() {
465+
String::from(strings::symbol::LINE_BREAK)
466+
} else {
467+
tabs_to_spaces(line.content.as_ref().to_string())
468+
};
462469
let content = trim_offset(&content, scrolled_right);
463470

464471
let filled = if selected {

Diff for: src/strings.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub mod symbol {
3838
pub const CHECKMARK: &str = "\u{2713}"; //✓
3939
pub const SPACE: &str = "\u{02FD}"; //˽
4040
pub const EMPTY_SPACE: &str = " ";
41+
pub const LINE_BREAK: &str = "¶";
4142
pub const FOLDER_ICON_COLLAPSED: &str = "\u{25b8}"; //▸
4243
pub const FOLDER_ICON_EXPANDED: &str = "\u{25be}"; //▾
4344
pub const EMPTY_STR: &str = "";

0 commit comments

Comments
 (0)