use crate::styles::{BADGE_INTRO, BOLD, CYAN, DIM, ERR, OK, WHITE, WHITE_BOLD, YELLOW};
use anstyle::Style;
use std::sync::RwLock;
#[derive(Clone, Copy)]
pub struct Colors {
pub accent: Style,
pub active: Style,
pub input: Style,
pub success: Style,
pub error: Style,
pub cancel: Style,
pub dim: Style,
pub header: Style,
pub header_plain: Style,
pub title: Style,
pub intro_badge: Style,
}
impl Default for Colors {
fn default() -> Self {
Self {
accent: CYAN,
active: CYAN,
input: WHITE,
success: OK,
error: YELLOW,
cancel: ERR,
dim: DIM,
header: WHITE_BOLD,
header_plain: WHITE,
title: BOLD,
intro_badge: BADGE_INTRO,
}
}
}
pub fn colors() -> Colors {
get().colors
}
pub fn update_colors<F: FnOnce(&mut Colors)>(f: F) {
update(|s| f(&mut s.colors))
}
pub fn set_colors(c: Colors) {
update(|s| s.colors = c);
}
#[derive(Clone)]
pub struct Settings {
pub cancel: String,
pub error: String,
pub with_guide: bool,
pub vim_keys: bool,
pub show_hints: bool,
pub bold_header: bool,
pub colors: Colors,
}
impl Default for Settings {
fn default() -> Self {
Self {
cancel: "Operation cancelled.".into(),
error: "Something went wrong.".into(),
with_guide: true,
vim_keys: true,
show_hints: false,
bold_header: true,
colors: Colors::default(),
}
}
}
static SETTINGS: RwLock<Option<Settings>> = RwLock::new(None);
pub fn get() -> Settings {
SETTINGS.read().unwrap().clone().unwrap_or_default()
}
pub fn update<F: FnOnce(&mut Settings)>(f: F) {
let mut guard = SETTINGS.write().unwrap();
let mut s = guard.clone().unwrap_or_default();
f(&mut s);
*guard = Some(s);
}