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/// Window title bar. `Auto` hides it under tiling window managers and asks
19/// the desktop for its own everywhere else.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
21#[serde(rename_all = "kebab-case")]
22pub enum TitleBar {
23    #[default]
24    Auto,
25    System,
26    None,
27}
28
29impl TitleBar {
30    pub const ALL: [TitleBar; 3] = [TitleBar::Auto, TitleBar::System, TitleBar::None];
31
32    pub fn label(self) -> &'static str {
33        match self {
34            TitleBar::Auto => "Auto",
35            TitleBar::System => "System",
36            TitleBar::None => "None",
37        }
38    }
39
40    /// Whether a title bar is wanted, resolving `Auto` from the environment.
41    pub fn wanted(self) -> bool {
42        match self {
43            TitleBar::System => true,
44            TitleBar::None => false,
45            TitleBar::Auto => !tiling_wm(),
46        }
47    }
48}
49
50/// Tiling window managers place and size windows themselves; a title bar
51/// there is only clutter.
52fn tiling_wm() -> bool {
53    [
54        "HYPRLAND_INSTANCE_SIGNATURE",
55        "SWAYSOCK",
56        "I3SOCK",
57        "NIRI_SOCKET",
58    ]
59    .iter()
60    .any(|var| std::env::var_os(var).is_some_and(|v| !v.is_empty()))
61}
62
63/// User configuration, read from `$XDG_CONFIG_HOME/termden/config.toml`.
64///
65/// Every field has a default so the file can stay tiny.
66#[derive(Debug, Clone, Serialize, Deserialize)]
67#[serde(default)]
68pub struct Config {
69    pub theme: ThemeId,
70    pub ui_font: String,
71    pub mono_font: String,
72    /// Terminal text size in px (ctrl+shift +/- adjusts and saves it).
73    pub font_size: f32,
74    pub panel_width: f32,
75    pub gap: f32,
76    pub corner_radius: f32,
77    pub title_bar: TitleBar,
78}
79
80impl Default for Config {
81    fn default() -> Self {
82        Self {
83            theme: ThemeId::default(),
84            ui_font: "Noto Sans".to_string(),
85            mono_font: "JetBrainsMono Nerd Font".to_string(),
86            font_size: 16.0,
87            panel_width: 268.0,
88            gap: 12.0,
89            corner_radius: 12.0,
90            title_bar: TitleBar::Auto,
91        }
92    }
93}
94
95impl Config {
96    pub fn path() -> PathBuf {
97        config_dir().join("termden").join("config.toml")
98    }
99
100    pub fn load() -> Self {
101        let path = Self::path();
102        let Ok(text) = fs::read_to_string(&path) else {
103            return Self::default();
104        };
105
106        match toml::from_str(&text) {
107            Ok(config) => config,
108            Err(error) => {
109                eprintln!("termden: could not parse {}: {error}", path.display());
110                Self::default()
111            }
112        }
113    }
114
115    pub fn save(&self) -> std::io::Result<()> {
116        let path = Self::path();
117        if let Some(parent) = path.parent() {
118            fs::create_dir_all(parent)?;
119        }
120        let text = toml::to_string_pretty(self).map_err(std::io::Error::other)?;
121        fs::write(path, text)
122    }
123}
124
125fn config_dir() -> PathBuf {
126    crate::state::xdg_dir("XDG_CONFIG_HOME", ".config")
127}