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(vcsh): Add new module for VCSH (starship#2513)
* feat(vcsh): Implement new VCSH module * test(vcsh): Add unit tests for VCSH module * docs(vcsh): Document VCSH module
- Loading branch information
Showing
7 changed files
with
136 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
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,22 @@ | ||
use crate::config::ModuleConfig; | ||
|
||
use starship_module_config_derive::ModuleConfig; | ||
|
||
#[derive(Clone, ModuleConfig)] | ||
pub struct VcshConfig<'a> { | ||
pub symbol: &'a str, | ||
pub style: &'a str, | ||
pub format: &'a str, | ||
pub disabled: bool, | ||
} | ||
|
||
impl<'a> Default for VcshConfig<'a> { | ||
fn default() -> Self { | ||
VcshConfig { | ||
symbol: "", | ||
style: "bold yellow", | ||
format: "vcsh [$symbol$repo]($style) ", | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,7 @@ pub const ALL_MODULES: &[&str] = &[ | |
"status", | ||
"time", | ||
"username", | ||
"vcsh", | ||
"vagrant", | ||
"zig", | ||
]; | ||
|
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,74 @@ | ||
use super::{Context, Module}; | ||
|
||
use crate::config::RootModuleConfig; | ||
use crate::configs::vcsh::VcshConfig; | ||
use crate::formatter::StringFormatter; | ||
|
||
/// Creates a module that displays VCSH repository currently in use | ||
/// | ||
/// Will display the name of the current VCSH repository if one is active. | ||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { | ||
let repo = context.get_env("VCSH_REPO_NAME").unwrap_or_default(); | ||
if repo.trim().is_empty() { | ||
return None; | ||
} | ||
|
||
let mut module = context.new_module("vcsh"); | ||
let config: VcshConfig = VcshConfig::try_load(module.config); | ||
|
||
let parsed = StringFormatter::new(config.format).and_then(|formatter| { | ||
formatter | ||
.map_meta(|var, _| match var { | ||
"symbol" => Some(config.symbol), | ||
_ => None, | ||
}) | ||
.map_style(|variable| match variable { | ||
"style" => Some(Ok(config.style)), | ||
_ => None, | ||
}) | ||
.map(|variable| match variable { | ||
"repo" => Some(Ok(&repo)), | ||
_ => None, | ||
}) | ||
.parse(None) | ||
}); | ||
|
||
module.set_segments(match parsed { | ||
Ok(segments) => segments, | ||
Err(error) => { | ||
log::warn!("Error in module `vcsh`:\n{}", error); | ||
return None; | ||
} | ||
}); | ||
|
||
Some(module) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::test::ModuleRenderer; | ||
use ansi_term::Color; | ||
|
||
#[test] | ||
fn not_in_env() { | ||
let actual = ModuleRenderer::new("vcsh").collect(); | ||
|
||
let expected = None; | ||
|
||
assert_eq!(expected, actual); | ||
} | ||
|
||
#[test] | ||
fn env_set() { | ||
let actual = ModuleRenderer::new("vcsh") | ||
.env("VCSH_REPO_NAME", "astronauts") | ||
.collect(); | ||
|
||
let expected = Some(format!( | ||
"vcsh {} ", | ||
Color::Yellow.bold().paint("astronauts") | ||
)); | ||
|
||
assert_eq!(expected, actual); | ||
} | ||
} |