feat: added mise module

This commit is contained in:
Andrew Pantuso 2024-02-01 18:36:47 -05:00 committed by Andrew Pantuso
parent 51d2c56be3
commit ca8677e26b
No known key found for this signature in database
GPG Key ID: B3D3D9630018779E
8 changed files with 311 additions and 0 deletions

View File

@ -1045,6 +1045,31 @@
}
]
},
"mise": {
"default": {
"detect_extensions": [],
"detect_files": [
"mise.toml",
"mise.local.toml",
".mise.toml",
".mise.local.toml"
],
"detect_folders": [
".mise"
],
"disabled": true,
"format": "[$symbol$health]($style) ",
"healthy_symbol": "healthy",
"style": "bold purple",
"symbol": "mise ",
"unhealthy_symbol": "unhealthy"
},
"allOf": [
{
"$ref": "#/definitions/MiseConfig"
}
]
},
"mojo": {
"default": {
"detect_extensions": [
@ -4514,6 +4539,64 @@
},
"additionalProperties": false
},
"MiseConfig": {
"type": "object",
"properties": {
"format": {
"default": "[$symbol$health]($style) ",
"type": "string"
},
"symbol": {
"default": "mise ",
"type": "string"
},
"style": {
"default": "bold purple",
"type": "string"
},
"disabled": {
"default": true,
"type": "boolean"
},
"detect_extensions": {
"default": [],
"type": "array",
"items": {
"type": "string"
}
},
"detect_files": {
"default": [
"mise.toml",
"mise.local.toml",
".mise.toml",
".mise.local.toml"
],
"type": "array",
"items": {
"type": "string"
}
},
"detect_folders": {
"default": [
".mise"
],
"type": "array",
"items": {
"type": "string"
}
},
"healthy_symbol": {
"default": "healthy",
"type": "string"
},
"unhealthy_symbol": {
"default": "unhealthy",
"type": "string"
}
},
"additionalProperties": false
},
"MojoConfig": {
"type": "object",
"properties": {

View File

@ -342,6 +342,7 @@ $azure\
$nats\
$direnv\
$env_var\
$mise\
$crystal\
$custom\
$sudo\
@ -2945,6 +2946,40 @@ truncation_length = 4
truncation_symbol = ''
```
## Mise
The `mise` module shows the current mise health as reported by running `mise doctor`.
### Options
| Option | Default | Description |
| ------------------ | -------------------------------- | ------------------------------------------------ |
| `symbol` | `'mise '` | The symbol used before displaying _mise_ health. |
| `style` | `'bold purple'` | The style for the module. |
| `format` | `'on [$symbol$health]($style) '` | The format for the module. |
| `healthy_symbol` | `healthy` | The message displayed when _mise_ is healthy. |
| `unhealthy_symbol` | `unhealthy` | The message displayed when _mise_ is unhealthy. |
| `disabled` | `true` | Disables the `mise` module. |
### Variables
| Variable | Example | Description |
| -------- | --------- | ------------------------------------ |
| health | `healthy` | The health of _mise_ |
| 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
[mise]
health = 'ready'
```
## Mojo
The `mojo` module shows the current version of [Mojo programming language](https://www.modular.com/mojo) installed

41
src/configs/mise.rs Executable file
View File

@ -0,0 +1,41 @@
use serde::{Deserialize, Serialize};
#[derive(Clone, Deserialize, Serialize)]
#[cfg_attr(
feature = "config-schema",
derive(schemars::JsonSchema),
schemars(deny_unknown_fields)
)]
#[serde(default)]
pub struct MiseConfig<'a> {
pub format: &'a str,
pub symbol: &'a str,
pub style: &'a str,
pub disabled: bool,
pub detect_extensions: Vec<&'a str>,
pub detect_files: Vec<&'a str>,
pub detect_folders: Vec<&'a str>,
pub healthy_symbol: &'a str,
pub unhealthy_symbol: &'a str,
}
impl<'a> Default for MiseConfig<'a> {
fn default() -> Self {
Self {
format: "[$symbol$health]($style) ",
symbol: "mise ",
style: "bold purple",
disabled: true,
detect_extensions: vec![],
detect_files: vec![
"mise.toml",
"mise.local.toml",
".mise.toml",
".mise.local.toml",
],
detect_folders: vec![".mise"],
healthy_symbol: "healthy",
unhealthy_symbol: "unhealthy",
}
}
}

View File

@ -55,6 +55,7 @@ pub mod localip;
pub mod lua;
pub mod memory_usage;
pub mod meson;
pub mod mise;
pub mod mojo;
pub mod nats;
pub mod nim;
@ -220,6 +221,8 @@ pub struct FullConfig<'a> {
#[serde(borrow)]
meson: meson::MesonConfig<'a>,
#[serde(borrow)]
mise: mise::MiseConfig<'a>,
#[serde(borrow)]
mojo: mojo::MojoConfig<'a>,
#[serde(borrow)]
nats: nats::NatsConfig<'a>,

View File

@ -114,6 +114,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"azure",
"direnv",
"env_var",
"mise",
"crystal",
"custom",
"sudo",

View File

@ -60,6 +60,7 @@ pub const ALL_MODULES: &[&str] = &[
"lua",
"memory_usage",
"meson",
"mise",
"mojo",
"nats",
"nim",

144
src/modules/mise.rs Normal file
View File

@ -0,0 +1,144 @@
use super::{Context, Module, ModuleConfig};
use crate::configs::mise::MiseConfig;
use crate::formatter::StringFormatter;
/// Creates a module with the current mise config
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("mise");
let config = MiseConfig::try_load(module.config);
let mise_applies = !config.disabled
&& context
.try_begin_scan()?
.set_extensions(&config.detect_extensions)
.set_files(&config.detect_files)
.set_folders(&config.detect_folders)
.is_match();
if !mise_applies {
return None;
}
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter
.map_style(|variable| match variable {
"style" => Some(Ok(config.style)),
_ => None,
})
.map(|variable| match variable {
"symbol" => Some(Ok(config.symbol)),
"health" => match context.exec_cmd("mise", &["doctor"]) {
Some(_) => Some(Ok(config.healthy_symbol)),
None => Some(Ok(config.unhealthy_symbol)),
},
_ => None,
})
.parse(None, Some(context))
});
module.set_segments(match parsed {
Ok(segments) => segments,
Err(e) => {
log::warn!("{e}");
return None;
}
});
Some(module)
}
#[cfg(test)]
mod tests {
use crate::test::ModuleRenderer;
use crate::utils::CommandOutput;
use nu_ansi_term::Color;
use std::io;
#[test]
fn folder_without_mise_config() {
let renderer = ModuleRenderer::new("mise").config(toml::toml! {
[mise]
disabled = false
});
assert_eq!(None, renderer.collect());
}
#[test]
fn folder_with_mise_config_file_healthy() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let config_path = dir.path().join(".mise.toml");
std::fs::File::create(config_path)?.sync_all()?;
let renderer = ModuleRenderer::new("mise")
.path(dir.path())
.config(toml::toml! {
[mise]
disabled = false
})
.cmd(
"mise doctor",
Some(CommandOutput {
stdout: String::default(),
stderr: String::default(),
}),
);
let expected = Some(format!("{} ", Color::Purple.bold().paint("mise healthy")));
assert_eq!(expected, renderer.collect());
dir.close()
}
#[test]
fn folder_with_mise_config_folder_healthy() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let config_dir = dir.path().join(".mise");
std::fs::create_dir_all(config_dir)?;
let renderer = ModuleRenderer::new("mise")
.path(dir.path())
.config(toml::toml! {
[mise]
disabled = false
})
.cmd(
"mise doctor",
Some(CommandOutput {
stdout: String::default(),
stderr: String::default(),
}),
);
let expected = Some(format!("{} ", Color::Purple.bold().paint("mise healthy")));
assert_eq!(expected, renderer.collect());
dir.close()
}
#[test]
fn folder_with_mise_config_file_unhealthy() -> io::Result<()> {
let dir = tempfile::tempdir()?;
let config_path = dir.path().join(".mise.toml");
std::fs::File::create(config_path)?.sync_all()?;
let renderer = ModuleRenderer::new("mise")
.path(dir.path())
.config(toml::toml! {
[mise]
disabled = false
})
.cmd("mise doctor", None);
let expected = Some(format!("{} ", Color::Purple.bold().paint("mise unhealthy")));
assert_eq!(expected, renderer.collect());
dir.close()
}
}

View File

@ -52,6 +52,7 @@ mod localip;
mod lua;
mod memory_usage;
mod meson;
mod mise;
mod mojo;
mod nats;
mod nim;
@ -164,6 +165,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"lua" => lua::module(context),
"memory_usage" => memory_usage::module(context),
"meson" => meson::module(context),
"mise" => mise::module(context),
"mojo" => mojo::module(context),
"nats" => nats::module(context),
"nim" => nim::module(context),
@ -289,6 +291,7 @@ pub fn description(module: &str) -> &'static str {
"meson" => {
"The current Meson environment, if $MESON_DEVENV and $MESON_PROJECT_NAME are set"
}
"mise" => "The current mise status",
"mojo" => "The currently installed version of Mojo",
"nats" => "The current NATS context",
"nim" => "The currently installed version of Nim",