From c2d2fa05274f8c0704f47a72bc9b02d040d53c6d Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Mon, 8 Jan 2018 15:44:27 +0100 Subject: [PATCH] Update Style to include a set of effects --- Cargo.toml | 1 + src/lib.rs | 4 +++- src/printer.rs | 43 ++++++++++++++++++++++++++++++++++++++----- src/theme.rs | 36 +++++++++++++++++++----------------- 4 files changed, 61 insertions(+), 23 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a18e3f4..453d138 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -34,6 +34,7 @@ toml = "0.4" unicode-segmentation = "1.0" unicode-width = "0.1" xi-unicode = "0.1.0" +enumset = "0.3.3" [dependencies.bear-lib-terminal] optional = true diff --git a/src/lib.rs b/src/lib.rs index f0bbc71..50be25f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -59,16 +59,18 @@ //! Or you can use gdb as usual. #![deny(missing_docs)] +#[macro_use] +extern crate enumset; #[macro_use] extern crate log; #[macro_use] extern crate maplit; -extern crate xi_unicode; extern crate num; extern crate owning_ref; extern crate toml; extern crate unicode_segmentation; extern crate unicode_width; +extern crate xi_unicode; #[cfg(feature = "termion")] #[macro_use] diff --git a/src/printer.rs b/src/printer.rs index 416a458..1cf6581 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -1,10 +1,11 @@ //! Makes drawing on ncurses windows easier. use backend::{self, Backend}; +use enumset::EnumSet; use std::cell::Cell; use std::cmp::min; use std::rc::Rc; -use theme::{BorderStyle, ColorStyle, Effect, Theme}; +use theme::{BorderStyle, ColorStyle, Effect, Style, Theme}; use unicode_segmentation::UnicodeSegmentation; use utils::lines::simple::prefix; use vec::Vec2; @@ -133,10 +134,26 @@ impl<'a> Printer<'a> { self.backend.with_color(c.resolve(self.theme), || f(self)); } - /// Same as `with_color`, but apply a ncurses style instead, - /// like `ncurses::A_BOLD()` or `ncurses::A_REVERSE()`. - /// - /// Will probably use a cursive enum some day. + /// Call the given closure with a styled printer, + /// that will apply the given style on prints. + pub fn with_style(&self, style: Style, f: F) + where + F: FnOnce(&Printer), + { + let color = style.color; + let effects = style.effects; + + if let Some(color) = color { + self.with_color(color, |printer| { + printer.with_effects(effects, f); + }); + } else { + self.with_effects(effects, f); + } + } + + /// Call the given closure with a modified printer + /// that will apply the given effect on prints. pub fn with_effect(&self, effect: Effect, f: F) where F: FnOnce(&Printer), @@ -144,6 +161,22 @@ impl<'a> Printer<'a> { self.backend.with_effect(effect, || f(self)); } + /// Call the given closure with a modified printer + /// that will apply each given effect on prints. + pub fn with_effects(&self, effects: EnumSet, f: F) + where + F: FnOnce(&Printer), + { + match effects.iter().next() { + None => f(self), + Some(effect) => { + let mut effects = effects; + effects.remove(effect); + self.with_effects(effects, f); + } + } + } + /// Prints a rectangular box. /// /// If `invert` is `true`, and the theme uses `Outset` borders, then the diff --git a/src/theme.rs b/src/theme.rs index 3bb2035..b092ddb 100644 --- a/src/theme.rs +++ b/src/theme.rs @@ -114,6 +114,7 @@ //! highlight_inactive = "#5555FF" //! ``` +use enumset::EnumSet; use std::fs::File; use std::io; use std::io::Read; @@ -128,7 +129,7 @@ pub struct Style { /// Effect to apply. /// /// `None` to keep using previous effects. - pub effect: Option, + pub effects: EnumSet, /// Color style to apply. /// @@ -140,7 +141,7 @@ impl Style { /// Returns a new `Style` that doesn't apply anything. pub fn none() -> Self { Style { - effect: None, + effects: EnumSet::new(), color: None, } } @@ -149,7 +150,7 @@ impl Style { impl From for Style { fn from(effect: Effect) -> Self { Style { - effect: Some(effect), + effects: enum_set!(Effect, effect), color: None, } } @@ -158,25 +159,26 @@ impl From for Style { impl From for Style { fn from(color: ColorStyle) -> Self { Style { - effect: None, + effects: EnumSet::new(), color: Some(color), } } } -/// Text effect -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum Effect { - /// No effect - Simple, - /// Reverses foreground and background colors - Reverse, - /// Prints foreground in bold - Bold, - /// Prints foreground in italic - Italic, - /// Prints foreground with underline - Underline, +enum_set_type! { + /// Text effect + pub enum Effect { + /// No effect + Simple, + /// Reverses foreground and background colors + Reverse, + /// Prints foreground in bold + Bold, + /// Prints foreground in italic + Italic, + /// Prints foreground with underline + Underline, + } } /// Combines a front and back color.