-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: added mise module #5747
Open
Ajpantuso
wants to merge
1
commit into
starship:master
Choose a base branch
from
Ajpantuso:apantuso/mise_module
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+304
−0
Open
feat: added mise module #5747
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
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"], | ||
detect_folders: vec![".mise"], | ||
healthy_symbol: "healthy", | ||
unhealthy_symbol: "unhealthy", | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mise also supports
.config/mise.toml
, among others: https://mise.jdx.dev/configuration.htmlNote in particular environment-specifig config files, which may be of particular interest here.