summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2019-07-11 23:49:11 -0400
committerDan Davison <dandavison7@gmail.com>2019-07-12 01:05:32 -0400
commit58f4cbfffc8d5f52df8fc510e3dcdc93ed9d2551 (patch)
tree9b6c8196c589f5004f327524b1c80fcf31d08d0a
parent905fdce351b7704a385a2da0929576f8d7570eb1 (diff)
Refactor: box drawing module
-rw-r--r--Cargo.toml1
-rw-r--r--src/draw.rs50
-rw-r--r--src/main.rs1
-rw-r--r--src/parse.rs67
4 files changed, 85 insertions, 34 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 9f4be73d..1d6d36d2 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -6,6 +6,7 @@ edition = "2018"
[dependencies]
ansi_term = "0.11"
+box_drawing = "0.1.2"
console = "0.7.7"
shell-words = "0.1.0"
structopt = "0.2.16"
diff --git a/src/draw.rs b/src/draw.rs
new file mode 100644
index 00000000..5a9947a6
--- /dev/null
+++ b/src/draw.rs
@@ -0,0 +1,50 @@
+use std::io::Write;
+
+use ansi_term::Style;
+use box_drawing;
+
+/// Write text to stream, surrounded by a box, leaving the cursor just
+/// beyond the bottom right corner.
+pub fn write_boxed(
+ text: &str,
+ box_width: usize,
+ box_style: Style,
+ writer: &mut Write,
+) -> std::io::Result<()> {
+ _write_boxed_partial(text, box_width, box_style, writer)?;
+ write!(writer, "{}", box_style.paint(box_drawing::light::UP_LEFT))?;
+ Ok(())
+}
+
+pub fn write_boxed_with_line(
+ text: &str,
+ box_width: usize,
+ box_style: Style,
+ writer: &mut Write,
+) -> std::io::Result<()> {
+ _write_boxed_partial(text, box_width, box_style, writer)?;
+ write!(
+ writer,
+ "{}",
+ box_style.paint(box_drawing::light::UP_HORIZONTAL)
+ )?;
+ Ok(())
+}
+
+fn _write_boxed_partial(
+ text: &str,
+ box_width: usize,
+ box_style: Style,
+ writer: &mut Write,
+) -> std::io::Result<()> {
+ let horizontal_edge = box_drawing::light::HORIZONTAL.repeat(box_width);
+ write!(
+ writer,
+ "{}{}\n{} {}\n{}",
+ box_style.paint(&horizontal_edge),
+ box_style.paint(box_drawing::light::DOWN_LEFT),
+ text,
+ box_style.paint(box_drawing::light::VERTICAL),
+ box_style.paint(&horizontal_edge),
+ )
+}
diff --git a/src/main.rs b/src/main.rs
index bab4f4d8..e3b08acf 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -2,6 +2,7 @@
extern crate error_chain;
mod bat;
+mod draw;
mod paint;
mod parse;
diff --git a/src/parse.rs b/src/parse.rs
index ecb7e95e..d64272f2 100644
--- a/src/parse.rs
+++ b/src/parse.rs
@@ -1,9 +1,11 @@
use std::io::Write;
use ansi_term::Colour::Blue;
+use box_drawing;
use console::strip_ansi_codes;
use crate::bat::assets::HighlightingAssets;
+use crate::draw;
use crate::paint::{Config, Painter, NO_BACKGROUND_COLOR_STYLE_MODIFIER};
use crate::parse::parse_git_diff::{
get_file_change_description_from_diff_line, get_file_extension_from_diff_line,
@@ -73,16 +75,23 @@ pub fn delta(
};
if !config.no_structural_changes {
painter.emit()?;
- let hline = "━".repeat(config.terminal_width); // U+2501
let file_change_description = get_file_change_description_from_diff_line(&line);
let ansi_style = Blue.bold();
- writeln!(
+ let box_width = file_change_description.len() + 1;
+ draw::write_boxed_with_line(
+ &file_change_description,
+ box_width,
+ ansi_style,
painter.writer,
- "{}\n{}\n{}",
- ansi_style.paint(&hline),
- ansi_style.paint(&file_change_description),
- ansi_style.paint(&hline)
+ )?;
+ write!(
+ painter.writer,
+ "{}",
+ ansi_style.paint(
+ box_drawing::light::HORIZONTAL
+ .repeat(config.terminal_width - box_width - 1),
+ )
)?;
continue;
}
@@ -94,34 +103,24 @@ pub fn delta(
if !config.no_structural_changes {
painter.emit()?;
let (code_fragment, line_number) = parse_hunk_metadata(&line);
- painter.paint_lines(
- vec![code_fragment.clone()],
- vec![vec![(
- NO_BACKGROUND_COLOR_STYLE_MODIFIER,
- code_fragment.clone(),
- )]],
- );
- painter.output_buffer.pop(); // trim newline
-
- let hline_char = "─"; // U+2500
- let vline_char = "│"; // U+2502
- let top_right_corner_char = "┐"; // U+2510
- let bottom_right_corner_char = "┘"; // U+2518
- let hline_top = hline_char.repeat(code_fragment.len() + 1);
- let hline_bottom = hline_char.repeat(code_fragment.len() + 1);
- // let hline_bottom = hline_char.repeat(config.terminal_width);
- let ansi_style = Blue;
- writeln!(
- painter.writer,
- "{}{}\n{} {}\n{}{}\n{}",
- ansi_style.paint(&hline_top),
- ansi_style.paint(top_right_corner_char),
- painter.output_buffer,
- ansi_style.paint(vline_char),
- ansi_style.paint(&hline_bottom),
- ansi_style.paint(bottom_right_corner_char),
- ansi_style.paint(&line_number),
- )?;
+ let ansi_style = Blue.normal();
+ if code_fragment.len() > 0 {
+ painter.paint_lines(
+ vec![code_fragment.clone()],
+ vec![vec![(
+ NO_BACKGROUND_COLOR_STYLE_MODIFIER,
+ code_fragment.clone(),
+ )]],
+ );
+ painter.output_buffer.pop(); // trim newline
+ draw::write_boxed(
+ &painter.output_buffer,
+ code_fragment.len() + 1,
+ ansi_style,
+ &mut painter.writer,
+ )?;
+ }
+ writeln!(painter.writer, "\n{}", ansi_style.paint(line_number))?;
painter.output_buffer.truncate(0);
continue;
}