Skip to content

Commit

Permalink
feat(bar): add label prefix config opt
Browse files Browse the repository at this point in the history
This commit makes the label prefix configurable. Users can select if
they want to show an icon, only text, or both text and an icon.
  • Loading branch information
CtByte authored and LGUG2Z committed Oct 10, 2024
1 parent 24da24f commit 26a8912
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 50 deletions.
27 changes: 23 additions & 4 deletions komorebi-bar/src/battery.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::LabelPrefix;
use crate::widget::BarWidget;
use crate::WIDGET_SPACING;
use eframe::egui::text::LayoutJob;
Expand All @@ -23,13 +24,16 @@ pub struct BatteryConfig {
pub enable: bool,
/// Data refresh interval (default: 10 seconds)
pub data_refresh_interval: Option<u64>,
/// Display label prefix
pub label_prefix: Option<LabelPrefix>,
}

impl From<BatteryConfig> for Battery {
fn from(value: BatteryConfig) -> Self {
let manager = Manager::new().unwrap();
let mut last_state = String::new();
let mut state = None;
let prefix = value.label_prefix.unwrap_or(LabelPrefix::Icon);

if let Ok(mut batteries) = manager.batteries() {
if let Some(Ok(first)) = batteries.nth(0) {
Expand All @@ -39,8 +43,13 @@ impl From<BatteryConfig> for Battery {
State::Discharging => state = Some(BatteryState::Discharging),
_ => {}
}

last_state = format!("{percentage}%");

last_state = match prefix {
LabelPrefix::Text | LabelPrefix::IconAndText => {
format!("BAT: {percentage:.0}%")
}
LabelPrefix::None | LabelPrefix::Icon => format!("{percentage:.0}%"),
}
}
}

Expand All @@ -49,6 +58,7 @@ impl From<BatteryConfig> for Battery {
manager,
last_state,
data_refresh_interval: value.data_refresh_interval.unwrap_or(10),
label_prefix: prefix,
state: state.unwrap_or(BatteryState::Discharging),
last_updated: Instant::now(),
}
Expand All @@ -65,6 +75,7 @@ pub struct Battery {
manager: Manager,
pub state: BatteryState,
data_refresh_interval: u64,
label_prefix: LabelPrefix,
last_state: String,
last_updated: Instant,
}
Expand All @@ -86,7 +97,12 @@ impl Battery {
_ => {}
}

output = format!("{percentage:.0}%");
output = match self.label_prefix {
LabelPrefix::Text | LabelPrefix::IconAndText => {
format!("BAT: {percentage:.0}%")
}
LabelPrefix::None | LabelPrefix::Icon => format!("{percentage:.0}%"),
}
}
}

Expand Down Expand Up @@ -116,7 +132,10 @@ impl BarWidget for Battery {
.unwrap_or_else(FontId::default);

let mut layout_job = LayoutJob::simple(
emoji.to_string(),
match self.label_prefix {
LabelPrefix::Icon | LabelPrefix::IconAndText => emoji.to_string(),
LabelPrefix::None | LabelPrefix::Text => String::new(),
},
font_id.clone(),
ctx.style().visuals.selection.stroke.color,
100.0,
Expand Down
12 changes: 12 additions & 0 deletions komorebi-bar/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,15 @@ impl From<KomorebiTheme> for KomobarTheme {
}
}
}

#[derive(Copy, Clone, Debug, Serialize, Deserialize, JsonSchema)]
pub enum LabelPrefix {
/// Show no prefix
None,
/// Show an icon
Icon,
/// Show text
Text,
/// Show an icon and text
IconAndText,
}
17 changes: 15 additions & 2 deletions komorebi-bar/src/cpu.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::LabelPrefix;
use crate::widget::BarWidget;
use crate::WIDGET_SPACING;
use eframe::egui::text::LayoutJob;
Expand All @@ -23,6 +24,8 @@ pub struct CpuConfig {
pub enable: bool,
/// Data refresh interval (default: 10 seconds)
pub data_refresh_interval: Option<u64>,
/// Display label prefix
pub label_prefix: Option<LabelPrefix>,
}

impl From<CpuConfig> for Cpu {
Expand All @@ -36,6 +39,7 @@ impl From<CpuConfig> for Cpu {
enable: value.enable,
system,
data_refresh_interval: value.data_refresh_interval.unwrap_or(10),
label_prefix: value.label_prefix.unwrap_or(LabelPrefix::IconAndText),
last_updated: Instant::now(),
}
}
Expand All @@ -45,6 +49,7 @@ pub struct Cpu {
pub enable: bool,
system: System,
data_refresh_interval: u64,
label_prefix: LabelPrefix,
last_updated: Instant,
}

Expand All @@ -57,7 +62,10 @@ impl Cpu {
}

let used = self.system.global_cpu_usage();
format!("CPU: {:.0}%", used)
match self.label_prefix {
LabelPrefix::Text | LabelPrefix::IconAndText => format!("CPU: {:.0}%", used),
LabelPrefix::None | LabelPrefix::Icon => format!("{:.0}%", used),
}
}
}

Expand All @@ -74,7 +82,12 @@ impl BarWidget for Cpu {
.unwrap_or_else(FontId::default);

let mut layout_job = LayoutJob::simple(
egui_phosphor::regular::CIRCUITRY.to_string(),
match self.label_prefix {
LabelPrefix::Icon | LabelPrefix::IconAndText => {
egui_phosphor::regular::CIRCUITRY.to_string()
}
LabelPrefix::None | LabelPrefix::Text => String::new(),
},
font_id.clone(),
ctx.style().visuals.selection.stroke.color,
100.0,
Expand Down
18 changes: 16 additions & 2 deletions komorebi-bar/src/date.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::LabelPrefix;
use crate::widget::BarWidget;
use crate::WIDGET_SPACING;
use eframe::egui::text::LayoutJob;
Expand All @@ -19,13 +20,16 @@ pub struct DateConfig {
pub enable: bool,
/// Set the Date format
pub format: DateFormat,
/// Display label prefix
pub label_prefix: Option<LabelPrefix>,
}

impl From<DateConfig> for Date {
fn from(value: DateConfig) -> Self {
Self {
enable: value.enable,
format: value.format,
label_prefix: value.label_prefix.unwrap_or(LabelPrefix::Icon),
}
}
}
Expand Down Expand Up @@ -70,6 +74,7 @@ impl DateFormat {
pub struct Date {
pub enable: bool,
pub format: DateFormat,
label_prefix: LabelPrefix,
}

impl Date {
Expand All @@ -83,7 +88,7 @@ impl Date {
impl BarWidget for Date {
fn render(&mut self, ctx: &Context, ui: &mut Ui) {
if self.enable {
let output = self.output();
let mut output = self.output();
if !output.is_empty() {
let font_id = ctx
.style()
Expand All @@ -93,12 +98,21 @@ impl BarWidget for Date {
.unwrap_or_else(FontId::default);

let mut layout_job = LayoutJob::simple(
egui_phosphor::regular::CALENDAR_DOTS.to_string(),
match self.label_prefix {
LabelPrefix::Icon | LabelPrefix::IconAndText => {
egui_phosphor::regular::CALENDAR_DOTS.to_string()
}
LabelPrefix::None | LabelPrefix::Text => String::new(),
},
font_id.clone(),
ctx.style().visuals.selection.stroke.color,
100.0,
);

if let LabelPrefix::Text | LabelPrefix::IconAndText = self.label_prefix {
output.insert_str(0, "DATE: ");
}

layout_job.append(
&output,
10.0,
Expand Down
19 changes: 17 additions & 2 deletions komorebi-bar/src/memory.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::LabelPrefix;
use crate::widget::BarWidget;
use crate::WIDGET_SPACING;
use eframe::egui::text::LayoutJob;
Expand All @@ -23,6 +24,8 @@ pub struct MemoryConfig {
pub enable: bool,
/// Data refresh interval (default: 10 seconds)
pub data_refresh_interval: Option<u64>,
/// Display label prefix
pub label_prefix: Option<LabelPrefix>,
}

impl From<MemoryConfig> for Memory {
Expand All @@ -36,6 +39,7 @@ impl From<MemoryConfig> for Memory {
enable: value.enable,
system,
data_refresh_interval: value.data_refresh_interval.unwrap_or(10),
label_prefix: value.label_prefix.unwrap_or(LabelPrefix::IconAndText),
last_updated: Instant::now(),
}
}
Expand All @@ -45,6 +49,7 @@ pub struct Memory {
pub enable: bool,
system: System,
data_refresh_interval: u64,
label_prefix: LabelPrefix,
last_updated: Instant,
}

Expand All @@ -58,7 +63,12 @@ impl Memory {

let used = self.system.used_memory();
let total = self.system.total_memory();
format!("RAM: {}%", (used * 100) / total)
match self.label_prefix {
LabelPrefix::Text | LabelPrefix::IconAndText => {
format!("RAM: {}%", (used * 100) / total)
}
LabelPrefix::None | LabelPrefix::Icon => format!("{}%", (used * 100) / total),
}
}
}

Expand All @@ -75,7 +85,12 @@ impl BarWidget for Memory {
.unwrap_or_else(FontId::default);

let mut layout_job = LayoutJob::simple(
egui_phosphor::regular::MEMORY.to_string(),
match self.label_prefix {
LabelPrefix::Icon | LabelPrefix::IconAndText => {
egui_phosphor::regular::MEMORY.to_string()
}
LabelPrefix::None | LabelPrefix::Text => String::new(),
},
font_id.clone(),
ctx.style().visuals.selection.stroke.color,
100.0,
Expand Down
Loading

0 comments on commit 26a8912

Please sign in to comment.