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(guix_shell): Initial implementation (starship#4397)
* feat(guix_shell): Initial implementation (starship#3999) * fix(guix_shell): Change guix nerd font icon to water buffalo emoji * fix(guix_shell): Added guix_shell entries in preset files * fix(guix_shell): Moved guix_shell config docs in to the correct place (alphabetically)
- Loading branch information
Thierry Delafontaine
authored
Oct 25, 2022
1 parent
c3cd499
commit d4bcc51
Showing
11 changed files
with
181 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 |
---|---|---|
|
@@ -31,6 +31,9 @@ symbol = " " | |
[golang] | ||
symbol = " " | ||
|
||
[guix_shell] | ||
symbol = " " | ||
|
||
[haskell] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,9 @@ symbol = "git " | |
[golang] | ||
symbol = "go " | ||
|
||
[guix_shell] | ||
symbol = "guix " | ||
|
||
[hg_branch] | ||
symbol = "hg " | ||
|
||
|
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,26 @@ | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Deserialize, Serialize)] | ||
#[cfg_attr( | ||
feature = "config-schema", | ||
derive(schemars::JsonSchema), | ||
schemars(deny_unknown_fields) | ||
)] | ||
#[serde(default)] | ||
pub struct GuixShellConfig<'a> { | ||
pub format: &'a str, | ||
pub symbol: &'a str, | ||
pub style: &'a str, | ||
pub disabled: bool, | ||
} | ||
|
||
impl<'a> Default for GuixShellConfig<'a> { | ||
fn default() -> Self { | ||
GuixShellConfig { | ||
format: "via [$symbol]($style) ", | ||
symbol: "🐃 ", | ||
style: "yellow 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,69 @@ | ||
use super::{Context, Module, ModuleConfig}; | ||
|
||
use crate::configs::guix_shell::GuixShellConfig; | ||
use crate::formatter::StringFormatter; | ||
|
||
/// Creates a module showing if inside a guix-shell | ||
/// | ||
/// The module will use the `$GUIX_ENVIRONMENT` environment variable to determine if it's | ||
/// inside a guix-shell and the name of it. | ||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { | ||
let mut module = context.new_module("guix_shell"); | ||
let config: GuixShellConfig = GuixShellConfig::try_load(module.config); | ||
|
||
let is_guix_shell = context.get_env("GUIX_ENVIRONMENT").is_some(); | ||
|
||
if !is_guix_shell { | ||
return None; | ||
}; | ||
|
||
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, | ||
}) | ||
.parse(None, Some(context)) | ||
}); | ||
|
||
module.set_segments(match parsed { | ||
Ok(segments) => segments, | ||
Err(error) => { | ||
log::warn!("Error in module `guix_shell`:\n{}", error); | ||
return None; | ||
} | ||
}); | ||
|
||
Some(module) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::test::ModuleRenderer; | ||
use nu_ansi_term::Color; | ||
|
||
#[test] | ||
fn no_env_variables() { | ||
let actual = ModuleRenderer::new("guix_shell").collect(); | ||
let expected = None; | ||
|
||
assert_eq!(expected, actual); | ||
} | ||
|
||
#[test] | ||
fn env_variables() { | ||
let actual = ModuleRenderer::new("guix_shell") | ||
.env( | ||
"GUIX_ENVIRONMENT", | ||
"/gnu/store/7vmfs4khf4fllsh83kqkxssbw3437qsh-profile", | ||
) | ||
.collect(); | ||
let expected = Some(format!("via {} ", Color::Yellow.bold().paint("🐃 "))); | ||
|
||
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