Skip to content

Commit

Permalink
feat(loadavg): add module for load average
Browse files Browse the repository at this point in the history
Inspired by #1252 (comment)

Example configuration:

    [loadavg]
    disabled = false

    [[loadavg.display]]
    threshold_one = 8.0
    style = "bold red"

    [[loadavg.display]]
    threshold_one = 4.0
    style = "bold yellow"

    [[loadavg.display]]
    threshold_one = 2.0
    style = "bold white"

    [[loadavg.display]]
    threshold_one = 0.0
    style = "dimmed white"
  • Loading branch information
cgzones committed Feb 26, 2023
1 parent 1447957 commit 676a0e3
Show file tree
Hide file tree
Showing 8 changed files with 393 additions and 0 deletions.
86 changes: 86 additions & 0 deletions .github/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,27 @@
}
]
},
"loadavg": {
"default": {
"disabled": true,
"display": [
{
"style": "white bold",
"symbol": null,
"threshold_fifteen": null,
"threshold_five": null,
"threshold_one": null
}
],
"format": "[$symbol $one $five $fifteen]($style) ",
"symbol": ""
},
"allOf": [
{
"$ref": "#/definitions/LoadavgConfig"
}
]
},
"localip": {
"default": {
"disabled": true,
Expand Down Expand Up @@ -3930,6 +3951,71 @@
},
"additionalProperties": false
},
"LoadavgConfig": {
"type": "object",
"properties": {
"format": {
"default": "[$symbol $one $five $fifteen]($style) ",
"type": "string"
},
"symbol": {
"default": "",
"type": "string"
},
"display": {
"default": [
{
"style": "white bold",
"symbol": null,
"threshold_fifteen": null,
"threshold_five": null,
"threshold_one": null
}
],
"type": "array",
"items": {
"$ref": "#/definitions/LoadavgDisplayConfig"
}
},
"disabled": {
"default": true,
"type": "boolean"
}
},
"additionalProperties": false
},
"LoadavgDisplayConfig": {
"type": "object",
"properties": {
"threshold_one": {
"default": null,
"type": "number",
"format": "float"
},
"threshold_five": {
"default": null,
"type": "number",
"format": "float"
},
"threshold_fifteen": {
"default": null,
"type": "number",
"format": "float"
},
"style": {
"default": "white bold",
"type": "string"
},
"symbol": {
"default": null,
"type": [
"string",
"null"
]
}
},
"additionalProperties": false
},
"LocalipConfig": {
"type": "object",
"properties": {
Expand Down
83 changes: 83 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ $nix_shell\
$conda\
$meson\
$spack\
$loadavg\
$memory_usage\
$aws\
$gcloud\
Expand Down Expand Up @@ -2516,6 +2517,88 @@ The `line_break` module separates the prompt into two lines.
disabled = true
```

## Load Average

The `loadavg` module shows current system load average.

::: tip

This module is disabled by default.
To enable it, set `disabled` to `false` and define at least one `Load Average Display` in your configuration file.

:::

### Options

| Option | Default | Description |
| ---------- | ------------------------------------------ | --------------------------------------------------- |
| `format` | `'[$symbol $one $five $fifteen]($style) '` | The format for the module. |
| `symbol` | `"⌛"` | The symbol used before displaying the load average. |
| `disabled` | `true` | Disables the `loadavg` module. |

### Variables

| Variable | Example | Description |
| -------- | ------- | ------------------------------------ |
| one | `1.42` | The one minute load average. |
| five | `2.01` | The five minute load average. |
| fifteen | `10.98` | The fifteen minute load average. |
| symbol | `` | Mirrors the value of option `symbol` |
| style\* | | Mirrors the value of option `style` |

*: This variable can only be used as a part of a style string

### Example

```toml
# ~/.config/starship.toml

[loadavg]
disabled = false
```

### Load Average Display

The `display` configuration option is used to define when the `Load Average` should be shown (threshold-*), which symbol would be used (symbol).
If no `display` is provided.

By default, the thresholds are set to an invalid value.

The default value for the `symbol` option is the value of `loadavg`'s `symbol` option.

#### Options

The `display` option is an array of the following table.

| Option | Default | Description |
| ------------------- | -------------- | --------------------------------------------------------------------------------------------- |
| `threshold-one` | `NAN` | The lower bound for the one minute average. |
| `threshold-five` | `NAN` | The lower bound for the five minute average. |
| `threshold-fifteen` | `NAN` | The lower bound for the fifteen minute average. |
| `style` | `'white bold'` | The style used if the display option is in use. |
| `symbol` | | Optional symbol displayed if display option is in use, defaults to loadavg's `symbol` option. |

#### Example

```toml
[[loadavg.display]]
threshold_one = 8.0
style = "bold red"

[[loadavg.display]]
threshold_one = 4.0
style = "bold yellow"

[[loadavg.display]]
threshold_one = 2.0
style = "bold white"

[[loadavg.display]]
threshold_one = 0.0
style = "dimmed white"
symbol = ""
```

## Local IP

The `localip` module shows the IPv4 address of the primary network interface.
Expand Down
54 changes: 54 additions & 0 deletions src/configs/loadavg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Deserialize, Serialize)]
#[cfg_attr(
feature = "config-schema",
derive(schemars::JsonSchema),
schemars(deny_unknown_fields)
)]
#[serde(default)]
pub struct LoadavgConfig<'a> {
pub format: &'a str,
pub symbol: &'a str,
#[serde(borrow)]
pub display: Vec<LoadavgDisplayConfig<'a>>,
pub disabled: bool,
}

impl<'a> Default for LoadavgConfig<'a> {
fn default() -> Self {
LoadavgConfig {
format: "[$symbol $one $five $fifteen]($style) ",
symbol: "⌛",
display: vec![LoadavgDisplayConfig::default()],
disabled: true,
}
}
}

#[derive(Clone, Deserialize, Serialize)]
#[cfg_attr(
feature = "config-schema",
derive(schemars::JsonSchema),
schemars(deny_unknown_fields)
)]
#[serde(default)]
pub struct LoadavgDisplayConfig<'a> {
pub threshold_one: f32,
pub threshold_five: f32,
pub threshold_fifteen: f32,
pub style: &'a str,
pub symbol: Option<&'a str>,
}

impl<'a> Default for LoadavgDisplayConfig<'a> {
fn default() -> Self {
LoadavgDisplayConfig {
threshold_one: f32::NAN,
threshold_five: f32::NAN,
threshold_fifteen: f32::NAN,
style: "white bold",
symbol: None,
}
}
}
3 changes: 3 additions & 0 deletions src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub mod julia;
pub mod kotlin;
pub mod kubernetes;
pub mod line_break;
pub mod loadavg;
pub mod localip;
pub mod lua;
pub mod memory_usage;
Expand Down Expand Up @@ -197,6 +198,8 @@ pub struct FullConfig<'a> {
kubernetes: kubernetes::KubernetesConfig<'a>,
line_break: line_break::LineBreakConfig,
#[serde(borrow)]
loadavg: loadavg::LoadavgConfig<'a>,
#[serde(borrow)]
localip: localip::LocalipConfig<'a>,
#[serde(borrow)]
lua: lua::LuaConfig<'a>,
Expand Down
1 change: 1 addition & 0 deletions src/configs/starship_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"conda",
"meson",
"spack",
"loadavg",
"memory_usage",
"aws",
"gcloud",
Expand Down
1 change: 1 addition & 0 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub const ALL_MODULES: &[&str] = &[
"kotlin",
"kubernetes",
"line_break",
"loadavg",
"localip",
"lua",
"memory_usage",
Expand Down

0 comments on commit 676a0e3

Please sign in to comment.