#[deprecated]
pub use crate::style::{
BaseColor, BorderStyle, Color, ColorPair, ColorStyle, ColorType, ConcreteEffects,
ConcreteStyle, Effect, EffectStatus, Effects, NoSuchColor, Palette, PaletteColor, PaletteNode,
PaletteStyle, Style, StyleType,
};
#[cfg(feature = "toml")]
use std::fs::File;
use std::io;
#[cfg(feature = "toml")]
use std::io::Read;
#[cfg(feature = "toml")]
use std::path::Path;
#[derive(Clone, Debug)]
pub struct Theme {
pub shadow: bool,
pub borders: BorderStyle,
pub palette: Palette,
}
impl Default for Theme {
fn default() -> Self {
Theme::retro()
}
}
impl Theme {
pub fn terminal_default() -> Self {
Theme {
shadow: false,
borders: BorderStyle::Simple,
palette: Palette::terminal_default(),
}
}
pub fn retro() -> Self {
Theme {
shadow: true,
borders: BorderStyle::Simple,
palette: Palette::retro(),
}
}
#[cfg(feature = "toml")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "toml")))]
pub fn load_toml(&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(borders)) = table.get("borders") {
self.borders = BorderStyle::from(borders);
}
if let Some(toml::Value::Table(table)) = table.get("colors") {
self.palette.load_toml(table);
}
if let Some(toml::Value::Table(table)) = table.get("styles") {
self.palette.load_toml_styles(table);
}
}
}
#[derive(Debug)]
pub enum Error {
Io(io::Error),
#[cfg(feature = "toml")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "toml")))]
Parse(toml::de::Error),
}
#[cfg(feature = "toml")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "toml")))]
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::Io(err)
}
}
#[cfg(feature = "toml")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "toml")))]
impl From<toml::de::Error> for Error {
fn from(err: toml::de::Error) -> Self {
Error::Parse(err)
}
}
#[cfg(feature = "toml")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "toml")))]
pub fn load_theme_file<P: AsRef<Path>>(filename: P) -> Result<Theme, Error> {
let content = {
let mut content = String::new();
let mut file = File::open(filename)?;
file.read_to_string(&mut content)?;
content
};
load_toml(&content)
}
#[cfg(feature = "toml")]
#[cfg_attr(feature = "doc-cfg", doc(cfg(feature = "toml")))]
pub fn load_toml(content: &str) -> Result<Theme, Error> {
let table = toml::de::from_str(content)?;
let mut theme = Theme::default();
theme.load_toml(&table);
Ok(theme)
}
pub fn load_default() -> Theme {
Theme::default()
}