Skip to content

Commit

Permalink
feat(module): Add a meson devenv indicator (starship#4389)
Browse files Browse the repository at this point in the history
* feat(module): Add a meson devenv indicator

Adds a Meson Developer Environment indicator, if the MESON_DEVENV
variable is set. Inside a `meson devenv`, the prompt will include the
current Meson project name

This also contains a new Truncate utility function, which may be adapted for other modules in the future

* docs: Add Meson to presets
  • Loading branch information
ItsJamie9494 authored Oct 11, 2022
1 parent 4107031 commit 355800f
Show file tree
Hide file tree
Showing 14 changed files with 346 additions and 0 deletions.
47 changes: 47 additions & 0 deletions .github/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,21 @@
}
]
},
"meson": {
"default": {
"disabled": false,
"format": "via [$symbol$project]($style) ",
"style": "blue bold",
"symbol": "",
"truncation_length": 4294967295,
"truncation_symbol": ""
},
"allOf": [
{
"$ref": "#/definitions/MesonConfig"
}
]
},
"nim": {
"default": {
"detect_extensions": [
Expand Down Expand Up @@ -3600,6 +3615,38 @@
},
"additionalProperties": false
},
"MesonConfig": {
"type": "object",
"properties": {
"truncation_length": {
"default": 4294967295,
"type": "integer",
"format": "uint32",
"minimum": 0.0
},
"truncation_symbol": {
"default": "",
"type": "string"
},
"format": {
"default": "via [$symbol$project]($style) ",
"type": "string"
},
"symbol": {
"default": "",
"type": "string"
},
"style": {
"default": "blue bold",
"type": "string"
},
"disabled": {
"default": false,
"type": "boolean"
}
},
"additionalProperties": false
},
"NimConfig": {
"type": "object",
"properties": {
Expand Down
3 changes: 3 additions & 0 deletions docs/.vuepress/public/presets/toml/bracketed-segments.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ format = '\[[$symbol($version)]($style)\]'
[memory_usage]
format = '\[$symbol[$ram( | $swap)]($style)\]'

[meson]
format = '\[[$symbol$project]($style)\]'

[nim]
format = '\[[$symbol($version)]($style)\]'

Expand Down
3 changes: 3 additions & 0 deletions docs/.vuepress/public/presets/toml/nerd-font-symbols.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ symbol = " "
[memory_usage]
symbol = ""

[meson]
symbol = ""

[nim]
symbol = ""

Expand Down
3 changes: 3 additions & 0 deletions docs/.vuepress/public/presets/toml/no-runtime-versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ format = 'via [$symbol]($style)'
[lua]
format = 'via [$symbol]($style)'

[meson]
format = 'via [$symbol]($style)'

[nim]
format = 'via [$symbol]($style)'

Expand Down
3 changes: 3 additions & 0 deletions docs/.vuepress/public/presets/toml/plain-text-symbols.toml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ symbol = "nodejs "
[memory_usage]
symbol = "memory "

[meson]
symbol = "meson "

[nim]
symbol = "nim "

Expand Down
40 changes: 40 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ $zig\
$buf\
$nix_shell\
$conda\
$meson\
$spack\
$memory_usage\
$aws\
Expand Down Expand Up @@ -2384,6 +2385,45 @@ symbol = " "
style = "bold dimmed green"
```

## Meson

The `meson` module shows the current Meson developer environment status.

By default the Meson project name is displayed, if `$MESON_DEVENV` is set.

### Options

| Option | Default | Description |
| ------------------- | ---------------------------------- | ----------------------------------------------------------------------------------------- |
| `truncation_length` | `2^32 - 1` | Truncates a project name to `N` graphemes. |
| `truncation_symbol` | `"…"` | The symbol used to indicate a project name was truncated. You can use `""` for no symbol. |
| `format` | `"via [$symbol$project]($style) "` | The format for the module. |
| `symbol` | `"⬢ "` | The symbol used before displaying the project name. |
| `style` | `"blue bold"` | The style for the module. |
| `disabled` | `false` | Disables the `meson` module. |

### Variables

| Variable | Example | Description |
| -------- | ---------- | ------------------------------------ |
| project | `starship` | The current Meson project name |
| 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

[meson]
disabled = false
truncation_symbol = "--"
symbol = " "
style = "bold dimmed green"
```

## Mercurial Branch

The `hg_branch` module shows the active branch of the repo in your current directory.
Expand Down
30 changes: 30 additions & 0 deletions src/configs/meson.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Deserialize, Serialize)]
#[cfg_attr(
feature = "config-schema",
derive(schemars::JsonSchema),
schemars(deny_unknown_fields)
)]
#[serde(default)]
pub struct MesonConfig<'a> {
pub truncation_length: u32,
pub truncation_symbol: &'a str,
pub format: &'a str,
pub symbol: &'a str,
pub style: &'a str,
pub disabled: bool,
}

impl<'a> Default for MesonConfig<'a> {
fn default() -> Self {
MesonConfig {
truncation_length: std::u32::MAX,
truncation_symbol: "…",
format: "via [$symbol$project]($style) ",
symbol: "⬢ ",
style: "blue bold",
disabled: false,
}
}
}
3 changes: 3 additions & 0 deletions src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub mod line_break;
pub mod localip;
pub mod lua;
pub mod memory_usage;
pub mod meson;
pub mod nim;
pub mod nix_shell;
pub mod nodejs;
Expand Down Expand Up @@ -184,6 +185,8 @@ pub struct FullConfig<'a> {
#[serde(borrow)]
memory_usage: memory_usage::MemoryConfig<'a>,
#[serde(borrow)]
meson: meson::MesonConfig<'a>,
#[serde(borrow)]
nim: nim::NimConfig<'a>,
#[serde(borrow)]
nix_shell: nix_shell::NixShellConfig<'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 @@ -87,6 +87,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"buf",
"nix_shell",
"conda",
"meson",
"spack",
"memory_usage",
"aws",
Expand Down
1 change: 1 addition & 0 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ pub const ALL_MODULES: &[&str] = &[
"localip",
"lua",
"memory_usage",
"meson",
"nim",
"nix_shell",
"nodejs",
Expand Down
98 changes: 98 additions & 0 deletions src/modules/meson.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
use super::{Context, Module, ModuleConfig};

use super::utils::truncate::truncate_text;
use crate::configs::meson::MesonConfig;
use crate::formatter::StringFormatter;

/// Creates a module with the current Meson dev environment
///
/// Will display the Meson environment if `$MESON_DEVENV` and `MESON_PROJECT_NAME` are set.
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let meson_env = context.get_env("MESON_DEVENV")?;
let project_env = context.get_env("MESON_PROJECT_NAME")?;
if meson_env != "1" || project_env.trim().is_empty() {
return None;
}

let mut module = context.new_module("meson");
let config: MesonConfig = MesonConfig::try_load(module.config);

let truncated_text = truncate_text(
&project_env,
config.truncation_length as usize,
config.truncation_symbol,
);

let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_meta(|variable, _| match variable {
"symbol" => Some(config.symbol),
_ => None,
})
.map_style(|variable| match variable {
"style" => Some(Ok(config.style)),
_ => None,
})
.map(|variable| match variable {
"project" => Some(Ok(&truncated_text)),
_ => None,
})
.parse(None, Some(context))
});

module.set_segments(match parsed {
Ok(segments) => segments,
Err(error) => {
log::warn!("Error in module `meson`:\n{}", error);
return None;
}
});

Some(module)
}

#[cfg(test)]
mod tests {
use crate::test::ModuleRenderer;
use nu_ansi_term::Color;

#[test]
fn not_in_env() {
let actual = ModuleRenderer::new("meson").collect();

let expected = None;

assert_eq!(expected, actual);
}

#[test]
fn env_set() {
let actual = ModuleRenderer::new("meson")
.env("MESON_DEVENV", "1")
.env("MESON_PROJECT_NAME", "starship")
.collect();

let expected = Some(format!("via {} ", Color::Blue.bold().paint("⬢ starship")));

assert_eq!(expected, actual);
}

#[test]
fn env_invalid_devenv() {
let actual = ModuleRenderer::new("meson")
.env("MESON_DEVENV", "0")
.env("MESON_PROJECT_NAME", "starship")
.collect();
let expected = None;
assert_eq!(expected, actual);
}
#[test]
fn env_invalid_project_name() {
let actual = ModuleRenderer::new("meson")
.env("MESON_DEVENV", "1")
.env("MESON_PROJECT_NAME", " ")
.collect();
let expected = None;
assert_eq!(expected, actual);
}
}
5 changes: 5 additions & 0 deletions src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod line_break;
mod localip;
mod lua;
mod memory_usage;
mod meson;
mod nim;
mod nix_shell;
mod nodejs;
Expand Down Expand Up @@ -137,6 +138,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"localip" => localip::module(context),
"lua" => lua::module(context),
"memory_usage" => memory_usage::module(context),
"meson" => meson::module(context),
"nim" => nim::module(context),
"nix_shell" => nix_shell::module(context),
"nodejs" => nodejs::module(context),
Expand Down Expand Up @@ -242,6 +244,9 @@ pub fn description(module: &str) -> &'static str {
"localip" => "The currently assigned ipv4 address",
"lua" => "The currently installed version of Lua",
"memory_usage" => "Current system memory and swap usage",
"meson" => {
"The current Meson environment, if $MESON_DEVENV and $MESON_PROJECT_NAME are set"
}
"nim" => "The currently installed version of Nim",
"nix_shell" => "The nix-shell environment",
"nodejs" => "The currently installed version of NodeJS",
Expand Down
2 changes: 2 additions & 0 deletions src/modules/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ pub mod directory_win;
pub mod directory_nix;

pub mod path;

pub mod truncate;
Loading

0 comments on commit 355800f

Please sign in to comment.