Skip to main content

par_term_config/
status_bar.rs

1//! Status bar widget configuration types.
2//!
3//! Defines the widget identifiers, section layout, and per-widget configuration
4//! used by the status bar system.
5
6use serde::{Deserialize, Serialize};
7
8/// Section of the status bar where a widget is placed.
9#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize, Serialize, Default)]
10#[serde(rename_all = "lowercase")]
11pub enum StatusBarSection {
12    /// Left-aligned section (default)
13    #[default]
14    Left,
15    /// Center-aligned section
16    Center,
17    /// Right-aligned section
18    Right,
19}
20
21/// Identifier for a built-in or custom status bar widget.
22#[derive(Debug, Clone, PartialEq, Eq, Hash, Deserialize, Serialize)]
23#[serde(rename_all = "snake_case")]
24pub enum WidgetId {
25    /// Current time (HH:MM:SS)
26    Clock,
27    /// user@hostname
28    UsernameHostname,
29    /// Current working directory
30    CurrentDirectory,
31    /// Git branch name with icon
32    GitBranch,
33    /// CPU usage percentage
34    CpuUsage,
35    /// Memory usage (used / total)
36    MemoryUsage,
37    /// Network throughput (rx/tx rates)
38    NetworkStatus,
39    /// Bell indicator with count
40    BellIndicator,
41    /// Currently running command name
42    CurrentCommand,
43    /// Update available notification
44    UpdateAvailable,
45    /// Custom widget (user-defined via format string)
46    Custom(String),
47}
48
49impl WidgetId {
50    /// Human-readable label for UI display.
51    pub fn label(&self) -> &str {
52        match self {
53            WidgetId::Clock => "Clock",
54            WidgetId::UsernameHostname => "User@Host",
55            WidgetId::CurrentDirectory => "Directory",
56            WidgetId::GitBranch => "Git Branch",
57            WidgetId::CpuUsage => "CPU Usage",
58            WidgetId::MemoryUsage => "Memory Usage",
59            WidgetId::NetworkStatus => "Network Status",
60            WidgetId::BellIndicator => "Bell Indicator",
61            WidgetId::CurrentCommand => "Current Command",
62            WidgetId::UpdateAvailable => "Update Available",
63            WidgetId::Custom(name) => name.as_str(),
64        }
65    }
66
67    /// Icon/prefix character for the widget.
68    pub fn icon(&self) -> &str {
69        match self {
70            WidgetId::Clock => "\u{1f551}",            // clock emoji
71            WidgetId::UsernameHostname => "\u{1f464}", // bust in silhouette
72            WidgetId::CurrentDirectory => "\u{1f4c2}", // open file folder
73            WidgetId::GitBranch => "\u{1f500}",        // twisted rightwards arrows (branch)
74            WidgetId::CpuUsage => "\u{1f4bb}",         // laptop
75            WidgetId::MemoryUsage => "\u{1f4be}",      // floppy disk
76            WidgetId::NetworkStatus => "\u{1f310}",    // globe with meridians
77            WidgetId::BellIndicator => "\u{1f514}",    // bell
78            WidgetId::CurrentCommand => "\u{25b6}",    // play button
79            WidgetId::UpdateAvailable => "\u{2b06}",   // upwards arrow
80            WidgetId::Custom(_) => "\u{2699}",         // gear
81        }
82    }
83
84    /// Whether this widget requires the system monitor to be running.
85    pub fn needs_system_monitor(&self) -> bool {
86        matches!(
87            self,
88            WidgetId::CpuUsage | WidgetId::MemoryUsage | WidgetId::NetworkStatus
89        )
90    }
91}
92
93/// Configuration for a single status bar widget.
94#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
95pub struct StatusBarWidgetConfig {
96    /// Which widget to display
97    pub id: WidgetId,
98    /// Whether this widget is enabled
99    #[serde(default = "default_true")]
100    pub enabled: bool,
101    /// Section placement (left, center, right)
102    #[serde(default)]
103    pub section: StatusBarSection,
104    /// Sort order within the section (lower values first)
105    #[serde(default)]
106    pub order: i32,
107    /// Optional format override string with `\(variable)` interpolation
108    #[serde(default, skip_serializing_if = "Option::is_none")]
109    pub format: Option<String>,
110}
111
112fn default_true() -> bool {
113    true
114}
115
116/// Default widget configuration set.
117///
118/// Returns a sensible starting set of widgets covering common use-cases.
119/// System monitor widgets (CPU, memory, network) are disabled by default
120/// to avoid unnecessary resource usage.
121pub fn default_widgets() -> Vec<StatusBarWidgetConfig> {
122    vec![
123        StatusBarWidgetConfig {
124            id: WidgetId::UsernameHostname,
125            enabled: true,
126            section: StatusBarSection::Left,
127            order: 0,
128            format: None,
129        },
130        StatusBarWidgetConfig {
131            id: WidgetId::CurrentDirectory,
132            enabled: true,
133            section: StatusBarSection::Left,
134            order: 1,
135            format: None,
136        },
137        StatusBarWidgetConfig {
138            id: WidgetId::GitBranch,
139            enabled: true,
140            section: StatusBarSection::Left,
141            order: 2,
142            format: None,
143        },
144        StatusBarWidgetConfig {
145            id: WidgetId::CurrentCommand,
146            enabled: true,
147            section: StatusBarSection::Center,
148            order: 0,
149            format: None,
150        },
151        StatusBarWidgetConfig {
152            id: WidgetId::CpuUsage,
153            enabled: false,
154            section: StatusBarSection::Right,
155            order: 0,
156            format: None,
157        },
158        StatusBarWidgetConfig {
159            id: WidgetId::MemoryUsage,
160            enabled: false,
161            section: StatusBarSection::Right,
162            order: 1,
163            format: None,
164        },
165        StatusBarWidgetConfig {
166            id: WidgetId::NetworkStatus,
167            enabled: false,
168            section: StatusBarSection::Right,
169            order: 2,
170            format: None,
171        },
172        StatusBarWidgetConfig {
173            id: WidgetId::BellIndicator,
174            enabled: true,
175            section: StatusBarSection::Right,
176            order: 3,
177            format: None,
178        },
179        StatusBarWidgetConfig {
180            id: WidgetId::Clock,
181            enabled: true,
182            section: StatusBarSection::Right,
183            order: 4,
184            format: None,
185        },
186        StatusBarWidgetConfig {
187            id: WidgetId::UpdateAvailable,
188            enabled: true,
189            section: StatusBarSection::Right,
190            order: 5,
191            format: None,
192        },
193    ]
194}