feat(battery): Add a symbol option to `battery.display` (#2475)

* feat: Add a symbol option to `battery.display`

* feat: Add a symbol option to `battery.display`

* use `impl defaulat` instead of `RootModuleConfig`

* edit the code according to clippy's linting

* change variable type to `Option<'a str>`

* update the documentation on the battery module

* updated documentation and source code according to review comment

* remove the unnecessary method and write the default value of BatteryDisplayConig to the document

* add 'charging_symbol' option to battery.display
This commit is contained in:
t-mangoe 2021-04-17 20:52:46 +09:00 committed by GitHub
parent e0f181d2c7
commit 1ee59ed18c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 14 deletions

View File

@ -348,7 +348,7 @@ discharging_symbol = "💀 "
### Battery Display ### Battery Display
The `display` configuration option is used to define when the battery indicator should be shown (threshold) and what it looks like (style). The `display` configuration option is used to define when the battery indicator should be shown (threshold), which symbol would be used (symbol), and what it would like (style).
If no `display` is provided. The default is as shown: If no `display` is provided. The default is as shown:
```toml ```toml
@ -357,25 +357,30 @@ threshold = 10
style = "bold red" style = "bold red"
``` ```
The default value for the `charging_symbol` and `discharging_symbol` option is respectively the value of `battery`'s `charging_symbol` and `discharging_symbol` option.
#### Options #### Options
The `display` option is an array of the following table. The `display` option is an array of the following table.
| Option | Description | | Option | Default | Description |
| ----------- | ----------------------------------------------- | | -------------------- | ---------- | ----------------------------------------------- |
| `threshold` | The upper bound for the display option. | | `threshold` | `10` | The upper bound for the display option. |
| `style` | The style used if the display option is in use. | | `style` | `bold red` | The style used if the display option is in use. |
| `charging_symbol` | `-` | Optional symbol displayed if display option is in use, defaults to battery's `charging_symbol` option. |
| `discharging_symbol` | `-` | Optional symbol displayed if display option is in use, defaults to battery's `discharging_symbol` option. |
#### Example #### Example
```toml ```toml
[[battery.display]] # "bold red" style when capacity is between 0% and 10% [[battery.display]] # "bold red" style and discharging_symbol when capacity is between 0% and 10%
threshold = 10 threshold = 10
style = "bold red" style = "bold red"
[[battery.display]] # "bold yellow" style when capacity is between 10% and 30% [[battery.display]] # "bold yellow" style and 💦 symbol when capacity is between 10% and 30%
threshold = 30 threshold = 30
style = "bold yellow" style = "bold yellow"
discharging_symbol = 💦
# when capacity is over 30%, the battery indicator will not be displayed # when capacity is over 30%, the battery indicator will not be displayed

View File

@ -24,17 +24,27 @@ impl<'a> Default for BatteryConfig<'a> {
unknown_symbol: "", unknown_symbol: "",
empty_symbol: "", empty_symbol: "",
format: "[$symbol$percentage]($style) ", format: "[$symbol$percentage]($style) ",
display: vec![BatteryDisplayConfig { display: vec![BatteryDisplayConfig::default()],
threshold: 10,
style: "red bold",
}],
disabled: false, disabled: false,
} }
} }
} }
#[derive(Clone, ModuleConfig, Default, Serialize)] #[derive(Clone, ModuleConfig, Serialize)]
pub struct BatteryDisplayConfig<'a> { pub struct BatteryDisplayConfig<'a> {
pub threshold: i64, pub threshold: i64,
pub style: &'a str, pub style: &'a str,
pub charging_symbol: Option<&'a str>,
pub discharging_symbol: Option<&'a str>,
}
impl<'a> Default for BatteryDisplayConfig<'a> {
fn default() -> Self {
BatteryDisplayConfig {
threshold: 10,
style: "red bold",
charging_symbol: None,
discharging_symbol: None,
}
}
} }

View File

@ -33,8 +33,12 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
.map_meta(|variable, _| match variable { .map_meta(|variable, _| match variable {
"symbol" => match state { "symbol" => match state {
battery::State::Full => Some(config.full_symbol), battery::State::Full => Some(config.full_symbol),
battery::State::Charging => Some(config.charging_symbol), battery::State::Charging => display_style
battery::State::Discharging => Some(config.discharging_symbol), .charging_symbol
.or(Some(config.charging_symbol)),
battery::State::Discharging => display_style
.discharging_symbol
.or(Some(config.discharging_symbol)),
battery::State::Unknown => Some(config.unknown_symbol), battery::State::Unknown => Some(config.unknown_symbol),
battery::State::Empty => Some(config.empty_symbol), battery::State::Empty => Some(config.empty_symbol),
_ => { _ => {