Skip to main content

leenfetch_core/config/
settings.rs

1use serde::Deserialize;
2
3/// Describes a single entry in the `modules` array.
4/// Entries are either literal strings (used for `break`) or objects describing a module.
5#[derive(Debug, Deserialize, Clone)]
6#[serde(untagged)]
7pub enum LayoutItem {
8    /// Matches the literal string "break" (case insensitive) to insert a blank line.
9    Break(String),
10    /// A normal module configuration entry.
11    Module(ModuleEntry),
12}
13
14/// Fine-grained configuration for how each block is displayed.
15/// Most fields correspond to a feature or formatting option.
16#[derive(Debug, Deserialize, Clone)]
17pub struct Flags {
18    #[serde(default)]
19    pub ascii_distro: String, // Which distro's ASCII art to use ("auto" or a specific name)
20    #[serde(default)]
21    pub ascii_colors: String, // Which color palette to use ("distro", or a comma-separated list of color indices)
22    #[serde(default)]
23    pub battery_display: String, // Battery display mode ("off", "bar", "infobar", "barinfo")
24    #[serde(default)]
25    pub color_blocks: String, // String to use for color blocks (e.g., "███")
26    #[serde(default)]
27    pub cpu_brand: bool, // Show CPU brand prefix (true/false)
28    #[serde(default)]
29    pub cpu_cores: bool, // Show CPU core count (true/false)
30    #[serde(default)]
31    pub cpu_frequency: bool, // Show CPU frequency (true/false)
32    #[serde(default)]
33    pub cpu_speed: bool, // Show CPU speed (true/false)
34    #[serde(default)]
35    pub cpu_temp: char, // Temperature unit ('C', 'F', or 'off')
36    #[serde(default)]
37    pub cpu_show_temp: bool, // Show CPU temperature (true/false)
38    #[serde(default)]
39    pub custom_ascii_path: String, // Path to custom ASCII art file
40    #[serde(default)]
41    pub de_version: bool, // Show DE version (true/false)
42    #[serde(default)]
43    pub distro_display: String, // Distro display format (see DistroDisplay enum)
44    #[serde(default)]
45    pub disk_display: String, // Disk display format (see DiskDisplay enum)
46    #[serde(default)]
47    pub disk_subtitle: String, // Disk subtitle format (see DiskSubtitle enum)
48    #[serde(default)]
49    pub memory_percent: bool, // Show memory usage as percentage (true/false)
50    #[serde(default)]
51    pub memory_unit: String, // Memory unit ("kib", "mib", "gib")
52    #[serde(default)]
53    pub package_managers: String, // Package manager display (see PackageShorthand enum)
54    #[serde(default)]
55    pub shell_path: bool, // Show full shell path (true/false)
56    #[serde(default)]
57    pub shell_version: bool, // Show shell version (true/false)
58    #[serde(default)]
59    pub uptime_shorthand: String, // Uptime display format (see UptimeShorthand enum)
60    #[serde(default)]
61    pub os_age_shorthand: String, // Uptime display format (see OsAgeShorthand enum)
62}
63
64/// Root configuration structure that mirrors the JSONC config file.
65/// Missing sections fall back to their type defaults so that
66/// users can keep only the keys they care about.
67#[derive(Debug, Deserialize, Clone)]
68pub struct Config {
69    #[serde(default, rename = "$schema")]
70    #[allow(dead_code)]
71    pub schema: Option<String>,
72    #[serde(default)]
73    #[allow(dead_code)]
74    pub logo: Option<Logo>,
75    #[serde(default)]
76    pub flags: Flags,
77    #[serde(default, alias = "layout", alias = "modules")]
78    pub layout: Vec<LayoutItem>,
79}
80
81/// Minimal representation of the logo block. Extra fields are ignored automatically.
82#[derive(Debug, Deserialize, Clone, Default)]
83pub struct Logo {
84    #[serde(rename = "type", default)]
85    #[allow(dead_code)]
86    pub logo_type: Option<String>,
87    #[serde(default)]
88    #[allow(dead_code)]
89    pub source: Option<String>,
90}
91
92/// Configuration for an individual info module.
93#[derive(Debug, Deserialize, Clone, Default)]
94pub struct ModuleEntry {
95    #[serde(rename = "type", default)]
96    pub module_type: Option<String>,
97    #[serde(default)]
98    pub key: Option<String>,
99    #[serde(default)]
100    pub label: Option<String>,
101    #[serde(default)]
102    pub field: Option<String>,
103    #[serde(default)]
104    pub format: Option<String>,
105    #[serde(default)]
106    pub text: Option<String>,
107}
108
109impl ModuleEntry {
110    /// Returns the resolved field identifier used for data lookups.
111    pub fn field_name(&self) -> Option<&str> {
112        self.module_type
113            .as_deref()
114            .or(self.field.as_deref())
115            .map(str::trim)
116            .filter(|value| !value.is_empty())
117    }
118
119    /// Returns the label to render for this module, if any.
120    pub fn label(&self) -> Option<&str> {
121        self.key.as_deref().or(self.label.as_deref())
122    }
123
124    /// Returns true when the module is a custom text block.
125    pub fn is_custom(&self) -> bool {
126        self.field_name()
127            .map(|field| field.eq_ignore_ascii_case("custom"))
128            .unwrap_or(false)
129    }
130}
131
132impl Default for Flags {
133    fn default() -> Self {
134        Self {
135            ascii_distro: "auto".into(),
136            ascii_colors: "distro".into(),
137            custom_ascii_path: String::new(),
138            battery_display: "barinfo".into(),
139            color_blocks: "███".into(),
140            cpu_brand: true,
141            cpu_cores: true,
142            cpu_frequency: true,
143            cpu_speed: true,
144            cpu_temp: 'C',
145            cpu_show_temp: true,
146            de_version: true,
147            distro_display: "name_model_arch".into(),
148            disk_display: "barinfo".into(),
149            disk_subtitle: "dir".into(),
150            memory_percent: true,
151            memory_unit: "mib".into(),
152            package_managers: "tiny".into(),
153            shell_path: true,
154            shell_version: true,
155            uptime_shorthand: "full".into(),
156            os_age_shorthand: "full".into(),
157        }
158    }
159}
160
161// impl Default for Toggles {
162//     fn default() -> Self {
163//         Self {
164//             show_titles: true,
165//             show_os: true,
166//             show_distro: true,
167//             show_model: true,
168//             show_uptime: true,
169//             show_packages: true,
170//             show_shell: true,
171//             show_wm: true,
172//             show_de: true,
173//             show_kernel: true,
174//             show_cpu: true,
175//             show_gpu: true,
176//             show_memory: true,
177//             show_song: true,
178//             show_resolution: true,
179//             show_theme: true,
180//             show_disks: true,
181//             show_battery: true,
182//             show_terminal_colors: true,
183//         }
184//     }
185// }