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-width = "0.1"
xi-unicode = "0.1.0"
enumset = "0.3.3"
[dependencies.bear-lib-terminal]
optional = true

View File

@ -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]

View File

@ -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<F>(&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<F>(&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<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.
///
/// If `invert` is `true`, and the theme uses `Outset` borders, then the

View File

@ -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<Effect>,
pub effects: EnumSet<Effect>,
/// 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<Effect> for Style {
fn from(effect: Effect) -> Self {
Style {
effect: Some(effect),
effects: enum_set!(Effect, effect),
color: None,
}
}
@ -158,25 +159,26 @@ impl From<Effect> for Style {
impl From<ColorStyle> 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.