forked from starship/starship
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(module): Add a meson devenv indicator (starship#4389)
* 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
1 parent
4107031
commit 355800f
Showing
14 changed files
with
346 additions
and
0 deletions.
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 |
---|---|---|
|
@@ -49,6 +49,9 @@ symbol = " " | |
[memory_usage] | ||
symbol = " " | ||
|
||
[meson] | ||
symbol = "喝 " | ||
|
||
[nim] | ||
symbol = " " | ||
|
||
|
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,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, | ||
} | ||
} | ||
} |
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,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); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -7,3 +7,5 @@ pub mod directory_win; | |
pub mod directory_nix; | ||
|
||
pub mod path; | ||
|
||
pub mod truncate; |
Oops, something went wrong.