summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2020-05-28 13:36:12 -0400
committerDan Davison <dandavison7@gmail.com>2020-05-28 15:39:11 -0400
commitad9c0899c32217f42ce0b68e5191e16c69355c77 (patch)
treed89865cf0af1f1f46c058234c6df086ff930c6df
parent470fb46a0a23052ea40ddc0906175d9ca54a18cb (diff)
Bug fix: honor bold by using "heavy" box drawing characters
This commit honors the bold style attribute by selecting characters from the "heavy" set and then painting those without the bold attribute set. Painting with the bold style attribute set seems to result in incorrect rendering of the box-drawing characters: e.g. the vertical line sections don't "meet up" (at least, in iTerm2 and Terminal on MacOS).
-rw-r--r--src/delta.rs3
-rw-r--r--src/draw.rs87
-rw-r--r--src/tests/test_example_diffs.rs4
3 files changed, 44 insertions, 50 deletions
diff --git a/src/delta.rs b/src/delta.rs
index 6a87f796..034ce706 100644
--- a/src/delta.rs
+++ b/src/delta.rs
@@ -219,7 +219,6 @@ fn handle_commit_meta_header_line(
config.terminal_width,
config.commit_style.ansi_term_style,
decoration_ansi_term_style,
- true,
)?;
Ok(())
}
@@ -277,7 +276,6 @@ fn handle_generic_file_meta_header_line(
config.terminal_width,
config.file_style.ansi_term_style,
decoration_ansi_term_style,
- false,
)?;
Ok(())
}
@@ -338,7 +336,6 @@ fn handle_hunk_header_line(
config.terminal_width,
config.hunk_header_style.ansi_term_style,
decoration_ansi_term_style,
- false,
)?;
painter.output_buffer.clear();
}
diff --git a/src/draw.rs b/src/draw.rs
index 2f205a58..cecc263a 100644
--- a/src/draw.rs
+++ b/src/draw.rs
@@ -6,6 +6,19 @@ use box_drawing;
use console::strip_ansi_codes;
use unicode_width::UnicodeWidthStr;
+trait NotBoldExt {
+ fn not_bold(&self) -> Self;
+}
+
+impl NotBoldExt for ansi_term::Style {
+ fn not_bold(&self) -> Self {
+ Self {
+ is_bold: false,
+ ..*self
+ }
+ }
+}
+
/// Write text to stream, surrounded by a box, leaving the cursor just
/// beyond the bottom right corner.
pub fn write_boxed(
@@ -14,16 +27,15 @@ pub fn write_boxed(
_line_width: usize, // ignored
text_style: ansi_term::Style,
decoration_style: ansi_term::Style,
- heavy: bool,
) -> std::io::Result<()> {
- let up_left = if heavy {
+ let up_left = if decoration_style.is_bold {
box_drawing::heavy::UP_LEFT
} else {
box_drawing::light::UP_LEFT
};
let box_width = UnicodeWidthStr::width(strip_ansi_codes(text).as_ref());
- write_boxed_partial(writer, text, box_width, text_style, decoration_style, heavy)?;
- write!(writer, "{}", decoration_style.paint(up_left))?;
+ write_boxed_partial(writer, text, box_width, text_style, decoration_style)?;
+ write!(writer, "{}", decoration_style.not_bold().paint(up_left))?;
Ok(())
}
@@ -35,17 +47,9 @@ pub fn write_boxed_with_line(
line_width: usize,
text_style: ansi_term::Style,
decoration_style: ansi_term::Style,
- heavy: bool,
) -> std::io::Result<()> {
let box_width = UnicodeWidthStr::width(strip_ansi_codes(text).as_ref());
- write_boxed_with_horizontal_whisker(
- writer,
- text,
- box_width,
- text_style,
- decoration_style,
- heavy,
- )?;
+ write_boxed_with_horizontal_whisker(writer, text, box_width, text_style, decoration_style)?;
write_horizontal_line(
writer,
if line_width > box_width {
@@ -55,7 +59,6 @@ pub fn write_boxed_with_line(
},
text_style,
decoration_style,
- heavy,
)?;
write!(writer, "\n")?;
Ok(())
@@ -73,7 +76,6 @@ pub fn write_underlined(
line_width: usize,
text_style: ansi_term::Style,
decoration_style: ansi_term::Style,
- heavy: bool,
) -> std::io::Result<()> {
_write_under_or_over_lined(
Underoverline::Under,
@@ -82,7 +84,6 @@ pub fn write_underlined(
line_width,
text_style,
decoration_style,
- heavy,
)
}
@@ -92,7 +93,6 @@ pub fn write_overlined(
line_width: usize,
text_style: ansi_term::Style,
decoration_style: ansi_term::Style,
- heavy: bool,
) -> std::io::Result<()> {
_write_under_or_over_lined(
Underoverline::Over,
@@ -101,7 +101,6 @@ pub fn write_overlined(
line_width,
text_style,
decoration_style,
- heavy,
)
}
@@ -111,7 +110,6 @@ pub fn write_underoverlined(
line_width: usize,
text_style: ansi_term::Style,
decoration_style: ansi_term::Style,
- heavy: bool,
) -> std::io::Result<()> {
_write_under_or_over_lined(
Underoverline::Underover,
@@ -120,7 +118,6 @@ pub fn write_underoverlined(
line_width,
text_style,
decoration_style,
- heavy,
)
}
@@ -131,11 +128,10 @@ fn _write_under_or_over_lined(
line_width: usize,
text_style: ansi_term::Style,
decoration_style: ansi_term::Style,
- heavy: bool,
) -> std::io::Result<()> {
let mut write_line: Box<dyn FnMut(&mut dyn Write) -> std::io::Result<()>> =
Box::new(|writer| {
- write_horizontal_line(writer, line_width - 1, text_style, decoration_style, heavy)?;
+ write_horizontal_line(writer, line_width - 1, text_style, decoration_style)?;
write!(writer, "\n")?;
Ok(())
});
@@ -156,9 +152,8 @@ fn write_horizontal_line(
line_width: usize,
_text_style: ansi_term::Style,
decoration_style: ansi_term::Style,
- heavy: bool,
) -> std::io::Result<()> {
- let horizontal = if heavy {
+ let horizontal = if decoration_style.is_bold {
box_drawing::heavy::HORIZONTAL
} else {
box_drawing::light::HORIZONTAL
@@ -166,25 +161,30 @@ fn write_horizontal_line(
write!(
writer,
"{}",
- decoration_style.paint(horizontal.repeat(line_width))
+ decoration_style
+ .not_bold()
+ .paint(horizontal.repeat(line_width))
)
}
-pub fn write_boxed_with_horizontal_whisker(
+fn write_boxed_with_horizontal_whisker(
writer: &mut dyn Write,
text: &str,
box_width: usize,
text_style: ansi_term::Style,
decoration_style: ansi_term::Style,
- heavy: bool,
) -> std::io::Result<()> {
- let up_horizontal = if heavy {
+ let up_horizontal = if decoration_style.is_bold {
box_drawing::heavy::UP_HORIZONTAL
} else {
box_drawing::light::UP_HORIZONTAL
};
- write_boxed_partial(writer, text, box_width, text_style, decoration_style, heavy)?;
- write!(writer, "{}", decoration_style.paint(up_horizontal))?;
+ write_boxed_partial(writer, text, box_width, text_style, decoration_style)?;
+ write!(
+ writer,
+ "{}",
+ decoration_style.not_bold().paint(up_horizontal)
+ )?;
Ok(())
}
@@ -194,25 +194,22 @@ fn write_boxed_partial(
box_width: usize,
text_style: ansi_term::Style,
decoration_style: ansi_term::Style,
- heavy: bool,
) -> std::io::Result<()> {
- let horizontal = if heavy {
- box_drawing::heavy::HORIZONTAL
- } else {
- box_drawing::light::HORIZONTAL
- };
- let down_left = if heavy {
- box_drawing::heavy::DOWN_LEFT
+ let (horizontal, down_left, vertical) = if decoration_style.is_bold {
+ (
+ box_drawing::heavy::HORIZONTAL,
+ box_drawing::heavy::DOWN_LEFT,
+ box_drawing::heavy::VERTICAL,
+ )
} else {
- box_drawing::light::DOWN_LEFT
+ (
+ box_drawing::light::HORIZONTAL,
+ box_drawing::light::DOWN_LEFT,
+ box_drawing::light::VERTICAL,
+ )
};
- let vertical = if heavy {
- box_drawing::heavy::VERTICAL
- } else {
- box_drawing::light::VERTICAL
- };
-
let horizontal_edge = horizontal.repeat(box_width);
+ let decoration_style = decoration_style.not_bold();
write!(
writer,
"{}{}\n{}{}\n{}",
diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs
index cd9b1f12..b7f64399 100644
--- a/src/tests/test_example_diffs.rs
+++ b/src/tests/test_example_diffs.rs
@@ -226,7 +226,7 @@ commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e
fn test_commit_style_box() {
let mut options = integration_test_utils::get_command_line_options();
options.commit_style = "blue".to_string();
- options.commit_decoration_style = "blue box".to_string();
+ options.commit_decoration_style = "bold blue box".to_string();
let (output, config) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options);
ansi_test_utils::assert_line_has_foreground_color(
&output,
@@ -262,7 +262,7 @@ commit 94907c0f136f46dc46ffae2dc92dca9af7eb7c2e ┃
fn test_commit_style_underline() {
let mut options = integration_test_utils::get_command_line_options();
options.commit_style = "yellow".to_string();
- options.commit_decoration_style = "yellow underline".to_string();
+ options.commit_decoration_style = "bold yellow underline".to_string();
let (output, config) = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, options);
ansi_test_utils::assert_line_has_foreground_color(
&output,