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    /// Custom widget (user-defined via format string)
44    Custom(String),
45}
46
47impl WidgetId {
48    /// Human-readable label for UI display.
49    pub fn label(&self) -> &str {
50        match self {
51            WidgetId::Clock => "Clock",
52            WidgetId::UsernameHostname => "User@Host",
53            WidgetId::CurrentDirectory => "Directory",
54            WidgetId::GitBranch => "Git Branch",
55            WidgetId::CpuUsage => "CPU Usage",
56            WidgetId::MemoryUsage => "Memory Usage",
57            WidgetId::NetworkStatus => "Network Status",
58            WidgetId::BellIndicator => "Bell Indicator",
59            WidgetId::CurrentCommand => "Current Command",
60            WidgetId::Custom(name) => name.as_str(),
61        }
62    }
63
64    /// Icon/prefix character for the widget.
65    pub fn icon(&self) -> &str {
66        match self {
67            WidgetId::Clock => "\u{1f551}",            // clock emoji
68            WidgetId::UsernameHostname => "\u{1f464}", // bust in silhouette
69            WidgetId::CurrentDirectory => "\u{1f4c2}", // open file folder
70            WidgetId::GitBranch => "\u{e0a0}",         // powerline branch symbol
71            WidgetId::CpuUsage => "\u{1f4bb}",         // laptop
72            WidgetId::MemoryUsage => "\u{1f4be}",      // floppy disk
73            WidgetId::NetworkStatus => "\u{1f310}",    // globe with meridians
74            WidgetId::BellIndicator => "\u{1f514}",    // bell
75            WidgetId::CurrentCommand => "\u{25b6}",    // play button
76            WidgetId::Custom(_) => "\u{2699}",         // gear
77        }
78    }
79
80    /// Whether this widget requires the system monitor to be running.
81    pub fn needs_system_monitor(&self) -> bool {
82        matches!(
83            self,
84            WidgetId::CpuUsage | WidgetId::MemoryUsage | WidgetId::NetworkStatus
85        )
86    }
87}
88
89/// Configuration for a single status bar widget.
90#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
91pub struct StatusBarWidgetConfig {
92    /// Which widget to display
93    pub id: WidgetId,
94    /// Whether this widget is enabled
95    #[serde(default = "default_true")]
96    pub enabled: bool,
97    /// Section placement (left, center, right)
98    #[serde(default)]
99    pub section: StatusBarSection,
100    /// Sort order within the section (lower values first)
101    #[serde(default)]
102    pub order: i32,
103    /// Optional format override string with `\(variable)` interpolation
104    #[serde(default, skip_serializing_if = "Option::is_none")]
105    pub format: Option<String>,
106}
107
108fn default_true() -> bool {
109    true
110}
111
112/// Default widget configuration set.
113///
114/// Returns a sensible starting set of widgets covering common use-cases.
115/// System monitor widgets (CPU, memory, network) are disabled by default
116/// to avoid unnecessary resource usage.
117pub fn default_widgets() -> Vec<StatusBarWidgetConfig> {
118    vec![
119        StatusBarWidgetConfig {
120            id: WidgetId::UsernameHostname,
121            enabled: true,
122            section: StatusBarSection::Left,
123            order: 0,
124            format: None,
125        },
126        StatusBarWidgetConfig {
127            id: WidgetId::CurrentDirectory,
128            enabled: true,
129            section: StatusBarSection::Left,
130            order: 1,
131            format: None,
132        },
133        StatusBarWidgetConfig {
134            id: WidgetId::GitBranch,
135            enabled: true,
136            section: StatusBarSection::Left,
137            order: 2,
138            format: None,
139        },
140        StatusBarWidgetConfig {
141            id: WidgetId::CurrentCommand,
142            enabled: true,
143            section: StatusBarSection::Center,
144            order: 0,
145            format: None,
146        },
147        StatusBarWidgetConfig {
148            id: WidgetId::CpuUsage,
149            enabled: false,
150            section: StatusBarSection::Right,
151            order: 0,
152            format: None,
153        },
154        StatusBarWidgetConfig {
155            id: WidgetId::MemoryUsage,
156            enabled: false,
157            section: StatusBarSection::Right,
158            order: 1,
159            format: None,
160        },
161        StatusBarWidgetConfig {
162            id: WidgetId::NetworkStatus,
163            enabled: false,
164            section: StatusBarSection::Right,
165            order: 2,
166            format: None,
167        },
168        StatusBarWidgetConfig {
169            id: WidgetId::BellIndicator,
170            enabled: true,
171            section: StatusBarSection::Right,
172            order: 3,
173            format: None,
174        },
175        StatusBarWidgetConfig {
176            id: WidgetId::Clock,
177            enabled: true,
178            section: StatusBarSection::Right,
179            order: 4,
180            format: None,
181        },
182    ]
183}