Radish alpha
r
Radicle terminal user interface
Radicle
Git (anonymous pull)
Log in to clone via SSH
bin: Add support for application settings
Erik Kundt committed 1 year ago
commit 0e3c9ab2c1f2b72c9b5a73f9359f7e5cba30642c
parent eb00c21e8b09b90f17a133c969fd5cdbf6cd8dc6
1 file changed +74 -0
added bin/settings.rs
@@ -0,0 +1,74 @@
+
use std::collections::HashMap;
+

+
use radicle_tui as tui;
+
use tui::ui::theme::Theme;
+

+
static THEME_RADICLE: &str = "Radicle";
+

+
pub type ThemeBundleId = String;
+

+
/// `ThemeMode` defines which theme is selected from a `ThemeBundle`. It can
+
/// be either `light``, `dark`` or `auto``, which sets the mode depending on
+
/// the terminal background luma.
+
#[allow(dead_code)]
+
#[derive(Debug, PartialEq, Eq, Hash)]
+
pub enum ThemeMode {
+
    Auto,
+
    Light,
+
    Dark,
+
}
+

+
/// A `ThemeBundle` defines a tuple of themes, that should be adapted to light or
+
/// dark terminal colors.
+
#[derive(Debug)]
+
pub struct ThemeBundle {
+
    pub light: Theme,
+
    pub dark: Theme,
+
}
+

+
impl Default for ThemeBundle {
+
    fn default() -> Self {
+
        Self {
+
            light: Theme::default_light(),
+
            dark: Theme::default_dark(),
+
        }
+
    }
+
}
+

+
#[derive(Debug)]
+
pub struct ThemeSettings {
+
    /// Set light or dark mode, or detect terminal background luma and
+
    /// switch automatically.
+
    mode: ThemeMode,
+
    /// Theme bundle identifier.
+
    active_bundle: ThemeBundleId,
+
    /// All theme bundles.
+
    bundles: HashMap<ThemeBundleId, ThemeBundle>,
+
}
+

+
impl ThemeSettings {
+
    pub fn mode(&self) -> &ThemeMode {
+
        &self.mode
+
    }
+

+
    pub fn active_bundle(&self) -> Option<&ThemeBundle> {
+
        self.bundles.get(&self.active_bundle)
+
    }
+
}
+

+
#[derive(Debug)]
+
pub struct Settings {
+
    pub theme: ThemeSettings,
+
}
+

+
impl Default for Settings {
+
    fn default() -> Self {
+
        Self {
+
            theme: ThemeSettings {
+
                mode: ThemeMode::Auto,
+
                active_bundle: THEME_RADICLE.into(),
+
                bundles: HashMap::from([(THEME_RADICLE.to_string(), ThemeBundle::default())]),
+
            },
+
        }
+
    }
+
}