#![allow(unused)]
use std::collections::BTreeMap;
use iocore::Path;
use serde::{Deserialize, Serialize};
use crate::{parse_tokens, Definition, Error, Result, Rule};
#[derive(
Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Deserialize, Serialize, Debug,
)]
pub struct Config {
pub ps1: String,
pub vcs: Option<VcsConfig>,
pub variants: BTreeMap<String, String>,
pub enable_colors: Option<bool>,
}
impl Config {
pub fn ps1(&self) -> String {
let ps1 = self.ps1.clone();
ps1
}
pub fn render_ps1(&self) -> Result<String> {
let tokens = parse_tokens(&self.ps1())?;
let ps1 = tokens
.iter()
.map(|token| token.to_string())
.collect::<String>();
Ok(ps1)
}
pub fn new(ps1: &str) -> Config {
Config::with_vcs(ps1, VcsConfig::default())
}
pub fn with_vcs(ps1: &str, vcs: VcsConfig) -> Config {
Config::with_options(
ps1,
vcs,
console::Term::stdout()
.features()
.colors_supported(),
Vec::new(),
)
}
pub fn with_options(
ps1: &str,
vcs: VcsConfig,
enable_colors: bool,
variants: Vec<(String, String)>,
) -> Config {
Config {
ps1: ps1.to_string(),
vcs: Some(vcs),
variants: BTreeMap::from_iter(variants.iter().map(Clone::clone)),
enable_colors: Some(enable_colors),
}
}
}
impl Default for Config {
fn default() -> Config {
Config {
ps1: r"{220}\u{231}:{220}\W{37}{git:branch}{220}\${reset}".to_string(),
enable_colors: None,
vcs: None,
variants: Default::default(),
}
}
}
#[derive(
Clone, PartialOrd, Ord, PartialEq, Eq, Hash, Deserialize, Serialize, Debug, Default,
)]
pub struct VcsConfig {
pub git: Option<String>,
pub mercurial: Option<String>,
}
impl Config {
pub fn load(path: &Path) -> Result<Config> {
Ok(Config::from_toml_string(&path.read()?)?)
}
pub fn from_toml_string(config: &str) -> Result<Config> {
Ok(toml::from_str::<Config>(config)?)
}
}