Skip to main content

termden_core/
config.rs

1use std::fs;
2use std::path::PathBuf;
3
4use serde::{Deserialize, Serialize};
5
6/// Which theme to use, by id (e.g. `catppuccin-mocha`, `nord`). The app owns
7/// the list; an unknown id falls back to the default theme.
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9#[serde(transparent)]
10pub struct ThemeId(pub String);
11
12impl Default for ThemeId {
13    fn default() -> Self {
14        Self("catppuccin-mocha".into())
15    }
16}
17
18/// `Pinned` keeps the sidebar next to the tiles; `AutoHide` hides it until the
19/// pointer touches the left screen edge.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
21#[serde(rename_all = "kebab-case")]
22pub enum Sidebar {
23    #[default]
24    Pinned,
25    AutoHide,
26}
27
28impl Sidebar {
29    pub const ALL: [Sidebar; 2] = [Sidebar::Pinned, Sidebar::AutoHide];
30
31    pub fn label(self) -> &'static str {
32        match self {
33            Sidebar::Pinned => "Pinned",
34            Sidebar::AutoHide => "Auto-hide",
35        }
36    }
37}
38
39/// Window title bar. `Auto` hides it under tiling window managers and asks
40/// the desktop for its own everywhere else.
41#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
42#[serde(rename_all = "kebab-case")]
43pub enum TitleBar {
44    #[default]
45    Auto,
46    System,
47    None,
48}
49
50impl TitleBar {
51    pub const ALL: [TitleBar; 3] = [TitleBar::Auto, TitleBar::System, TitleBar::None];
52
53    pub fn label(self) -> &'static str {
54        match self {
55            TitleBar::Auto => "Auto",
56            TitleBar::System => "System",
57            TitleBar::None => "None",
58        }
59    }
60
61    /// Whether a title bar is wanted, resolving `Auto` from the environment.
62    pub fn wanted(self) -> bool {
63        match self {
64            TitleBar::System => true,
65            TitleBar::None => false,
66            TitleBar::Auto => !tiling_wm(),
67        }
68    }
69}
70
71/// Tiling window managers place and size windows themselves; a title bar
72/// there is only clutter.
73fn tiling_wm() -> bool {
74    [
75        "HYPRLAND_INSTANCE_SIGNATURE",
76        "SWAYSOCK",
77        "I3SOCK",
78        "NIRI_SOCKET",
79    ]
80    .iter()
81    .any(|var| std::env::var_os(var).is_some_and(|v| !v.is_empty()))
82}
83
84/// User configuration, read from `$XDG_CONFIG_HOME/termden/config.toml`.
85///
86/// Every field has a default so the file can stay tiny.
87#[derive(Debug, Clone, Serialize, Deserialize)]
88#[serde(default)]
89pub struct Config {
90    pub theme: ThemeId,
91    pub ui_font: String,
92    pub mono_font: String,
93    /// Terminal text size in px (ctrl+shift +/- adjusts and saves it).
94    pub font_size: f32,
95    pub panel_width: f32,
96    pub gap: f32,
97    pub corner_radius: f32,
98    pub title_bar: TitleBar,
99    pub sidebar: Sidebar,
100}
101
102impl Default for Config {
103    fn default() -> Self {
104        Self {
105            theme: ThemeId::default(),
106            ui_font: "Noto Sans".to_string(),
107            mono_font: "JetBrainsMono Nerd Font".to_string(),
108            font_size: 16.0,
109            panel_width: 268.0,
110            gap: 12.0,
111            corner_radius: 12.0,
112            title_bar: TitleBar::Auto,
113            sidebar: Sidebar::Pinned,
114        }
115    }
116}
117
118impl Config {
119    pub fn path() -> PathBuf {
120        config_dir().join("termden").join("config.toml")
121    }
122
123    pub fn load() -> Self {
124        let path = Self::path();
125        let Ok(text) = fs::read_to_string(&path) else {
126            return Self::default();
127        };
128
129        match toml::from_str(&text) {
130            Ok(config) => config,
131            Err(error) => {
132                eprintln!("termden: could not parse {}: {error}", path.display());
133                Self::default()
134            }
135        }
136    }
137
138    pub fn save(&self) -> std::io::Result<()> {
139        let path = Self::path();
140        if let Some(parent) = path.parent() {
141            fs::create_dir_all(parent)?;
142        }
143        let text = toml::to_string_pretty(self).map_err(std::io::Error::other)?;
144        fs::write(path, text)
145    }
146}
147
148fn config_dir() -> PathBuf {
149    crate::state::xdg_dir("XDG_CONFIG_HOME", ".config")
150}