Skip to content

Commit

Permalink
refactor: rename cpu module to cpu_usage
Browse files Browse the repository at this point in the history
  • Loading branch information
doums committed Aug 30, 2024
1 parent 0249e0e commit ba510de
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 59 deletions.
57 changes: 41 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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:

Expand All @@ -61,9 +61,34 @@ RUST_LOG=debug baru -l stdout

#### Config example

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.

```yaml
format: '%m %f %c %t %b %i %s %w%e %a %d'
tick: 50

# modules configuration
battery:
full_design: true
low_level: 30
Expand All @@ -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'
Expand Down Expand Up @@ -107,7 +132,7 @@ wired:
discrete: true
label: 'e'
disconnected_label: '\'
format: '%l'
format: '%l' # display label only
wireless:
interface: wlan0
display: Essid
Expand All @@ -134,7 +159,7 @@ weather:
format: '%v'
```
### usage
### Usage
```shell
baru -h
Expand All @@ -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).\
Expand All @@ -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
Expand All @@ -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
52 changes: 26 additions & 26 deletions baru.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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.


# # # # # # # # # #
Expand Down Expand Up @@ -170,11 +170,11 @@ brightness:
format: '%l:%v'


# # # # # # # #
# Cpu module #
# # # # # # # #
# # # # # # # # # # #
# Cpu usage module #
# # # # # # # # # # #

cpu:
cpu_usage:
# Takes the following options:

# tick: u32, default: 500
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,7 +54,7 @@ pub struct Config {
pulse_tick: Option<u32>,
battery: Option<BatteryConfig>,
brightness: Option<BrightnessConfig>,
cpu: Option<CpuConfig>,
cpu_usage: Option<CpuUsageConfig>,
cpu_freq: Option<CpuFreqConfig>,
date_time: Option<DateTimeConfig>,
memory: Option<MemoryConfig>,
Expand Down
14 changes: 7 additions & 7 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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>),
Expand All @@ -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))),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
14 changes: 7 additions & 7 deletions src/modules/cpu.rs → src/modules/cpu_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -73,33 +73,33 @@ 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
}
if let Some(v) = &c.format {
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 {
Expand Down
2 changes: 1 addition & 1 deletion src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ba510de

Please sign in to comment.