mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-09 19:00:46 +00:00
Refactor themes and colors
And adds a theme example. TODO: Shadow & Borders support
This commit is contained in:
parent
cb523e88ae
commit
e5cb687990
@ -48,3 +48,7 @@ path = "examples/key_codes.rs"
|
||||
[[example]]
|
||||
name = "select"
|
||||
path = "examples/select.rs"
|
||||
|
||||
[[example]]
|
||||
name = "theme"
|
||||
path = "examples/theme.rs"
|
||||
|
@ -1,17 +1,17 @@
|
||||
# Everything in a theme file is optional.
|
||||
# Every field in a theme file is optional.
|
||||
|
||||
shadow = false
|
||||
borders = "simple", # Alternatives are "none" and "outset"
|
||||
borders = "simple" # Alternatives are "none" and "outset"
|
||||
|
||||
# Base colors are red, green, blue,
|
||||
# cyan, magenta, yellow, white and black.
|
||||
[colors]
|
||||
background = "black"
|
||||
background = "#123456"
|
||||
# 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"
|
||||
shadow = ["#222222", "black"]
|
||||
view = "#888888"
|
||||
|
||||
# Array and simple values have the same effect.
|
||||
primary = ["#111111"]
|
||||
@ -20,10 +20,10 @@ borders = "simple", # Alternatives are "none" and "outset"
|
||||
|
||||
# Hex values can use lower or uppercase.
|
||||
# (base color MUST be lowercase)
|
||||
title_primary = "#ff5555"
|
||||
title_primary = "#883333"
|
||||
title_secondary = "#ffff55"
|
||||
|
||||
# Lower precision values can use only 3 digits.
|
||||
highlight = "#F00"
|
||||
highlight = "#833"
|
||||
highlight_inactive = "#5555FF"
|
||||
|
||||
|
15
examples/theme.rs
Normal file
15
examples/theme.rs
Normal file
@ -0,0 +1,15 @@
|
||||
extern crate cursive;
|
||||
|
||||
use cursive::Cursive;
|
||||
use cursive::view::{Dialog,TextView};
|
||||
|
||||
fn main() {
|
||||
let mut siv = Cursive::new();
|
||||
siv.load_theme("assets/style.toml");
|
||||
|
||||
siv.add_layer(Dialog::new(TextView::new("This application uses a custom theme!"))
|
||||
.title("Themed dialog")
|
||||
.button("Quit", |s| s.quit()));
|
||||
|
||||
siv.run();
|
||||
}
|
329
src/color.rs
329
src/color.rs
@ -1,329 +0,0 @@
|
||||
//! Module to handle colors and themes in the UI.
|
||||
|
||||
use std::io;
|
||||
use std::io::Read;
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
|
||||
use ncurses;
|
||||
use toml;
|
||||
|
||||
/// Represents a colorpair from a Theme.
|
||||
pub type ThemeColor = i16;
|
||||
|
||||
/// Application background, where no view is present.
|
||||
pub const BACKGROUND: ThemeColor = 1;
|
||||
/// Color used by view shadows. Only background matters.
|
||||
pub const SHADOW: ThemeColor = 2;
|
||||
/// Main text with default background.
|
||||
pub const PRIMARY: ThemeColor = 3;
|
||||
/// Secondary text color, with default background.
|
||||
pub const SECONDARY: ThemeColor = 4;
|
||||
/// Tertiary text color, with default background.
|
||||
pub const TERTIARY: ThemeColor = 5;
|
||||
/// Title text color with default background.
|
||||
pub const TITLE_PRIMARY: ThemeColor = 6;
|
||||
/// Alternative color for a title.
|
||||
pub const TITLE_SECONDARY: ThemeColor = 7;
|
||||
/// Alternate text with highlight background.
|
||||
pub const HIGHLIGHT: ThemeColor = 8;
|
||||
/// Highlight color for inactive views (not in focus).
|
||||
pub const HIGHLIGHT_INACTIVE: ThemeColor = 9;
|
||||
|
||||
fn load_hex(s: &str) -> i16 {
|
||||
let mut sum = 0;
|
||||
for c in s.chars() {
|
||||
sum *= 16;
|
||||
sum += match c {
|
||||
n @ '0' ... '9' => n as i16 - '0' as i16,
|
||||
n @ 'a' ... 'f' => n as i16 - 'a' as i16 + 10,
|
||||
n @ 'A' ... 'F' => n as i16 - 'A' as i16 + 10,
|
||||
_ => 0,
|
||||
};
|
||||
}
|
||||
|
||||
sum
|
||||
}
|
||||
|
||||
/// Defines a color as used by a theme.
|
||||
///
|
||||
/// Can be created from rgb values, or from a preset color.
|
||||
pub struct Color {
|
||||
/// Red component. Between 0 and 1000.
|
||||
pub r: i16,
|
||||
/// Green component. Between 0 and 1000.
|
||||
pub g: i16,
|
||||
/// Blue component. Between 0 and 1000.
|
||||
pub b: i16,
|
||||
}
|
||||
|
||||
impl Color {
|
||||
/// Returns a new color from the given values.
|
||||
pub fn new(r:i16, g:i16, b:i16) -> Self {
|
||||
Color {
|
||||
r:r,
|
||||
g:g,
|
||||
b:b,
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns a black color: (0,0,0).
|
||||
pub fn black() -> Self {
|
||||
Color::new(0,0,0)
|
||||
}
|
||||
|
||||
/// Returns a white color: (1000,1000,1000).
|
||||
pub fn white() -> Self {
|
||||
Color::new(1000,1000,1000)
|
||||
}
|
||||
|
||||
/// Returns a red color: (1000,0,0).
|
||||
pub fn red() -> Self {
|
||||
Color::new(1000,0,0)
|
||||
}
|
||||
|
||||
/// Returns a green color: (0,1000,0).
|
||||
pub fn green() -> Self {
|
||||
Color::new(0,1000,0)
|
||||
}
|
||||
|
||||
/// Returns a blue color: (0,0,1000).
|
||||
pub fn blue() -> Self {
|
||||
Color::new(0,0,1000)
|
||||
}
|
||||
|
||||
/// Returns a light gray color: (700,700,700).
|
||||
pub fn gray() -> Self {
|
||||
Color::new(700,700,700)
|
||||
}
|
||||
|
||||
/// Returns a dark gray color: (300,300,300).
|
||||
pub fn dark_gray() -> Self {
|
||||
Color::new(300,300,300)
|
||||
}
|
||||
|
||||
/// Returns a yellow color: (1000,1000,0).
|
||||
pub fn yellow() -> Self {
|
||||
Color::new(1000,1000,0)
|
||||
}
|
||||
|
||||
/// Returns a cyan color: (0,1000,1000).
|
||||
pub fn cyan() -> Self {
|
||||
Color::new(0,1000,1000)
|
||||
}
|
||||
|
||||
/// Returns a magenta color: (1000,0,1000).
|
||||
pub fn magenta() -> Self {
|
||||
Color::new(1000,0,1000)
|
||||
}
|
||||
|
||||
/// Applies the current color to the given color id
|
||||
fn init_color(&self, color_id: ThemeColor) {
|
||||
ncurses::init_color(color_id, self.r, self.g, self.b);
|
||||
}
|
||||
|
||||
/// Read a string value into the current color.
|
||||
fn load_color(&mut self, s: &str) {
|
||||
|
||||
if s.len() == 0 {
|
||||
panic!("Cannot read color: empty string");
|
||||
}
|
||||
|
||||
if s.starts_with("#") {
|
||||
let s = &s[1..];
|
||||
// HTML-style
|
||||
let (l,max) = match s.len() {
|
||||
6 => (2,255),
|
||||
3 => (1,15),
|
||||
_ => panic!("Cannot parse color: {}", s),
|
||||
};
|
||||
|
||||
self.r = (load_hex(&s[0*l..1*l]) as i32 * 1000 / max) as i16;
|
||||
self.g = (load_hex(&s[1*l..2*l]) as i32 * 1000 / max) as i16;
|
||||
self.b = (load_hex(&s[2*l..3*l]) as i32 * 1000 / max) as i16;
|
||||
} else {
|
||||
// Unknown color. Panic.
|
||||
panic!("Cannot parse color: {}", s);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type ColorId = i16;
|
||||
|
||||
const BACKGROUND_COLOR: i16 = 8;
|
||||
const SHADOW_COLOR: i16 = 9;
|
||||
const VIEW_COLOR: i16 = 10;
|
||||
const PRIMARY_COLOR: i16 = 11;
|
||||
const SECONDARY_COLOR: i16 = 12;
|
||||
const TERTIARY_COLOR: i16 = 13;
|
||||
const TITLE_PRIMARY_COLOR: i16 = 14;
|
||||
const TITLE_SECONDARY__COLOR: i16 = 15;
|
||||
const HIGHLIGHT_COLOR: i16 = 16;
|
||||
const HIGHLIGHT_INACTIVE_COLOR: i16 = 17;
|
||||
|
||||
/// Defines colors for various situations.
|
||||
pub struct Theme {
|
||||
pub background: Color,
|
||||
pub shadow: Color,
|
||||
|
||||
pub view_background: Color,
|
||||
|
||||
pub primary: Color,
|
||||
pub secondary: Color,
|
||||
pub tertiary: Color,
|
||||
pub title_primary: Color,
|
||||
pub title_secondary: Color,
|
||||
pub highlight: Color,
|
||||
pub highlight_inactive: Color,
|
||||
}
|
||||
|
||||
impl Theme {
|
||||
/// Apply the theme. Effective immediately.
|
||||
pub fn apply(&self) {
|
||||
// First, init the colors
|
||||
self.background.init_color(BACKGROUND_COLOR);
|
||||
self.shadow.init_color(SHADOW_COLOR);
|
||||
self.view_background.init_color(VIEW_COLOR);
|
||||
self.primary.init_color(PRIMARY_COLOR);
|
||||
self.secondary.init_color(SECONDARY_COLOR);
|
||||
self.tertiary.init_color(TERTIARY_COLOR);
|
||||
self.title_primary.init_color(TITLE_PRIMARY_COLOR);
|
||||
self.title_secondary.init_color(TITLE_SECONDARY__COLOR);
|
||||
self.highlight.init_color(HIGHLIGHT_COLOR);
|
||||
self.highlight_inactive.init_color(HIGHLIGHT_INACTIVE_COLOR);
|
||||
|
||||
// Then init the pairs
|
||||
ncurses::init_pair(BACKGROUND, BACKGROUND_COLOR, BACKGROUND_COLOR);
|
||||
ncurses::init_pair(SHADOW, SHADOW_COLOR, SHADOW_COLOR);
|
||||
ncurses::init_pair(PRIMARY, PRIMARY_COLOR, VIEW_COLOR);
|
||||
ncurses::init_pair(SECONDARY, SECONDARY_COLOR, VIEW_COLOR);
|
||||
ncurses::init_pair(TERTIARY, TERTIARY_COLOR, VIEW_COLOR);
|
||||
ncurses::init_pair(TITLE_PRIMARY, TITLE_PRIMARY_COLOR, VIEW_COLOR);
|
||||
ncurses::init_pair(TITLE_SECONDARY, TITLE_SECONDARY__COLOR, VIEW_COLOR);
|
||||
ncurses::init_pair(HIGHLIGHT, VIEW_COLOR, HIGHLIGHT_COLOR);
|
||||
ncurses::init_pair(HIGHLIGHT_INACTIVE, VIEW_COLOR, HIGHLIGHT_INACTIVE_COLOR);
|
||||
}
|
||||
|
||||
/// Returns the default theme.
|
||||
pub fn default() -> Theme {
|
||||
Theme {
|
||||
background: Color::blue(),
|
||||
shadow: Color::black(),
|
||||
view_background: Color::gray(),
|
||||
primary: Color::black(),
|
||||
secondary: Color::white(),
|
||||
tertiary: Color::dark_gray(),
|
||||
title_primary: Color::red(),
|
||||
title_secondary: Color::yellow(),
|
||||
highlight: Color::red(),
|
||||
highlight_inactive: Color::blue(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Load a single value into a color
|
||||
fn load(color: &mut Color, value: Option<&toml::Value>) {
|
||||
match value {
|
||||
Some(&toml::Value::String(ref s)) => color.load_color(s),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads the color content from a TOML configuration
|
||||
fn load_colors(&mut self, table: toml::Table) {
|
||||
Theme::load(&mut self.background, table.get("background"));
|
||||
Theme::load(&mut self.shadow, table.get("shadow"));
|
||||
Theme::load(&mut self.view_background, table.get("view"));
|
||||
Theme::load(&mut self.primary, table.get("primary"));
|
||||
Theme::load(&mut self.secondary, table.get("secondary"));
|
||||
Theme::load(&mut self.tertiary, table.get("tertiary"));
|
||||
Theme::load(&mut self.title_primary, table.get("title_primaryy"));
|
||||
Theme::load(&mut self.title_secondary, table.get("title_secondary"));
|
||||
Theme::load(&mut self.highlight, table.get("highlight"));
|
||||
Theme::load(&mut self.highlight_inactive, table.get("highlight_primary"));
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads the default theme.
|
||||
pub fn load_default() {
|
||||
Theme::default().apply();
|
||||
}
|
||||
|
||||
/// Loads a simple default theme using built-in colors.
|
||||
pub fn load_legacy() {
|
||||
ncurses::init_pair(BACKGROUND, ncurses::COLOR_WHITE, ncurses::COLOR_BLUE);
|
||||
ncurses::init_pair(SHADOW, ncurses::COLOR_WHITE, ncurses::COLOR_BLACK);
|
||||
ncurses::init_pair(PRIMARY, ncurses::COLOR_BLACK, ncurses::COLOR_WHITE);
|
||||
ncurses::init_pair(SECONDARY, ncurses::COLOR_BLUE, ncurses::COLOR_WHITE);
|
||||
ncurses::init_pair(TERTIARY, ncurses::COLOR_WHITE, ncurses::COLOR_WHITE);
|
||||
ncurses::init_pair(TITLE_PRIMARY, ncurses::COLOR_RED, ncurses::COLOR_WHITE);
|
||||
ncurses::init_pair(TITLE_SECONDARY, ncurses::COLOR_YELLOW, ncurses::COLOR_WHITE);
|
||||
ncurses::init_pair(HIGHLIGHT, ncurses::COLOR_WHITE, ncurses::COLOR_RED);
|
||||
ncurses::init_pair(HIGHLIGHT_INACTIVE, ncurses::COLOR_WHITE, ncurses::COLOR_BLUE);
|
||||
}
|
||||
|
||||
/// Possible error returned when loading a theme.
|
||||
pub enum Error {
|
||||
/// An error occured when reading the file.
|
||||
IoError(io::Error),
|
||||
/// An error occured when parsing the toml content.
|
||||
ParseError,
|
||||
}
|
||||
|
||||
impl From<io::Error> for Error {
|
||||
fn from(err: io::Error) -> Self {
|
||||
Error::IoError(err)
|
||||
}
|
||||
}
|
||||
|
||||
/// Loads a theme file.
|
||||
///
|
||||
/// The file should be a toml file containing any of the following entries
|
||||
/// (missing entries will have default value):
|
||||
///
|
||||
/// - `background`
|
||||
/// - `shadow`
|
||||
/// - `view`
|
||||
/// - `primary`
|
||||
/// - `secondary`
|
||||
/// - `tertiary`
|
||||
/// - `title_primary`
|
||||
/// - `title_secondary`
|
||||
/// - `highlight`
|
||||
/// - `highlight_inactive`
|
||||
///
|
||||
/// Here is an example file:
|
||||
///
|
||||
/// ```text
|
||||
/// background = "#5555FF"
|
||||
/// shadow = "#000000"
|
||||
/// view = "#888888"
|
||||
///
|
||||
/// primary = "#111111"
|
||||
/// secondary = "#EEEEEE"
|
||||
/// tertiary = "#444444"
|
||||
///
|
||||
/// title_primary = "#ff5555"
|
||||
/// title_secondary = "#ffff55"
|
||||
///
|
||||
/// highlight = "#FF0000"
|
||||
/// highlight_inactive = "#5555FF"
|
||||
/// ```
|
||||
pub fn load_theme<P: AsRef<Path>>(filename: P) -> Result<(),Error> {
|
||||
let mut file = try!(File::open(filename));
|
||||
let mut content = String::new();
|
||||
|
||||
try!(file.read_to_string(&mut content));
|
||||
|
||||
let mut parser = toml::Parser::new(&content);
|
||||
let value = match parser.parse() {
|
||||
Some(value) => value,
|
||||
None => return Err(Error::ParseError),
|
||||
};
|
||||
|
||||
let mut theme = Theme::default();
|
||||
theme.load_colors(value);
|
||||
|
||||
theme.apply();
|
||||
|
||||
Ok(())
|
||||
}
|
31
src/lib.rs
31
src/lib.rs
@ -27,7 +27,7 @@ pub mod event;
|
||||
pub mod view;
|
||||
pub mod printer;
|
||||
pub mod vec;
|
||||
pub mod color;
|
||||
pub mod theme;
|
||||
pub mod align;
|
||||
|
||||
mod div;
|
||||
@ -36,6 +36,7 @@ mod utf8;
|
||||
use std::any::Any;
|
||||
use std::rc::Rc;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
|
||||
use vec::Vec2;
|
||||
use view::View;
|
||||
@ -62,6 +63,8 @@ pub struct Cursive {
|
||||
running: bool,
|
||||
|
||||
global_callbacks: HashMap<Event, Rc<Callback>>,
|
||||
|
||||
theme: theme::Theme,
|
||||
}
|
||||
|
||||
impl Cursive {
|
||||
@ -76,17 +79,17 @@ impl Cursive {
|
||||
ncurses::cbreak();
|
||||
ncurses::start_color();
|
||||
ncurses::curs_set(ncurses::CURSOR_VISIBILITY::CURSOR_INVISIBLE);
|
||||
color::load_legacy();
|
||||
// color::load_default();
|
||||
// color::load_theme("assets/style.toml").ok().unwrap();
|
||||
let theme = theme::load_default();
|
||||
// let theme = theme::load_theme("assets/style.toml").unwrap();
|
||||
|
||||
ncurses::wbkgd(ncurses::stdscr, ncurses::COLOR_PAIR(color::BACKGROUND));
|
||||
ncurses::wbkgd(ncurses::stdscr, ncurses::COLOR_PAIR(theme::ColorPair::Background.ncurses_id()));
|
||||
|
||||
let mut res = Cursive {
|
||||
screens: Vec::new(),
|
||||
active_screen: 0,
|
||||
running: true,
|
||||
global_callbacks: HashMap::new(),
|
||||
theme: theme,
|
||||
};
|
||||
|
||||
res.screens.push(StackView::new());
|
||||
@ -94,6 +97,22 @@ impl Cursive {
|
||||
res
|
||||
}
|
||||
|
||||
/// Returns the currently used theme
|
||||
pub fn current_theme(&self) -> &theme::Theme {
|
||||
&self.theme
|
||||
}
|
||||
|
||||
/// Loads a theme from the given file.
|
||||
///
|
||||
/// Returns TRUE if the theme was successfully loaded.
|
||||
pub fn load_theme<P: AsRef<Path>>(&mut self, filename: P) -> bool {
|
||||
match theme::load_theme(filename) {
|
||||
Err(err) => return false,
|
||||
Ok(theme) => self.theme = theme,
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
/// Regularly redraws everything, even when no input is given. Between 0 and 1000.
|
||||
///
|
||||
/// Call with fps=0 to disable (default value).
|
||||
@ -193,7 +212,7 @@ impl Cursive {
|
||||
}
|
||||
|
||||
fn draw(&mut self) {
|
||||
let printer = Printer::new(self.screen_size());
|
||||
let printer = Printer::new(self.screen_size(), self.theme.clone());
|
||||
self.screen_mut().draw(&printer);
|
||||
ncurses::refresh();
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ use std::cmp::min;
|
||||
|
||||
use ncurses;
|
||||
|
||||
use color;
|
||||
use theme::{ColorPair,Theme};
|
||||
use vec::{Vec2,ToVec2};
|
||||
|
||||
/// Convenient interface to draw on a subset of the screen.
|
||||
@ -15,15 +15,18 @@ pub struct Printer {
|
||||
pub size: Vec2,
|
||||
/// Whether the view to draw is currently focused or not.
|
||||
pub focused: bool,
|
||||
/// Currently used theme
|
||||
pub theme: Theme,
|
||||
}
|
||||
|
||||
impl Printer {
|
||||
/// Creates a new printer on the given window.
|
||||
pub fn new<T: ToVec2>(size: T) -> Self {
|
||||
pub fn new<T: ToVec2>(size: T, theme: Theme) -> Self {
|
||||
Printer {
|
||||
offset: Vec2::zero(),
|
||||
size: size.to_vec2(),
|
||||
focused: true,
|
||||
theme: theme,
|
||||
}
|
||||
}
|
||||
|
||||
@ -74,19 +77,17 @@ impl Printer {
|
||||
///
|
||||
/// ```
|
||||
/// # use cursive::printer::Printer;
|
||||
/// # use cursive::color;
|
||||
/// # let printer = Printer::new((6,4));
|
||||
/// printer.with_color(color::HIGHLIGHT, |printer| {
|
||||
/// # use cursive::theme;
|
||||
/// # let printer = Printer::new((6,4), theme::load_default());
|
||||
/// printer.with_color(theme::ColorPair::Highlight, |printer| {
|
||||
/// printer.print((0,0), "This text is highlighted!");
|
||||
/// });
|
||||
/// ```
|
||||
pub fn with_color<'a, F>(&'a self, c: color::ThemeColor, f: F)
|
||||
pub fn with_color<'a, F>(&'a self, c: ColorPair, f: F)
|
||||
where F: Fn(&Printer)
|
||||
{
|
||||
ncurses::attron(ncurses::COLOR_PAIR(c));
|
||||
f(self);
|
||||
ncurses::attroff(ncurses::COLOR_PAIR(c));
|
||||
ncurses::attron(ncurses::COLOR_PAIR(color::PRIMARY));
|
||||
self.with_style(ncurses::COLOR_PAIR(c.ncurses_id()), f);
|
||||
ncurses::attron(ncurses::COLOR_PAIR(ColorPair::Primary.ncurses_id()));
|
||||
}
|
||||
|
||||
/// Same as `with_color`, but apply a ncurses style instead,
|
||||
@ -107,7 +108,8 @@ impl Printer {
|
||||
///
|
||||
/// ```
|
||||
/// # use cursive::printer::Printer;
|
||||
/// # let printer = Printer::new((6,4));
|
||||
/// # use cursive::theme;
|
||||
/// # let printer = Printer::new((6,4), theme::load_default());
|
||||
/// printer.print_box((0,0), (6,4));
|
||||
/// ```
|
||||
pub fn print_box<T: ToVec2>(&self, start: T, size: T) {
|
||||
@ -133,6 +135,7 @@ impl Printer {
|
||||
// We can't be larger than what remains
|
||||
size: Vec2::min(self.size - offset_v, size.to_vec2()),
|
||||
focused: self.focused && focused,
|
||||
theme: self.theme.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
400
src/theme.rs
Normal file
400
src/theme.rs
Normal file
@ -0,0 +1,400 @@
|
||||
//! Module to handle colors and themes in the UI.
|
||||
|
||||
use std::io;
|
||||
use std::io::Read;
|
||||
use std::fs::File;
|
||||
use std::path::Path;
|
||||
|
||||
use ncurses;
|
||||
use toml;
|
||||
|
||||
/// Represents the color of a character and its background.
|
||||
#[derive(Clone,Copy)]
|
||||
pub enum ColorPair {
|
||||
/// Application background, where no view is present.
|
||||
Background,
|
||||
/// Color used by view shadows. Only background matters.
|
||||
Shadow,
|
||||
/// Main text with default background.
|
||||
Primary,
|
||||
/// Secondary text color, with default background.
|
||||
Secondary,
|
||||
/// Tertiary text color, with default background.
|
||||
Tertiary,
|
||||
/// Title text color with default background.
|
||||
TitlePrimary,
|
||||
/// Alternative color for a title.
|
||||
TitleSecondary,
|
||||
/// Alternate text with highlight background.
|
||||
Highlight,
|
||||
/// Highlight color for inactive views (not in focus).
|
||||
HighlightInactive,
|
||||
}
|
||||
|
||||
impl ColorPair {
|
||||
/// Returns the ncurses pair ID associated with this color pair.
|
||||
pub fn ncurses_id(self) -> i16 {
|
||||
match self {
|
||||
ColorPair::Background => 1,
|
||||
ColorPair::Shadow => 2,
|
||||
ColorPair::Primary => 3,
|
||||
ColorPair::Secondary => 4,
|
||||
ColorPair::Tertiary => 5,
|
||||
ColorPair::TitlePrimary => 6,
|
||||
ColorPair::TitleSecondary => 7,
|
||||
ColorPair::Highlight => 8,
|
||||
ColorPair::HighlightInactive => 9,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents the style a Cursive application will use.
|
||||
#[derive(Clone,Debug)]
|
||||
pub struct Theme {
|
||||
/// Wheter 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?
|
||||
pub colors: ColorStyle,
|
||||
}
|
||||
|
||||
impl Theme {
|
||||
fn default() -> Theme {
|
||||
Theme {
|
||||
shadow: true,
|
||||
borders: BorderStyle::Simple,
|
||||
colors: ColorStyle {
|
||||
background: Color::blue(),
|
||||
shadow: Color::black(),
|
||||
view: Color::white(),
|
||||
primary: Color::black(),
|
||||
secondary: Color::blue(),
|
||||
tertiary: Color::white(),
|
||||
title_primary: Color::red(),
|
||||
title_secondary: Color::yellow(),
|
||||
highlight: Color::red(),
|
||||
highlight_inactive: Color::blue(),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn load(&mut self, table: &toml::Table) {
|
||||
match table.get("shadow") {
|
||||
Some(&toml::Value::Boolean(shadow)) => self.shadow = shadow,
|
||||
_ => (),
|
||||
}
|
||||
|
||||
match table.get("borders") {
|
||||
Some(&toml::Value::String(ref borders)) => match BorderStyle::from(borders) {
|
||||
Some(borders) => self.borders = borders,
|
||||
None => (),
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
|
||||
match table.get("colors") {
|
||||
Some(&toml::Value::Table(ref table)) => self.colors.load(table),
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn apply(&self) {
|
||||
Theme::apply_color(ColorPair::Background, &self.colors.background, &self.colors.background);
|
||||
Theme::apply_color(ColorPair::Shadow, &self.colors.shadow, &self.colors.shadow);
|
||||
Theme::apply_color(ColorPair::Primary, &self.colors.primary, &self.colors.view);
|
||||
Theme::apply_color(ColorPair::Secondary, &self.colors.secondary, &self.colors.view);
|
||||
Theme::apply_color(ColorPair::Tertiary, &self.colors.tertiary, &self.colors.view);
|
||||
Theme::apply_color(ColorPair::TitlePrimary, &self.colors.title_primary, &self.colors.view);
|
||||
Theme::apply_color(ColorPair::TitleSecondary, &self.colors.title_secondary, &self.colors.view);
|
||||
Theme::apply_color(ColorPair::Highlight, &self.colors.view, &self.colors.highlight);
|
||||
Theme::apply_color(ColorPair::HighlightInactive, &self.colors.view, &self.colors.highlight_inactive);
|
||||
}
|
||||
|
||||
fn apply_color(pair: ColorPair, front: &Color, back: &Color) {
|
||||
ncurses::init_pair(pair.ncurses_id(), front.id, back.id);
|
||||
}
|
||||
}
|
||||
|
||||
/// Specifies how View borders should be drawn.
|
||||
#[derive(Clone,Copy,Debug)]
|
||||
pub enum BorderStyle {
|
||||
/// Don't draw any border.
|
||||
NoBorder,
|
||||
/// Simple borders.
|
||||
Simple,
|
||||
/// Outset borders with a 3d effect.
|
||||
Outset,
|
||||
}
|
||||
|
||||
impl BorderStyle {
|
||||
fn from(s: &str) -> Option<Self> {
|
||||
if s == "simple" {
|
||||
Some(BorderStyle::Simple)
|
||||
} else if s == "none" {
|
||||
Some(BorderStyle::NoBorder)
|
||||
} else if s == "outset" {
|
||||
Some(BorderStyle::Outset)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents the colors the application will use in various situations.
|
||||
#[derive(Clone,Debug)]
|
||||
pub struct ColorStyle {
|
||||
/// Color used for the application background.
|
||||
pub background: Color,
|
||||
/// Color used for View shadows.
|
||||
pub shadow: Color,
|
||||
/// Color used for View backgrounds.
|
||||
pub view: Color,
|
||||
/// Primary color used for the text.
|
||||
pub primary: Color,
|
||||
/// Secondary color used for the text.
|
||||
pub secondary: Color,
|
||||
/// Tertiary color used for the text.
|
||||
pub tertiary: Color,
|
||||
/// Primary color used for title text.
|
||||
pub title_primary: Color,
|
||||
/// Secondary color used for title text.
|
||||
pub title_secondary: Color,
|
||||
/// Color used for highlighting text.
|
||||
pub highlight: Color,
|
||||
/// Color used for highlighting inactive text.
|
||||
pub highlight_inactive: Color,
|
||||
}
|
||||
|
||||
impl ColorStyle {
|
||||
fn load(&mut self, table: &toml::Table) {
|
||||
let mut new_id = 8;
|
||||
|
||||
self.background.load(table, "background", &mut new_id);
|
||||
self.shadow.load(table, "shadow", &mut new_id);
|
||||
self.view.load(table, "view", &mut new_id);
|
||||
self.primary.load(table, "primary", &mut new_id);
|
||||
self.secondary.load(table, "secondary", &mut new_id);
|
||||
self.tertiary.load(table, "tertiary", &mut new_id);
|
||||
self.title_primary.load(table, "title_primary", &mut new_id);
|
||||
self.title_secondary.load(table, "title_secondary", &mut new_id);
|
||||
self.highlight.load(table, "highlight", &mut new_id);
|
||||
self.highlight_inactive.load(table, "highlight_inactive", &mut new_id);
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents a color used by the theme.
|
||||
#[derive(Clone,Debug)]
|
||||
pub struct Color {
|
||||
/// Color ID used by ncurses.
|
||||
pub id: i16,
|
||||
}
|
||||
|
||||
impl Color {
|
||||
/// Return the rgb values used by the color.
|
||||
pub fn rgb(&self) -> (i16,i16,i16) {
|
||||
let (mut r, mut g, mut b) = (0,0,0);
|
||||
|
||||
ncurses::color_content(self.id, &mut r, &mut g, &mut b);
|
||||
|
||||
(r,g,b)
|
||||
}
|
||||
}
|
||||
|
||||
/// Possible error returned when loading a theme.
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
/// An error occured when reading the file.
|
||||
IoError(io::Error),
|
||||
/// An error occured when parsing the toml content.
|
||||
ParseError,
|
||||
}
|
||||
|
||||
impl From<io::Error> for Error {
|
||||
fn from(err: io::Error) -> Self {
|
||||
Error::IoError(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl Color {
|
||||
fn parse(value: &str, new_id: &mut i16) -> Option<Self> {
|
||||
let color = if value == "black" {
|
||||
Color::black()
|
||||
} else if value == "red" {
|
||||
Color::red()
|
||||
} else if value == "green" {
|
||||
Color::green()
|
||||
} else if value == "yellow" {
|
||||
Color::yellow()
|
||||
} else if value == "blue" {
|
||||
Color::blue()
|
||||
} else if value == "magenta" {
|
||||
Color::magenta()
|
||||
} else if value == "cyan" {
|
||||
Color::cyan()
|
||||
} else if value == "white" {
|
||||
Color::white()
|
||||
} else {
|
||||
// Let's make a new color
|
||||
return Color::make_new(value, new_id);
|
||||
};
|
||||
|
||||
Some(color)
|
||||
}
|
||||
|
||||
fn load(&mut self, table: &toml::Table, key: &str, new_id: &mut i16) {
|
||||
match table.get(key) {
|
||||
Some(&toml::Value::String(ref value)) => { self.load_value(value, new_id); },
|
||||
Some(&toml::Value::Array(ref array)) => for color in array.iter() {
|
||||
match color {
|
||||
&toml::Value::String(ref color) => if self.load_value(color, new_id) { return; },
|
||||
_ => (),
|
||||
}
|
||||
},
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
|
||||
fn load_value(&mut self, value: &str, new_id: &mut i16) -> bool {
|
||||
match Color::parse(value, new_id) {
|
||||
Some(color) => self.id = color.id,
|
||||
None => return false,
|
||||
}
|
||||
true
|
||||
}
|
||||
|
||||
fn make_new(value: &str, new_id: &mut i16) -> Option<Self> {
|
||||
// if !ncurses::can_change_color() {
|
||||
if !ncurses::has_colors() {
|
||||
return None;
|
||||
}
|
||||
|
||||
if !value.starts_with("#") {
|
||||
return None;
|
||||
}
|
||||
|
||||
let s = &value[1..];
|
||||
let (l,max) = match s.len() {
|
||||
6 => (2,255),
|
||||
3 => (1,15),
|
||||
_ => panic!("Cannot parse color: {}", s),
|
||||
};
|
||||
|
||||
let r = (load_hex(&s[0*l..1*l]) as i32 * 1000 / max) as i16;
|
||||
let g = (load_hex(&s[1*l..2*l]) as i32 * 1000 / max) as i16;
|
||||
let b = (load_hex(&s[2*l..3*l]) as i32 * 1000 / max) as i16;
|
||||
|
||||
ncurses::init_color(*new_id, r, g, b);
|
||||
|
||||
let color = Color { id: *new_id };
|
||||
*new_id += 1;
|
||||
|
||||
Some(color)
|
||||
}
|
||||
|
||||
|
||||
pub fn black() -> Self {
|
||||
Color { id: 0 }
|
||||
}
|
||||
pub fn red() -> Self {
|
||||
Color { id: 1 }
|
||||
}
|
||||
pub fn green() -> Self {
|
||||
Color { id: 2 }
|
||||
}
|
||||
pub fn yellow() -> Self {
|
||||
Color { id: 3 }
|
||||
}
|
||||
pub fn blue() -> Self {
|
||||
Color { id: 4 }
|
||||
}
|
||||
pub fn magenta() -> Self {
|
||||
Color { id: 5 }
|
||||
}
|
||||
pub fn cyan() -> Self {
|
||||
Color { id: 6 }
|
||||
}
|
||||
pub fn white() -> Self {
|
||||
Color { id: 7 }
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Loads the default theme, and returns its representation.
|
||||
pub fn load_default() -> Theme {
|
||||
let theme = Theme::default();
|
||||
theme.apply();
|
||||
theme
|
||||
}
|
||||
|
||||
/// Loads a theme file, and returns its representation if everything worked well.
|
||||
///
|
||||
/// The file should be a toml file. All fields are optional. Here is are the possible entries:
|
||||
///
|
||||
/// ```text
|
||||
/// # Every field in a theme file is optional.
|
||||
///
|
||||
/// shadow = false
|
||||
/// borders = "simple", # Alternatives are "none" and "outset"
|
||||
///
|
||||
/// # Base colors are red, green, blue,
|
||||
/// # cyan, magenta, yellow, white and black.
|
||||
/// [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"
|
||||
/// ```
|
||||
|
||||
pub fn load_theme<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
|
||||
};
|
||||
|
||||
let mut parser = toml::Parser::new(&content);
|
||||
let table = match parser.parse() {
|
||||
Some(value) => value,
|
||||
None => return Err(Error::ParseError),
|
||||
};
|
||||
|
||||
let mut theme = Theme::default();
|
||||
theme.load(&table);
|
||||
theme.apply();
|
||||
|
||||
Ok(theme)
|
||||
}
|
||||
|
||||
/// Loads a hexadecimal code
|
||||
fn load_hex(s: &str) -> i16 {
|
||||
let mut sum = 0;
|
||||
for c in s.chars() {
|
||||
sum *= 16;
|
||||
sum += match c {
|
||||
n @ '0' ... '9' => n as i16 - '0' as i16,
|
||||
n @ 'a' ... 'f' => n as i16 - 'a' as i16 + 10,
|
||||
n @ 'A' ... 'F' => n as i16 - 'A' as i16 + 10,
|
||||
_ => 0,
|
||||
};
|
||||
}
|
||||
|
||||
sum
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
use std::rc::Rc;
|
||||
|
||||
use color;
|
||||
use theme::ColorPair;
|
||||
use ::Cursive;
|
||||
use vec::Vec2;
|
||||
use view::{View,SizeRequest};
|
||||
@ -29,7 +29,7 @@ impl Button {
|
||||
impl View for Button {
|
||||
|
||||
fn draw(&mut self, printer: &Printer) {
|
||||
let style = if !printer.focused { color::PRIMARY } else { color::HIGHLIGHT };
|
||||
let style = if !printer.focused { ColorPair::Primary } else { ColorPair::Highlight };
|
||||
let x = printer.size.x - 1;
|
||||
|
||||
printer.with_color(style, |printer| {
|
||||
|
@ -1,10 +1,10 @@
|
||||
use std::cmp::max;
|
||||
use std::any::Any;
|
||||
|
||||
use color;
|
||||
use ::{Cursive};
|
||||
use align::*;
|
||||
use event::*;
|
||||
use theme::ColorPair;
|
||||
use view::{View,SizeRequest,DimensionRequest,Selector};
|
||||
use view::{Button,SizedView};
|
||||
use vec::{Vec2,Vec4,ToVec4};
|
||||
@ -139,7 +139,7 @@ impl View for Dialog {
|
||||
printer.print((x-2,0), "┤ ");
|
||||
printer.print((x+len,0), " ├");
|
||||
|
||||
printer.with_color(color::TITLE_PRIMARY, |p| p.print((x,0), &self.title));
|
||||
printer.with_color(ColorPair::TitlePrimary, |p| p.print((x,0), &self.title));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
use ncurses;
|
||||
|
||||
use color;
|
||||
use theme::ColorPair;
|
||||
use vec::Vec2;
|
||||
use view::{View,SizeRequest};
|
||||
use event::*;
|
||||
@ -60,7 +60,7 @@ impl View for EditView {
|
||||
fn draw(&mut self, printer: &Printer) {
|
||||
// let style = if focused { color::HIGHLIGHT } else { color::HIGHLIGHT_INACTIVE };
|
||||
let len = self.content.chars().count();
|
||||
printer.with_color(color::SECONDARY, |printer| {
|
||||
printer.with_color(ColorPair::Secondary, |printer| {
|
||||
printer.with_style(ncurses::A_REVERSE(), |printer| {
|
||||
printer.print((0,0), &self.content);
|
||||
printer.print_hline((len,0), printer.size.x-len, '_' as u64);
|
||||
|
@ -1,6 +1,6 @@
|
||||
use std::cmp::{min,max};
|
||||
|
||||
use color;
|
||||
use theme::ColorPair;
|
||||
use vec::Vec2;
|
||||
use printer::Printer;
|
||||
|
||||
@ -88,8 +88,9 @@ impl ScrollBase {
|
||||
/// ```
|
||||
/// # use cursive::view::ScrollBase;
|
||||
/// # use cursive::printer::Printer;
|
||||
/// # use cursive::theme;
|
||||
/// # let scrollbase = ScrollBase::new();
|
||||
/// # let printer = Printer::new((5,1));
|
||||
/// # let printer = Printer::new((5,1), theme::load_default());
|
||||
/// # let printer = &printer;
|
||||
/// let lines = ["Line 1", "Line number 2"];
|
||||
/// scrollbase.draw(printer, |printer, i| {
|
||||
@ -121,7 +122,7 @@ impl ScrollBase {
|
||||
// Now
|
||||
let start = steps * self.start_line / (1 + self.content_height - self.view_height);
|
||||
|
||||
let color = if printer.focused { color::HIGHLIGHT } else { color::HIGHLIGHT_INACTIVE };
|
||||
let color = if printer.focused { ColorPair::Highlight } else { ColorPair::HighlightInactive };
|
||||
|
||||
printer.print_vline((printer.size.x-1,0), printer.size.y, '|' as u64);
|
||||
printer.with_color(color, |printer| {
|
||||
|
@ -1,7 +1,7 @@
|
||||
use std::cmp::min;
|
||||
use std::rc::Rc;
|
||||
|
||||
use color;
|
||||
use theme::ColorPair;
|
||||
use ::Cursive;
|
||||
use align::*;
|
||||
use view::{View,IdView,SizeRequest,DimensionRequest};
|
||||
@ -123,10 +123,10 @@ impl <T: 'static> View for SelectView<T> {
|
||||
|
||||
self.scrollbase.draw(printer, |printer,i| {
|
||||
let style = if i == self.focus {
|
||||
if printer.focused { color::HIGHLIGHT }
|
||||
else { color::HIGHLIGHT_INACTIVE }
|
||||
if printer.focused { ColorPair::Highlight }
|
||||
else { ColorPair::HighlightInactive }
|
||||
} else {
|
||||
color::PRIMARY
|
||||
ColorPair::Primary
|
||||
};
|
||||
printer.with_color(style, |printer| {
|
||||
let l = self.items[i].label.chars().count();
|
||||
|
@ -1,7 +1,7 @@
|
||||
use view::{View,ViewWrapper,SizeRequest};
|
||||
use printer::Printer;
|
||||
use vec::Vec2;
|
||||
use color;
|
||||
use theme::ColorPair;
|
||||
|
||||
/// Wrapper view that adds a shadow.
|
||||
///
|
||||
@ -33,7 +33,7 @@ impl <T: View> ViewWrapper for ShadowView<T> {
|
||||
|
||||
fn wrap_draw(&mut self, printer: &Printer) {
|
||||
|
||||
printer.with_color(color::PRIMARY, |printer| {
|
||||
printer.with_color(ColorPair::Primary, |printer| {
|
||||
// Draw the view background
|
||||
for y in 1..printer.size.y-1 {
|
||||
printer.print_hline((1,y), printer.size.x-2, ' ' as u64);
|
||||
@ -46,7 +46,7 @@ impl <T: View> ViewWrapper for ShadowView<T> {
|
||||
let h = printer.size.y-1;
|
||||
let w = printer.size.x-1;
|
||||
|
||||
printer.with_color(color::SHADOW, |printer| {
|
||||
printer.with_color(ColorPair::Shadow, |printer| {
|
||||
printer.print_hline((2,h), w-1, ' ' as u64);
|
||||
printer.print_vline((w,2), h-1, ' ' as u64);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user