1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-26 03:27:30 +00:00

feat(vcsh): Add new module for VCSH (#2513)

* feat(vcsh): Implement new VCSH module

* test(vcsh): Add unit tests for VCSH module

* docs(vcsh): Document VCSH module
This commit is contained in:
Caleb Maclennan 2021-03-28 18:48:15 +03:00 committed by GitHub
parent 404b4f3d0c
commit f13e44c730
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 136 additions and 0 deletions

View File

@ -189,6 +189,7 @@ $hostname\
$shlvl\ $shlvl\
$kubernetes\ $kubernetes\
$directory\ $directory\
$vcsh\
$git_branch\ $git_branch\
$git_commit\ $git_commit\
$git_state\ $git_state\
@ -2717,6 +2718,39 @@ By default the module will be shown if any of the following conditions are met:
format = "via [⍱ $version](bold white) " format = "via [⍱ $version](bold white) "
``` ```
## VCSH
The `vcsh` module displays the current active VCSH repository.
The module will be shown only if a repository is currently in use.
### Options
| Option | Default | Description |
| ---------- | -------------------------------- | -------------------------------------------------------|
| `symbol` | | The symbol used before displaying the repository name. |
| `style` | `"bold yellow"` | The style for the module. |
| `format` | `"vcsh [$symbol$repo]($style) "` | The format for the module. |
| `disabled` | `false` | Disables the `vcsh` module. |
### Variables
| Variable | Example | Description |
| -------- | ------------------------------------------- | -------------------------------------|
| repo | `dotfiles` if in a VCSH repo named dotfiles | The active repository name |
| symbol | | Mirrors the value of option `symbol` |
| style\* | `black bold dimmed` | Mirrors the value of option `style` |
\*: This variable can only be used as a part of a style string
### Example
```toml
# ~/.config/starship.toml
[vcsh]
format = "[🆅 $repo](bold blue) "
```
## Zig ## Zig
By default the the `zig` module shows the currently installed version of Zig. By default the the `zig` module shows the currently installed version of Zig.

View File

@ -53,6 +53,7 @@ pub mod terraform;
pub mod time; pub mod time;
pub mod username; pub mod username;
pub mod vagrant; pub mod vagrant;
pub mod vcsh;
pub mod zig; pub mod zig;
pub use starship_root::*; pub use starship_root::*;

View File

@ -20,6 +20,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"singularity", "singularity",
"kubernetes", "kubernetes",
"directory", "directory",
"vcsh",
"git_branch", "git_branch",
"git_commit", "git_commit",
"git_state", "git_state",

22
src/configs/vcsh.rs Normal file
View File

@ -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,
}
}
}

View File

@ -63,6 +63,7 @@ pub const ALL_MODULES: &[&str] = &[
"status", "status",
"time", "time",
"username", "username",
"vcsh",
"vagrant", "vagrant",
"zig", "zig",
]; ];

View File

@ -54,6 +54,7 @@ mod time;
mod username; mod username;
mod utils; mod utils;
mod vagrant; mod vagrant;
mod vcsh;
mod zig; mod zig;
#[cfg(feature = "battery")] #[cfg(feature = "battery")]
@ -126,6 +127,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"crystal" => crystal::module(context), "crystal" => crystal::module(context),
"username" => username::module(context), "username" => username::module(context),
"vagrant" => vagrant::module(context), "vagrant" => vagrant::module(context),
"vcsh" => vcsh::module(context),
"zig" => zig::module(context), "zig" => zig::module(context),
_ => { _ => {
eprintln!("Error: Unknown module {}. Use starship module --list to list out all supported modules.", module); eprintln!("Error: Unknown module {}. Use starship module --list to list out all supported modules.", module);
@ -203,6 +205,7 @@ pub fn description(module: &str) -> &'static str {
"time" => "The current local time", "time" => "The current local time",
"username" => "The active user's username", "username" => "The active user's username",
"vagrant" => "The currently installed version of Vagrant", "vagrant" => "The currently installed version of Vagrant",
"vcsh" => "The currently active VCSH repository",
"zig" => "The currently installed version of Zig", "zig" => "The currently installed version of Zig",
_ => "<no description>", _ => "<no description>",
} }

74
src/modules/vcsh.rs Normal file
View File

@ -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);
}
}