summaryrefslogtreecommitdiffstats
path: root/src/draw.rs
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 /src/draw.rs
parent905fdce351b7704a385a2da0929576f8d7570eb1 (diff)
Refactor: box drawing module
Diffstat (limited to 'src/draw.rs')
-rw-r--r--src/draw.rs50
1 files changed, 50 insertions, 0 deletions
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),
+ )
+}