From 0757a823d0e480a1f2dd1a16f613f14d667dee19 Mon Sep 17 00:00:00 2001 From: pierre Date: Sat, 31 Aug 2024 01:00:37 +0200 Subject: [PATCH] refactor: rename cpu module to cpu_usage --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 57 ++++++++++++++++++++-------- baru.yaml | 52 ++++++++++++------------- src/lib.rs | 4 +- src/module.rs | 14 +++---- src/modules/{cpu.rs => cpu_usage.rs} | 14 +++---- src/modules/mod.rs | 2 +- 8 files changed, 86 insertions(+), 61 deletions(-) rename src/modules/{cpu.rs => cpu_usage.rs} (95%) diff --git a/Cargo.lock b/Cargo.lock index eaddd9b..8eba78a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -125,7 +125,7 @@ dependencies = [ [[package]] name = "baru" -version = "0.4.2" +version = "0.4.3" dependencies = [ "anyhow", "chrono", diff --git a/Cargo.toml b/Cargo.toml index d1b5bdf..8cbb440 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "baru" -version = "0.4.2" +version = "0.4.3" description = "A simple system monitor for WM statusbar" authors = ["pierre "] edition = "2021" diff --git a/README.md b/README.md index 58f7ac7..f92eee5 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Like [xmobar](https://codeberg.org/xmobar/xmobar), [features](#features) ∎ [prerequisite](#prerequisite) ∎ [install](#install) ∎ [configuration](#configuration) ∎ [usage](#usage) ∎ [credits](#credits) ∎ [license](#license) -### features +### Features * date and time * battery (level, status, design level based) @@ -33,25 +33,25 @@ Like [xmobar](https://codeberg.org/xmobar/xmobar), * customizable format output * configuration in YAML -### prerequisite +### Prerequisite The following system libraries are required: - libnl (for wired and wireless modules) - libpulse (for sound and mic modules) -### install +### Install - Arch Linux (AUR) [package](https://aur.archlinux.org/packages/baru) - latest [release](https://github.com/doums/baru/releases) -### configuration +### Configuration The binary looks for the config file `baru.yaml` located in `$XDG_CONFIG_HOME/baru/` (default to `$HOME/.config/baru/`).\ -If the config file is not found, baru prints an error and exits.\ -All options are -detailed [here](https://github.com/doums/baru/blob/master/baru.yaml). +If the config file is not found, baru prints an error and exits. + +You can find the full config details [here](https://github.com/doums/baru/blob/master/baru.yaml). TIPS: To test and debug your config run baru from the terminal like this: @@ -59,11 +59,36 @@ TIPS: To test and debug your config run baru from the terminal like this: RUST_LOG=debug baru -l stdout ``` +Use the root `format` option to customize baru output.\ +You can pick which modules you want to display. Using the special markup `%x` +where `x` is the letter of the module. +These markups are replaced by the output of the corresponding modules. + +Modules available: +- `a` battery +- `b` brightness +- `c` cpu usage +- `d` datetime +- `e` wired +- `f` cpu frequency +- `m` memory +- `i` mic +- `r` weather +- `s` sound +- `t` temperature +- `w` wireless + +Module output:\ +Each module takes a `format` option.\ +`%l` and `%v` are respectively the label and the current value of the module. + #### Config example ```yaml format: '%m %f %c %t %b %i %s %w%e %a %d' tick: 50 + +# modules configuration battery: full_design: true low_level: 30 @@ -72,11 +97,11 @@ battery: discharging_label: 'b' low_label: '!' unknown_label: '?' - format: '%l %v' + format: '%l %v' # display label and value brightness: label: 'l' format: '%l %v' -cpu: +cpu_usage: label: 'c' high_label: '!' format: '%v %l' @@ -107,7 +132,7 @@ wired: discrete: true label: 'e' disconnected_label: '\' - format: '%l' + format: '%l' # display label only wireless: interface: wlan0 display: Essid @@ -134,7 +159,7 @@ weather: format: '%v' ``` -### usage +### Usage ```shell baru -h @@ -149,7 +174,7 @@ to `$HOME/.cache/baru/`). baru -l file ``` -### implementation details +### Implementation details Baru gathers the information from `/sys` and `/proc` filesystems (filled by the kernel).\ @@ -171,9 +196,9 @@ In addition, wireless module uses the [802.11](https://github.com/torvalds/linux/blob/master/include/uapi/linux/nl80211.h) API. -### dev +### Dev -#### prerequisites +#### Prerequisites - [Rust](https://www.rust-lang.org/tools/install) - CMake @@ -183,11 +208,11 @@ API. RUST_LOG=trace cargo run -- -l stdout ``` -### credits +### Credits Clément Dommerc for providing me with the C code for the lib `netlink`, wireless part. -### license +### License Mozilla Public License 2.0 diff --git a/baru.yaml b/baru.yaml index f19fdb9..fdf0e98 100644 --- a/baru.yaml +++ b/baru.yaml @@ -10,27 +10,27 @@ # format: String, required # -# The format of the output. +# The global output. # -# A module can be enabled by markup of this form: "%x", -# where x is the corresponding letter for that module. -# These markups will be replaced by the output of the corresponding module. -# Here are the letters: +# You can pick which modules you want to display. Using the special markup `%x` +# where `x` is the letter of the module. +# These markups are replaced by the output of the corresponding modules. # -# a => battery -# b => brightness -# c => cpu -# d => datetime -# e => wired -# f => cpu frequency -# m => memory -# i => mic -# r => weather -# s => sound -# t => temperature -# w => wireless +# Modules available: +# a → battery +# b → brightness +# c → cpu usage +# d → datetime +# e → wired +# f → cpu frequency +# m → memory +# i → mic +# r → weather +# s → sound +# t → temperature +# w → wireless # -# The character "%" can be escaped by prepending a backslash: \% +# The character "%" can be escaped by prepending a backslash: `\%` # format: '%c %t %b %s %w%e %a %d' @@ -52,10 +52,10 @@ pulse_tick: 100 # failed_icon: '✗' -# Each module take a "format" option. -# This option is a string and the following markups will be replaced respectively by the value and the label of the module: -# %v -# %l +# Module output: +# Each module takes a `format` string option. +# `%l` and `%v` are respectively the label and the current value of the module. +# Note: for some module, the label can be dynamic. See below. # # # # # # # # # # @@ -170,11 +170,11 @@ brightness: format: '%l:%v' -# # # # # # # # -# Cpu module # -# # # # # # # # +# # # # # # # # # # # +# Cpu usage module # +# # # # # # # # # # # -cpu: +cpu_usage: # Takes the following options: # tick: u32, default: 500 diff --git a/src/lib.rs b/src/lib.rs index 94bbbcb..8f6e656 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,8 +18,8 @@ use error::Error; use module::{Bar, ModuleData}; use modules::battery::Config as BatteryConfig; use modules::brightness::Config as BrightnessConfig; -use modules::cpu::Config as CpuConfig; use modules::cpu_freq::Config as CpuFreqConfig; +use modules::cpu_usage::Config as CpuUsageConfig; use modules::date_time::Config as DateTimeConfig; use modules::memory::Config as MemoryConfig; use modules::mic::Config as MicConfig; @@ -54,7 +54,7 @@ pub struct Config { pulse_tick: Option, battery: Option, brightness: Option, - cpu: Option, + cpu_usage: Option, cpu_freq: Option, date_time: Option, memory: Option, diff --git a/src/module.rs b/src/module.rs index 8d3f634..c09ae7e 100644 --- a/src/module.rs +++ b/src/module.rs @@ -5,8 +5,8 @@ use crate::error::Error; use crate::modules::battery::Battery; use crate::modules::brightness::Brightness; -use crate::modules::cpu::Cpu; use crate::modules::cpu_freq::CpuFreq; +use crate::modules::cpu_usage::CpuUsage; use crate::modules::date_time::DateTime; use crate::modules::memory::Memory; use crate::modules::mic::Mic; @@ -40,7 +40,7 @@ pub trait Bar { pub enum Module<'a> { Battery(Battery<'a>), Brightness(Brightness<'a>), - Cpu(Cpu<'a>), + CpuUsage(CpuUsage<'a>), CpuFreq(CpuFreq<'a>), DateTime(DateTime<'a>), Memory(Memory<'a>), @@ -59,7 +59,7 @@ impl<'a> TryFrom<(char, &'a Config)> for Module<'a> { match key { 'a' => Ok(Module::Battery(Battery::with_config(config))), 'b' => Ok(Module::Brightness(Brightness::with_config(config))), - 'c' => Ok(Module::Cpu(Cpu::with_config(config))), + 'c' => Ok(Module::CpuUsage(CpuUsage::with_config(config))), 'd' => Ok(Module::DateTime(DateTime::with_config(config))), 'e' => Ok(Module::Wired(Wired::with_config(config))), 'f' => Ok(Module::CpuFreq(CpuFreq::with_config(config))), @@ -79,7 +79,7 @@ impl<'a> Bar for Module<'a> { match self { Module::Battery(m) => m.name(), Module::Brightness(m) => m.name(), - Module::Cpu(m) => m.name(), + Module::CpuUsage(m) => m.name(), Module::CpuFreq(m) => m.name(), Module::DateTime(m) => m.name(), Module::Memory(m) => m.name(), @@ -96,7 +96,7 @@ impl<'a> Bar for Module<'a> { match self { Module::Battery(m) => m.run_fn(), Module::Brightness(m) => m.run_fn(), - Module::Cpu(m) => m.run_fn(), + Module::CpuUsage(m) => m.run_fn(), Module::CpuFreq(m) => m.run_fn(), Module::DateTime(m) => m.run_fn(), Module::Memory(m) => m.run_fn(), @@ -113,7 +113,7 @@ impl<'a> Bar for Module<'a> { match self { Module::Battery(m) => m.placeholder(), Module::Brightness(m) => m.placeholder(), - Module::Cpu(m) => m.placeholder(), + Module::CpuUsage(m) => m.placeholder(), Module::CpuFreq(m) => m.placeholder(), Module::DateTime(m) => m.placeholder(), Module::Memory(m) => m.placeholder(), @@ -130,7 +130,7 @@ impl<'a> Bar for Module<'a> { match self { Module::Battery(m) => m.format(), Module::Brightness(m) => m.format(), - Module::Cpu(m) => m.format(), + Module::CpuUsage(m) => m.format(), Module::CpuFreq(m) => m.format(), Module::DateTime(m) => m.format(), Module::Memory(m) => m.format(), diff --git a/src/modules/cpu.rs b/src/modules/cpu_usage.rs similarity index 95% rename from src/modules/cpu.rs rename to src/modules/cpu_usage.rs index d651249..a280caf 100644 --- a/src/modules/cpu.rs +++ b/src/modules/cpu_usage.rs @@ -48,7 +48,7 @@ impl<'a> From<&'a MainConfig> for InternalConfig<'a> { let mut high_level = HIGH_LEVEL; let mut label = LABEL; let mut high_label = HIGH_LABEL; - if let Some(c) = &config.cpu { + if let Some(c) = &config.cpu_usage { if let Some(t) = c.tick { tick = Duration::from_millis(t as u64) } @@ -73,16 +73,16 @@ impl<'a> From<&'a MainConfig> for InternalConfig<'a> { } #[derive(Debug)] -pub struct Cpu<'a> { +pub struct CpuUsage<'a> { placeholder: &'a str, format: &'a str, } -impl<'a> Cpu<'a> { +impl<'a> CpuUsage<'a> { pub fn with_config(config: &'a MainConfig) -> Self { let mut placeholder = PLACEHOLDER; let mut format = FORMAT; - if let Some(c) = &config.cpu { + if let Some(c) = &config.cpu_usage { if let Some(p) = &c.placeholder { placeholder = p } @@ -90,16 +90,16 @@ impl<'a> Cpu<'a> { format = v; } } - Cpu { + CpuUsage { placeholder, format, } } } -impl<'a> Bar for Cpu<'a> { +impl<'a> Bar for CpuUsage<'a> { fn name(&self) -> &str { - "cpu" + "cpu_usage" } fn run_fn(&self) -> RunPtr { diff --git a/src/modules/mod.rs b/src/modules/mod.rs index be0866d..6ccebe4 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -4,8 +4,8 @@ pub mod battery; pub mod brightness; -pub mod cpu; pub mod cpu_freq; +pub mod cpu_usage; pub mod date_time; pub mod memory; pub mod mic;