summaryrefslogtreecommitdiffstats
path: root/src/draw.rs
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2020-02-25 20:17:03 -0600
committerDan Davison <dandavison7@gmail.com>2020-02-27 10:51:23 -0600
commit27e5673a40edbb7fe9d5429d60c052e5c91fba04 (patch)
tree529263be2b29b21b436b6f498ce2238ca3682657 /src/draw.rs
parent1f83397bf6028c3e4f927adb8b3bc06408f28d7b (diff)
Provide CLI options for all colors
- Fixes #99, #103 - Support both RGB and ANSI codes - Eliminate (almost) ansi_term crate in favor of syntect for representing colors
Diffstat (limited to 'src/draw.rs')
-rw-r--r--src/draw.rs48
1 files changed, 27 insertions, 21 deletions
diff --git a/src/draw.rs b/src/draw.rs
index 9487c557..751f99f9 100644
--- a/src/draw.rs
+++ b/src/draw.rs
@@ -1,17 +1,19 @@
use std::io::Write;
-use ansi_term::Style;
use box_drawing;
use console::strip_ansi_codes;
+use syntect::highlighting::Color;
use unicode_segmentation::UnicodeSegmentation;
+use crate::paint;
+
/// Write text to stream, surrounded by a box, leaving the cursor just
/// beyond the bottom right corner.
pub fn write_boxed(
writer: &mut dyn Write,
text: &str,
_line_width: usize, // ignored
- line_style: Style,
+ color: Color,
heavy: bool,
) -> std::io::Result<()> {
let up_left = if heavy {
@@ -20,8 +22,8 @@ pub fn write_boxed(
box_drawing::light::UP_LEFT
};
let box_width = strip_ansi_codes(text).graphemes(true).count() + 1;
- write_boxed_partial(writer, text, box_width, line_style, heavy)?;
- write!(writer, "{}", line_style.paint(up_left))?;
+ write_boxed_partial(writer, text, box_width, color, heavy)?;
+ write!(writer, "{}", paint::paint_text_foreground(up_left, color))?;
Ok(())
}
@@ -31,11 +33,11 @@ pub fn write_boxed_with_line(
writer: &mut dyn Write,
text: &str,
line_width: usize,
- line_style: Style,
+ color: Color,
heavy: bool,
) -> std::io::Result<()> {
let box_width = strip_ansi_codes(text).graphemes(true).count() + 1;
- write_boxed_with_horizontal_whisker(writer, text, box_width, line_style, heavy)?;
+ write_boxed_with_horizontal_whisker(writer, text, box_width, color, heavy)?;
write_horizontal_line(
writer,
if line_width > box_width {
@@ -43,7 +45,7 @@ pub fn write_boxed_with_line(
} else {
0
},
- line_style,
+ color,
heavy,
)?;
write!(writer, "\n")?;
@@ -54,11 +56,11 @@ pub fn write_underlined(
writer: &mut dyn Write,
text: &str,
line_width: usize,
- line_style: Style,
+ color: Color,
heavy: bool,
) -> std::io::Result<()> {
- writeln!(writer, "{}", line_style.paint(text))?;
- write_horizontal_line(writer, line_width - 1, line_style, heavy)?;
+ writeln!(writer, "{}", paint::paint_text_foreground(text, color))?;
+ write_horizontal_line(writer, line_width - 1, color, heavy)?;
write!(writer, "\n")?;
Ok(())
}
@@ -66,7 +68,7 @@ pub fn write_underlined(
fn write_horizontal_line(
writer: &mut dyn Write,
line_width: usize,
- line_style: Style,
+ color: Color,
heavy: bool,
) -> std::io::Result<()> {
let horizontal = if heavy {
@@ -77,7 +79,7 @@ fn write_horizontal_line(
write!(
writer,
"{}",
- line_style.paint(horizontal.repeat(line_width),)
+ paint::paint_text_foreground(&horizontal.repeat(line_width), color)
)
}
@@ -85,7 +87,7 @@ pub fn write_boxed_with_horizontal_whisker(
writer: &mut dyn Write,
text: &str,
box_width: usize,
- line_style: Style,
+ color: Color,
heavy: bool,
) -> std::io::Result<()> {
let up_horizontal = if heavy {
@@ -93,8 +95,12 @@ pub fn write_boxed_with_horizontal_whisker(
} else {
box_drawing::light::UP_HORIZONTAL
};
- write_boxed_partial(writer, text, box_width, line_style, heavy)?;
- write!(writer, "{}", line_style.paint(up_horizontal))?;
+ write_boxed_partial(writer, text, box_width, color, heavy)?;
+ write!(
+ writer,
+ "{}",
+ paint::paint_text_foreground(up_horizontal, color)
+ )?;
Ok(())
}
@@ -102,7 +108,7 @@ fn write_boxed_partial(
writer: &mut dyn Write,
text: &str,
box_width: usize,
- line_style: Style,
+ color: Color,
heavy: bool,
) -> std::io::Result<()> {
let horizontal = if heavy {
@@ -125,10 +131,10 @@ fn write_boxed_partial(
write!(
writer,
"{}{}\n{} {}\n{}",
- line_style.paint(&horizontal_edge),
- line_style.paint(down_left),
- line_style.paint(text),
- line_style.paint(vertical),
- line_style.paint(&horizontal_edge),
+ paint::paint_text_foreground(&horizontal_edge, color),
+ paint::paint_text_foreground(down_left, color),
+ paint::paint_text_foreground(text, color),
+ paint::paint_text_foreground(vertical, color),
+ paint::paint_text_foreground(&horizontal_edge, color),
)
}