Skip to content

Commit 2fd957e

Browse files
authored
Allow customizing line break visualization (#1904)
1 parent 09907f3 commit 2fd957e

File tree

6 files changed

+103
-5
lines changed

6 files changed

+103
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## Unreleased
99

10+
### Added
11+
* `theme.ron` now supports customizing line break symbol ([#1894](https://github.com/extrawurst/gitui/issues/1894))
12+
1013
## [0.24.3] - 2023-09-09
1114

1215
### Fixes

THEMES.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,21 @@ Notes:
3131
* using a color like `yellow` might appear in whatever your terminal/theme defines for `yellow`
3232
* valid colors can be found in tui-rs' [Color](https://docs.rs/tui/0.12.0/tui/style/enum.Color.html) struct.
3333
* all customizable theme elements can be found in [`style.rs` in the `impl Default for Theme` block](https://github.com/extrawurst/gitui/blob/master/src/ui/style.rs#L305)
34+
35+
## Customizing line breaks
36+
37+
If you want to change how the line break is displayed in the diff, you can also specify `line_break` in your `theme.ron`:
38+
39+
```
40+
(
41+
line_break: Some("¶"),
42+
)
43+
```
44+
45+
Note that if you want to turn it off, you should use a blank string:
46+
47+
```
48+
(
49+
line_break: Some(""),
50+
)
51+
```

src/components/diff.rs

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ impl DiffComponent {
462462

463463
let content =
464464
if !is_content_line && line.content.as_ref().is_empty() {
465-
String::from(strings::symbol::LINE_BREAK)
465+
theme.line_break()
466466
} else {
467467
tabs_to_spaces(line.content.as_ref().to_string())
468468
};
@@ -958,3 +958,75 @@ impl Component for DiffComponent {
958958
self.focused = focus;
959959
}
960960
}
961+
962+
#[cfg(test)]
963+
mod tests {
964+
use super::*;
965+
use crate::ui::style::Theme;
966+
use std::io::Write;
967+
use std::rc::Rc;
968+
use tempfile::NamedTempFile;
969+
970+
#[test]
971+
fn test_line_break() {
972+
let diff_line = DiffLine {
973+
content: "".into(),
974+
line_type: DiffLineType::Add,
975+
position: Default::default(),
976+
};
977+
978+
{
979+
let default_theme = Rc::new(Theme::default());
980+
981+
assert_eq!(
982+
DiffComponent::get_line_to_add(
983+
4,
984+
&diff_line,
985+
false,
986+
false,
987+
false,
988+
&default_theme,
989+
0
990+
)
991+
.spans
992+
.last()
993+
.unwrap(),
994+
&Span::styled(
995+
Cow::from(\n"),
996+
default_theme
997+
.diff_line(diff_line.line_type, false)
998+
)
999+
);
1000+
}
1001+
1002+
{
1003+
let mut file = NamedTempFile::new().unwrap();
1004+
1005+
writeln!(
1006+
file,
1007+
r#"
1008+
(
1009+
line_break: Some("+")
1010+
)
1011+
"#
1012+
)
1013+
.unwrap();
1014+
1015+
let theme =
1016+
Rc::new(Theme::init(&file.path().to_path_buf()));
1017+
1018+
assert_eq!(
1019+
DiffComponent::get_line_to_add(
1020+
4, &diff_line, false, false, false, &theme, 0
1021+
)
1022+
.spans
1023+
.last()
1024+
.unwrap(),
1025+
&Span::styled(
1026+
Cow::from("+\n"),
1027+
theme.diff_line(diff_line.line_type, false)
1028+
)
1029+
);
1030+
}
1031+
}
1032+
}

src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ fn main() -> Result<()> {
157157
let quit_state = run_app(
158158
app_start,
159159
repo_path.clone(),
160-
theme,
160+
theme.clone(),
161161
key_config.clone(),
162162
&input,
163163
updater,

src/strings.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ pub mod symbol {
4242
pub const CHECKMARK: &str = "\u{2713}"; //✓
4343
pub const SPACE: &str = "\u{02FD}"; //˽
4444
pub const EMPTY_SPACE: &str = " ";
45-
pub const LINE_BREAK: &str = "¶";
4645
pub const FOLDER_ICON_COLLAPSED: &str = "\u{25b8}"; //▸
4746
pub const FOLDER_ICON_EXPANDED: &str = "\u{25be}"; //▾
4847
pub const EMPTY_STR: &str = "";

src/ui/style.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use struct_patch::Patch;
88

99
pub type SharedTheme = Rc<Theme>;
1010

11-
#[derive(Serialize, Deserialize, Debug, Copy, Clone, Patch)]
11+
#[derive(Serialize, Deserialize, Debug, Clone, Patch)]
1212
#[patch_derive(Serialize, Deserialize)]
1313
pub struct Theme {
1414
selected_tab: Color,
@@ -32,6 +32,7 @@ pub struct Theme {
3232
push_gauge_fg: Color,
3333
tag_fg: Color,
3434
branch_fg: Color,
35+
line_break: String,
3536
}
3637

3738
impl Theme {
@@ -192,6 +193,10 @@ impl Theme {
192193
Style::default().fg(self.danger_fg)
193194
}
194195

196+
pub fn line_break(&self) -> String {
197+
self.line_break.clone()
198+
}
199+
195200
pub fn commandbar(&self, enabled: bool, line: usize) -> Style {
196201
if enabled {
197202
Style::default().fg(self.command_fg)
@@ -278,7 +283,7 @@ impl Theme {
278283
// This is supposed to be called when theme.ron doesn't already exists.
279284
fn save_patch(&self, theme_path: &PathBuf) -> Result<()> {
280285
let mut file = File::create(theme_path)?;
281-
let patch = self.into_patch_by_diff(Self::default());
286+
let patch = self.clone().into_patch_by_diff(Self::default());
282287
let data = to_string_pretty(&patch, PrettyConfig::default())?;
283288

284289
file.write_all(data.as_bytes())?;
@@ -336,6 +341,7 @@ impl Default for Theme {
336341
push_gauge_fg: Color::Reset,
337342
tag_fg: Color::LightMagenta,
338343
branch_fg: Color::LightYellow,
344+
line_break: "¶".to_string(),
339345
}
340346
}
341347
}

0 commit comments

Comments
 (0)