Update Style to include a set of effects

This commit is contained in:
Alexandre Bury 2018-01-08 15:44:27 +01:00
parent b59e480f60
commit c2d2fa0527
4 changed files with 61 additions and 23 deletions

View File

@ -34,6 +34,7 @@ toml = "0.4"
unicode-segmentation = "1.0" unicode-segmentation = "1.0"
unicode-width = "0.1" unicode-width = "0.1"
xi-unicode = "0.1.0" xi-unicode = "0.1.0"
enumset = "0.3.3"
[dependencies.bear-lib-terminal] [dependencies.bear-lib-terminal]
optional = true optional = true

View File

@ -59,16 +59,18 @@
//! Or you can use gdb as usual. //! Or you can use gdb as usual.
#![deny(missing_docs)] #![deny(missing_docs)]
#[macro_use]
extern crate enumset;
#[macro_use] #[macro_use]
extern crate log; extern crate log;
#[macro_use] #[macro_use]
extern crate maplit; extern crate maplit;
extern crate xi_unicode;
extern crate num; extern crate num;
extern crate owning_ref; extern crate owning_ref;
extern crate toml; extern crate toml;
extern crate unicode_segmentation; extern crate unicode_segmentation;
extern crate unicode_width; extern crate unicode_width;
extern crate xi_unicode;
#[cfg(feature = "termion")] #[cfg(feature = "termion")]
#[macro_use] #[macro_use]

View File

@ -1,10 +1,11 @@
//! Makes drawing on ncurses windows easier. //! Makes drawing on ncurses windows easier.
use backend::{self, Backend}; use backend::{self, Backend};
use enumset::EnumSet;
use std::cell::Cell; use std::cell::Cell;
use std::cmp::min; use std::cmp::min;
use std::rc::Rc; use std::rc::Rc;
use theme::{BorderStyle, ColorStyle, Effect, Theme}; use theme::{BorderStyle, ColorStyle, Effect, Style, Theme};
use unicode_segmentation::UnicodeSegmentation; use unicode_segmentation::UnicodeSegmentation;
use utils::lines::simple::prefix; use utils::lines::simple::prefix;
use vec::Vec2; use vec::Vec2;
@ -133,10 +134,26 @@ impl<'a> Printer<'a> {
self.backend.with_color(c.resolve(self.theme), || f(self)); self.backend.with_color(c.resolve(self.theme), || f(self));
} }
/// Same as `with_color`, but apply a ncurses style instead, /// Call the given closure with a styled printer,
/// like `ncurses::A_BOLD()` or `ncurses::A_REVERSE()`. /// that will apply the given style on prints.
/// pub fn with_style<F>(&self, style: Style, f: F)
/// Will probably use a cursive enum some day. 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<F>(&self, effect: Effect, f: F) pub fn with_effect<F>(&self, effect: Effect, f: F)
where where
F: FnOnce(&Printer), F: FnOnce(&Printer),
@ -144,6 +161,22 @@ impl<'a> Printer<'a> {
self.backend.with_effect(effect, || f(self)); 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<F>(&self, effects: EnumSet<Effect>, 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. /// Prints a rectangular box.
/// ///
/// If `invert` is `true`, and the theme uses `Outset` borders, then the /// If `invert` is `true`, and the theme uses `Outset` borders, then the

View File

@ -114,6 +114,7 @@
//! highlight_inactive = "#5555FF" //! highlight_inactive = "#5555FF"
//! ``` //! ```
use enumset::EnumSet;
use std::fs::File; use std::fs::File;
use std::io; use std::io;
use std::io::Read; use std::io::Read;
@ -128,7 +129,7 @@ pub struct Style {
/// Effect to apply. /// Effect to apply.
/// ///
/// `None` to keep using previous effects. /// `None` to keep using previous effects.
pub effect: Option<Effect>, pub effects: EnumSet<Effect>,
/// Color style to apply. /// Color style to apply.
/// ///
@ -140,7 +141,7 @@ impl Style {
/// Returns a new `Style` that doesn't apply anything. /// Returns a new `Style` that doesn't apply anything.
pub fn none() -> Self { pub fn none() -> Self {
Style { Style {
effect: None, effects: EnumSet::new(),
color: None, color: None,
} }
} }
@ -149,7 +150,7 @@ impl Style {
impl From<Effect> for Style { impl From<Effect> for Style {
fn from(effect: Effect) -> Self { fn from(effect: Effect) -> Self {
Style { Style {
effect: Some(effect), effects: enum_set!(Effect, effect),
color: None, color: None,
} }
} }
@ -158,15 +159,15 @@ impl From<Effect> for Style {
impl From<ColorStyle> for Style { impl From<ColorStyle> for Style {
fn from(color: ColorStyle) -> Self { fn from(color: ColorStyle) -> Self {
Style { Style {
effect: None, effects: EnumSet::new(),
color: Some(color), color: Some(color),
} }
} }
} }
/// Text effect enum_set_type! {
#[derive(Clone, Copy, Debug, PartialEq, Eq)] /// Text effect
pub enum Effect { pub enum Effect {
/// No effect /// No effect
Simple, Simple,
/// Reverses foreground and background colors /// Reverses foreground and background colors
@ -177,6 +178,7 @@ pub enum Effect {
Italic, Italic,
/// Prints foreground with underline /// Prints foreground with underline
Underline, Underline,
}
} }
/// Combines a front and back color. /// Combines a front and back color.