2018-01-08 15:11:12 +00:00
|
|
|
//! Handle colors and themes in the UI.
|
|
|
|
//!
|
2018-01-21 05:13:30 +00:00
|
|
|
//! # Colors
|
|
|
|
//!
|
|
|
|
//! Characters can be printed in the terminal with a specific color.
|
|
|
|
//! The [`Color`] enum represents the ways this can be set.
|
|
|
|
//!
|
|
|
|
//! [`Color`]: enum.Color.html
|
|
|
|
//!
|
|
|
|
//! # Palette
|
2018-01-08 15:11:12 +00:00
|
|
|
//!
|
|
|
|
//! To achieve a customizable, yet unified look, Cursive uses a configurable
|
|
|
|
//! palette of colors to be used through the entire application.
|
|
|
|
//!
|
2018-01-21 05:13:30 +00:00
|
|
|
//! The [`PaletteColor`] enum defines possible entries in this palette.
|
|
|
|
//!
|
|
|
|
//! [`PaletteColor`]: enum.PaletteColor.html
|
2018-01-08 15:11:12 +00:00
|
|
|
//!
|
2018-01-21 05:13:30 +00:00
|
|
|
//! These entries are:
|
|
|
|
//!
|
|
|
|
//! * **`Background`**: used to color the application background
|
2018-01-08 15:11:12 +00:00
|
|
|
//! (around views).
|
|
|
|
//! Defaults to **blue**.
|
2018-01-21 05:13:30 +00:00
|
|
|
//! * **`Shadow`**: used to color shadow around views.
|
2018-01-08 15:11:12 +00:00
|
|
|
//! Defaults to **black**.
|
2018-01-21 05:13:30 +00:00
|
|
|
//! * **`View`**: used to color the background for views.
|
2018-01-08 15:11:12 +00:00
|
|
|
//! Defaults to **white**.
|
2018-01-21 05:13:30 +00:00
|
|
|
//! * **`Primary`**: used to print primary text.
|
2018-01-08 15:11:12 +00:00
|
|
|
//! Defaults to **black**.
|
2018-01-21 05:13:30 +00:00
|
|
|
//! * **`Secondary`**: used to print secondary text.
|
2018-01-08 15:11:12 +00:00
|
|
|
//! Defaults to **blue**.
|
2018-01-21 05:13:30 +00:00
|
|
|
//! * **`Tertiary`**: used to print tertiary text.
|
2018-01-08 15:11:12 +00:00
|
|
|
//! Defaults to **white**.
|
2018-01-21 05:13:30 +00:00
|
|
|
//! * **`TitlePrimary`**: used to print primary titles.
|
2018-01-08 15:11:12 +00:00
|
|
|
//! Defaults to **red**.
|
2018-01-21 05:13:30 +00:00
|
|
|
//! * **`TitleSecondary`**: used to print secondary titles.
|
2018-01-08 15:11:12 +00:00
|
|
|
//! Defaults to **yellow**.
|
2018-01-21 05:13:30 +00:00
|
|
|
//! * **`Highlight`**: used to highlight selected items.
|
2018-01-08 15:11:12 +00:00
|
|
|
//! Defaults to **red**.
|
2018-01-21 05:13:30 +00:00
|
|
|
//! * **`HighlightInactive`**: used to highlight selected but inactive items.
|
2018-01-08 15:11:12 +00:00
|
|
|
//! Defaults to **blue**.
|
|
|
|
//!
|
2018-01-21 05:13:30 +00:00
|
|
|
//! A [`Palette`] then maps each of these to an actual [`Color`].
|
|
|
|
//!
|
2018-01-21 19:20:08 +00:00
|
|
|
//! [`Palette`]: type.Palette.html
|
2018-01-21 05:13:30 +00:00
|
|
|
//! [`Color`]: enum.Color.html
|
|
|
|
//!
|
|
|
|
//! # Color Types
|
|
|
|
//!
|
|
|
|
//! When drawing views, color can be picked in two way:
|
|
|
|
//!
|
|
|
|
//! * An exact [`Color`] can be given directly
|
|
|
|
//! * A [`PaletteColor`] entry can be given, which will fetch whatever color
|
|
|
|
//! is currently defined for this.
|
|
|
|
//!
|
|
|
|
//! The [`ColorType`] enum abstract over these two choices.
|
|
|
|
//!
|
|
|
|
//! [`ColorType`]: enum.ColorType.html
|
|
|
|
//!
|
2018-01-08 15:11:12 +00:00
|
|
|
//! # Color Styles
|
|
|
|
//!
|
2018-01-21 05:13:30 +00:00
|
|
|
//! To actually print anything, two colors must be picked: one for the
|
|
|
|
//! foreground, and one for the background.
|
2018-01-08 15:11:12 +00:00
|
|
|
//!
|
2018-01-21 05:13:30 +00:00
|
|
|
//! As such, a [`ColorStyle`] is made of a pair of `ColorType`.
|
2018-01-08 15:11:12 +00:00
|
|
|
//!
|
2018-01-21 05:13:30 +00:00
|
|
|
//! Since some pairs are frequently used, `ColorStyle` defines some methods to
|
|
|
|
//! create these usual values:
|
|
|
|
//!
|
|
|
|
//! * **`ColorStyle::background()`**: style used to print the application background.
|
|
|
|
//! * Its *background* color is `Background`.
|
2018-01-08 15:11:12 +00:00
|
|
|
//! * Its *foreground* color is unimportant as no characters are ever
|
|
|
|
//! printed in the background.
|
2018-01-21 05:13:30 +00:00
|
|
|
//! * **`ColorStyle::shadow()`**: style used to print shadows behind views.
|
|
|
|
//! * Its *background* color is `Shadow`.
|
2018-01-08 15:11:12 +00:00
|
|
|
//! * Here again, the *foreground* color is unimportant.
|
2018-01-21 05:13:30 +00:00
|
|
|
//! * **`ColorStyle::primary()`**: style used to print primary text.
|
|
|
|
//! * Its *background* color is `View`.
|
|
|
|
//! * Its *foreground* color is `Primary`.
|
|
|
|
//! * **`ColorStyle::secondary()`**: style used to print secondary text.
|
|
|
|
//! * Its *background* color is `View`.
|
|
|
|
//! * Its *foreground* color is `Secondary`.
|
|
|
|
//! * **`ColorStyle::tertiary()`**: style used to print tertiary text.
|
|
|
|
//! * Its *background* color is `View`.
|
|
|
|
//! * Its *foreground* color is `Tertiary`.
|
|
|
|
//! * **`ColorStyle::title_primary()`**: style used to print titles.
|
|
|
|
//! * Its *background* color is `View`.
|
|
|
|
//! * Its *foreground* color is `TitlePrimary`.
|
|
|
|
//! * **`ColorStyle::title_secondary()`**: style used to print secondary titles.
|
|
|
|
//! * Its *background* color is `View`.
|
|
|
|
//! * Its *foreground* color is `TitleSecondary`.
|
|
|
|
//! * **`ColorStyle::highlight()`**: style used to print selected items.
|
|
|
|
//! * Its *background* color is `Highlight`.
|
|
|
|
//! * Its *foreground* color is `View`.
|
|
|
|
//! * **`ColorStyle::highlight_inactive()`**: style used to print selected,
|
2018-01-08 15:11:12 +00:00
|
|
|
//! but inactive items.
|
2018-01-21 05:13:30 +00:00
|
|
|
//! * Its *background* color is `HighlightInactive`.
|
|
|
|
//! * Its *foreground* color is `View`.
|
2018-01-08 15:11:12 +00:00
|
|
|
//!
|
|
|
|
//! Using one of these pairs when styling your application helps give it a
|
|
|
|
//! coherent look.
|
|
|
|
//!
|
2018-01-21 05:13:30 +00:00
|
|
|
//! [`ColorStyle`]: struct.ColorStyle.html
|
|
|
|
//!
|
2018-01-08 15:11:12 +00:00
|
|
|
//! # Effects
|
|
|
|
//!
|
|
|
|
//! On top of a color style, some effects can be applied on cells: `Reverse`,
|
|
|
|
//! for instance, swaps the foreground and background colors of a cell.
|
|
|
|
//!
|
2018-01-21 05:18:29 +00:00
|
|
|
//!
|
2018-01-21 05:13:30 +00:00
|
|
|
//! # Style
|
|
|
|
//!
|
|
|
|
//! Finally, a style combine a [`ColorType`] and a set of [`Effect`]s, to
|
|
|
|
//! represent any way text should be printed on screen.
|
|
|
|
//!
|
2018-01-21 05:18:29 +00:00
|
|
|
//! [`Effect`]: enum.Effect.html
|
|
|
|
//!
|
2018-01-08 15:11:12 +00:00
|
|
|
//! # Themes
|
|
|
|
//!
|
|
|
|
//! A theme defines the color palette an application will use, as well as
|
|
|
|
//! various options to style views.
|
|
|
|
//!
|
|
|
|
//! Themes are described in toml configuration files. All fields are optional.
|
|
|
|
//!
|
|
|
|
//! Here are the possible entries:
|
|
|
|
//!
|
|
|
|
//! ```toml
|
|
|
|
//! # Every field in a theme file is optional.
|
|
|
|
//!
|
|
|
|
//! # First come some various options
|
|
|
|
//! shadow = false # Don't draw shadows around stacked views
|
|
|
|
//! borders = "simple" # Alternatives are "none" and "outset"
|
|
|
|
//!
|
|
|
|
//! # Here we define the color palette.
|
|
|
|
//! [colors]
|
|
|
|
//! background = "black"
|
|
|
|
//! # If the value is an array, the first valid color will be used.
|
|
|
|
//! # If the terminal doesn't support custom color,
|
|
|
|
//! # non-base colors will be skipped.
|
|
|
|
//! shadow = ["#000000", "black"]
|
|
|
|
//! view = "#d3d7cf"
|
|
|
|
//!
|
|
|
|
//! # Array and simple values have the same effect.
|
|
|
|
//! primary = ["#111111"]
|
|
|
|
//! secondary = "#EEEEEE"
|
|
|
|
//! tertiary = "#444444"
|
|
|
|
//!
|
|
|
|
//! # Hex values can use lower or uppercase.
|
|
|
|
//! # (base color MUST be lowercase)
|
|
|
|
//! title_primary = "#ff5555"
|
|
|
|
//! title_secondary = "#ffff55"
|
|
|
|
//!
|
|
|
|
//! # Lower precision values can use only 3 digits.
|
|
|
|
//! highlight = "#F00"
|
|
|
|
//! highlight_inactive = "#5555FF"
|
|
|
|
//! ```
|
|
|
|
mod style;
|
|
|
|
mod effect;
|
|
|
|
mod color;
|
|
|
|
mod color_pair;
|
|
|
|
mod color_style;
|
|
|
|
mod border_style;
|
|
|
|
mod palette;
|
|
|
|
|
|
|
|
pub use self::border_style::BorderStyle;
|
2018-01-09 14:17:49 +00:00
|
|
|
pub use self::color::{BaseColor, Color};
|
2018-01-08 15:11:12 +00:00
|
|
|
pub use self::color_pair::ColorPair;
|
2018-01-17 17:35:57 +00:00
|
|
|
pub use self::color_style::{ColorStyle, ColorType};
|
2018-01-08 15:11:12 +00:00
|
|
|
pub use self::effect::Effect;
|
2018-01-21 19:06:33 +00:00
|
|
|
pub use self::palette::{Palette, PaletteColor, default_palette};
|
2018-01-08 15:11:12 +00:00
|
|
|
pub use self::style::Style;
|
|
|
|
use std::fs::File;
|
|
|
|
use std::io;
|
|
|
|
use std::io::Read;
|
|
|
|
use std::path::Path;
|
|
|
|
use toml;
|
|
|
|
|
|
|
|
/// Represents the style a Cursive application will use.
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub struct Theme {
|
|
|
|
/// Whether views in a StackView should have shadows.
|
|
|
|
pub shadow: bool,
|
|
|
|
/// How view borders should be drawn.
|
|
|
|
pub borders: BorderStyle,
|
|
|
|
/// What colors should be used through the application?
|
2018-01-17 17:35:57 +00:00
|
|
|
pub palette: Palette,
|
2018-01-08 15:11:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Theme {
|
|
|
|
fn default() -> Self {
|
|
|
|
Theme {
|
|
|
|
shadow: true,
|
|
|
|
borders: BorderStyle::Simple,
|
2018-01-21 19:06:33 +00:00
|
|
|
palette: default_palette(),
|
2018-01-08 15:11:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Theme {
|
|
|
|
fn load(&mut self, table: &toml::value::Table) {
|
|
|
|
if let Some(&toml::Value::Boolean(shadow)) = table.get("shadow") {
|
|
|
|
self.shadow = shadow;
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(&toml::Value::String(ref borders)) = table.get("borders") {
|
|
|
|
self.borders = BorderStyle::from(borders);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(&toml::Value::Table(ref table)) = table.get("colors") {
|
2018-01-21 19:06:33 +00:00
|
|
|
palette::load_table(&mut self.palette, table);
|
2018-01-08 15:11:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Possible error returned when loading a theme.
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
/// An error occured when reading the file.
|
|
|
|
Io(io::Error),
|
|
|
|
/// An error occured when parsing the toml content.
|
|
|
|
Parse(toml::de::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<io::Error> for Error {
|
|
|
|
fn from(err: io::Error) -> Self {
|
|
|
|
Error::Io(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<toml::de::Error> for Error {
|
|
|
|
fn from(err: toml::de::Error) -> Self {
|
|
|
|
Error::Parse(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads a theme from file and sets it as active.
|
|
|
|
pub fn load_theme_file<P: AsRef<Path>>(filename: P) -> Result<Theme, Error> {
|
|
|
|
let content = {
|
|
|
|
let mut content = String::new();
|
|
|
|
let mut file = try!(File::open(filename));
|
|
|
|
try!(file.read_to_string(&mut content));
|
|
|
|
content
|
|
|
|
};
|
|
|
|
|
|
|
|
load_theme(&content)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads a theme string and sets it as active.
|
|
|
|
pub fn load_theme(content: &str) -> Result<Theme, Error> {
|
|
|
|
let table = toml::de::from_str(content)?;
|
|
|
|
|
|
|
|
let mut theme = Theme::default();
|
|
|
|
theme.load(&table);
|
|
|
|
|
|
|
|
Ok(theme)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads the default theme, and returns its representation.
|
|
|
|
pub fn load_default() -> Theme {
|
|
|
|
Theme::default()
|
|
|
|
}
|