Skip to content

Commit

Permalink
feat(vcsh): Add new module for VCSH (starship#2513)
Browse files Browse the repository at this point in the history
* feat(vcsh): Implement new VCSH module

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

* docs(vcsh): Document VCSH module
  • Loading branch information
alerque authored Mar 28, 2021
1 parent 404b4f3 commit f13e44c
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 0 deletions.
34 changes: 34 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ $hostname\
$shlvl\
$kubernetes\
$directory\
$vcsh\
$git_branch\
$git_commit\
$git_state\
Expand Down Expand Up @@ -2717,6 +2718,39 @@ By default the module will be shown if any of the following conditions are met:
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

By default the the `zig` module shows the currently installed version of Zig.
Expand Down
1 change: 1 addition & 0 deletions src/configs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub mod terraform;
pub mod time;
pub mod username;
pub mod vagrant;
pub mod vcsh;
pub mod zig;

pub use starship_root::*;
1 change: 1 addition & 0 deletions src/configs/starship_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"singularity",
"kubernetes",
"directory",
"vcsh",
"git_branch",
"git_commit",
"git_state",
Expand Down
22 changes: 22 additions & 0 deletions src/configs/vcsh.rs
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,
}
}
}
1 change: 1 addition & 0 deletions src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub const ALL_MODULES: &[&str] = &[
"status",
"time",
"username",
"vcsh",
"vagrant",
"zig",
];
Expand Down
3 changes: 3 additions & 0 deletions src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ mod time;
mod username;
mod utils;
mod vagrant;
mod vcsh;
mod zig;

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

0 comments on commit f13e44c

Please sign in to comment.