From 921e4a451e2c0b135dccc73c07d9d78317f93ae2 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Fri, 5 Aug 2016 10:49:16 -0700 Subject: [PATCH] Add outset border functions to Printer --- src/printer.rs | 71 +++++++++++++++++++++++++++------------------ src/views/dialog.rs | 6 ++-- 2 files changed, 47 insertions(+), 30 deletions(-) diff --git a/src/printer.rs b/src/printer.rs index 2a1eddb..ee8dbac 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -133,35 +133,14 @@ impl Printer { } let size = size - (1, 1); - let borders = if let Some(borders) = self.theme.borders { - borders - } else { - // No border, no box... - // TODO: still allow other boxes? - // For instance make the check in the view. - return; - }; + self.with_high_border(invert, |s| { + s.print(start, "┌"); + s.print(start + size.keep_y(), "└"); + s.print_hline(start + (1, 0), size.x - 1, "─"); + s.print_vline(start + (0, 1), size.y - 1, "│"); + }); - self.with_color(match borders { - BorderStyle::Outset if !invert => { - ColorStyle::Tertiary - } - _ => ColorStyle::Primary, - }, - |s| { - s.print(start, "┌"); - s.print(start + size.keep_y(), "└"); - s.print_hline(start + (1, 0), size.x - 1, "─"); - s.print_vline(start + (0, 1), size.y - 1, "│"); - }); - - self.with_color(match borders { - BorderStyle::Outset if invert => { - ColorStyle::Tertiary - } - _ => ColorStyle::Primary, - }, - |s| { + self.with_low_border(invert, |s| { s.print(start + size.keep_x(), "┐"); s.print(start + size, "┘"); s.print_hline(start + (1, 0) + size.keep_y(), size.x - 1, "─"); @@ -169,6 +148,42 @@ impl Printer { }); } + /// Runs the given function using a color depending on the theme. + /// + /// * If the theme's borders is `None`, return without calling `f`. + /// * If the theme's borders is "outset" and `invert` is `false`, + /// use `ColorStyle::Tertiary`. + /// * Otherwise, use `ColorStyle::Primary`. + pub fn with_high_border(&self, invert: bool, f: F) + where F: FnOnce(&Printer) + { + let color = match self.theme.borders { + None => return, + Some(BorderStyle::Outset) if !invert => ColorStyle::Tertiary, + _ => ColorStyle::Primary, + }; + + self.with_color(color, f); + } + + /// Runs the given function using a color depending on the theme. + /// + /// * If the theme's borders is `None`, return without calling `f`. + /// * If the theme's borders is "outset" and `invert` is `true`, + /// use `ColorStyle::Tertiary`. + /// * Otherwise, use `ColorStyle::Primary`. + pub fn with_low_border(&self, invert: bool, f: F) + where F: FnOnce(&Printer) + { + let color = match self.theme.borders { + None => return, + Some(BorderStyle::Outset) if invert => ColorStyle::Secondary, + _ => ColorStyle::Primary, + }; + + self.with_color(color, f); + } + /// Apply a selection style and call the given function. /// /// * If `selection` is `false`, simply uses `ColorStyle::Primary`. diff --git a/src/views/dialog.rs b/src/views/dialog.rs index 075a03b..77cbf98 100644 --- a/src/views/dialog.rs +++ b/src/views/dialog.rs @@ -226,8 +226,10 @@ impl View for Dialog { return; } let x = (printer.size.x - len) / 2; - printer.print((x - 2, 0), "┤ "); - printer.print((x + len, 0), " ├"); + printer.with_high_border(false, |printer| { + printer.print((x - 2, 0), "┤ "); + printer.print((x + len, 0), " ├"); + }); printer.with_color(ColorStyle::TitlePrimary, |p| p.print((x, 0), &self.title));