From 9e5fcd1e140af12531f24b790a9372d5e48a8231 Mon Sep 17 00:00:00 2001 From: Thomas O'Donnell Date: Sun, 15 Aug 2021 21:30:58 +0200 Subject: [PATCH] feat(red,vlang): Add version formatting (#2987) Have added version formatting to the red and vlang modules. Note the docs for red already mentioned the `version_format` string but it had not actually been added. --- docs/config/README.md | 21 +++++++++++---------- src/configs/red.rs | 2 ++ src/configs/v.rs | 2 ++ src/modules/red.rs | 37 ++++++++++++++++++++++++------------- src/modules/vlang.rs | 32 ++++++++++++++++++++++++++++---- 5 files changed, 67 insertions(+), 27 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index cce6a4e1..b5ed73ec 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -3044,21 +3044,22 @@ By default the module will be shown if any of the following conditions are met: ### Options -| Option | Default | Description | -| ------------------- | -------------------------------------------- | -------------------------------------------- | -| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | -| `symbol` | `"V "` | A format string representing the symbol of V | -| `detect_extensions` | `["v"]` | Which extensions should trigger this module. | -| `detect_files` | `["v.mod", "vpkg.json", ".vpkg-lock.json" ]` | Which filenames should trigger this module. | -| `detect_folders` | `[]` | Which folders should trigger this module. | -| `style` | `"blue bold"` | The style for the module. | -| `disabled` | `false` | Disables the `vlang` module. | +| Option | Default | Description | +| ------------------- | -------------------------------------------- | ------------------------------------------------------------------------- | +| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | +| `version_format` | `"v${raw}"` | The version format. Available vars are `raw`, `major`, `minor`, & `patch` | +| `symbol` | `"V "` | A format string representing the symbol of V | +| `detect_extensions` | `["v"]` | Which extensions should trigger this module. | +| `detect_files` | `["v.mod", "vpkg.json", ".vpkg-lock.json" ]` | Which filenames should trigger this module. | +| `detect_folders` | `[]` | Which folders should trigger this module. | +| `style` | `"blue bold"` | The style for the module. | +| `disabled` | `false` | Disables the `vlang` module. | ### Variables | Variable | Example | Description | | -------- | -------- | ------------------------------------ | -| version | `v0.2` | The version of `v` | +| version | `v0.2` | The version of `v` | | symbol | | Mirrors the value of option `symbol` | | style\* | | Mirrors the value of option `style` | diff --git a/src/configs/red.rs b/src/configs/red.rs index 2f18bc95..ba2a9bca 100644 --- a/src/configs/red.rs +++ b/src/configs/red.rs @@ -6,6 +6,7 @@ use starship_module_config_derive::ModuleConfig; #[derive(Clone, ModuleConfig, Serialize)] pub struct RedConfig<'a> { pub format: &'a str, + pub version_format: &'a str, pub symbol: &'a str, pub style: &'a str, pub disabled: bool, @@ -18,6 +19,7 @@ impl<'a> Default for RedConfig<'a> { fn default() -> Self { RedConfig { format: "via [$symbol($version )]($style)", + version_format: "v${raw}", symbol: "🔺 ", style: "red bold", disabled: false, diff --git a/src/configs/v.rs b/src/configs/v.rs index d49bad7f..87316ef7 100644 --- a/src/configs/v.rs +++ b/src/configs/v.rs @@ -6,6 +6,7 @@ use starship_module_config_derive::ModuleConfig; #[derive(Clone, ModuleConfig, Serialize)] pub struct VConfig<'a> { pub format: &'a str, + pub version_format: &'a str, pub symbol: &'a str, pub style: &'a str, pub disabled: bool, @@ -18,6 +19,7 @@ impl<'a> Default for VConfig<'a> { fn default() -> Self { VConfig { format: "via [$symbol($version )]($style)", + version_format: "v${raw}", symbol: "V ", style: "blue bold", disabled: false, diff --git a/src/modules/red.rs b/src/modules/red.rs index 2494b50f..3b6ca4ac 100644 --- a/src/modules/red.rs +++ b/src/modules/red.rs @@ -1,7 +1,7 @@ use super::{Context, Module, RootModuleConfig}; use crate::configs::red::RedConfig; -use crate::formatter::StringFormatter; +use crate::formatter::{StringFormatter, VersionFormatter}; /// Creates a module with the current Red version pub fn module<'a>(context: &'a Context) -> Option> { @@ -31,7 +31,13 @@ pub fn module<'a>(context: &'a Context) -> Option> { .map(|variable| match variable { "version" => context .exec_cmd("red", &["--version"]) - .map(|output| parse_red_version(output.stdout.trim())) + .map(|output| { + VersionFormatter::format_module_version( + module.get_name(), + output.stdout.trim(), + config.version_format, + ) + })? .map(Ok), _ => None, }) @@ -49,24 +55,13 @@ pub fn module<'a>(context: &'a Context) -> Option> { Some(module) } -fn parse_red_version(red_version: &str) -> String { - format!("v{}", red_version) -} - #[cfg(test)] mod tests { - use super::parse_red_version; use crate::test::ModuleRenderer; use ansi_term::Color; use std::fs::File; use std::io; - #[test] - fn test_parse_red_version() { - const OUTPUT: &str = "0.6.4\n"; - assert_eq!(parse_red_version(OUTPUT.trim()), "v0.6.4".to_string()) - } - #[test] fn folder_without_red_files() -> io::Result<()> { let dir = tempfile::tempdir()?; @@ -95,4 +90,20 @@ mod tests { assert_eq!(expected, actual); dir.close() } + + #[test] + fn version_formatting() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("hello.red"))?.sync_all()?; + let actual = ModuleRenderer::new("red") + .path(dir.path()) + .config(toml::toml! { + [red] + version_format = "${raw}" + }) + .collect(); + let expected = Some(format!("via {}", Color::Red.bold().paint("🔺 0.6.4 "))); + assert_eq!(expected, actual); + dir.close() + } } diff --git a/src/modules/vlang.rs b/src/modules/vlang.rs index f516c711..b95ab841 100644 --- a/src/modules/vlang.rs +++ b/src/modules/vlang.rs @@ -1,7 +1,7 @@ use super::{Context, Module, RootModuleConfig}; use crate::configs::v::VConfig; -use crate::formatter::StringFormatter; +use crate::formatter::{StringFormatter, VersionFormatter}; /// Creates a module with the current V version pub fn module<'a>(context: &'a Context) -> Option> { @@ -31,7 +31,14 @@ pub fn module<'a>(context: &'a Context) -> Option> { .map(|variable| match variable { "version" => context .exec_cmd("v", &["version"]) - .and_then(|output| parse_v_version(output.stdout.trim())) + .map(|output| parse_v_version(&output.stdout))? + .map(|output| { + VersionFormatter::format_module_version( + module.get_name(), + &output, + config.version_format, + ) + })? .map(Ok), _ => None, }) @@ -51,12 +58,13 @@ pub fn module<'a>(context: &'a Context) -> Option> { fn parse_v_version(v_version: &str) -> Option { let version = v_version + .trim() // split into ["V", "0.2", "30c0659"] .split_whitespace() // return "0.2" .nth(1)?; - Some(format!("v{}", version)) + Some(version.to_owned()) } #[cfg(test)] @@ -70,7 +78,7 @@ mod tests { #[test] fn test_parse_v_version() { const OUTPUT: &str = "V 0.2 30c0659\n"; - assert_eq!(parse_v_version(OUTPUT.trim()), Some("v0.2".to_string())) + assert_eq!(parse_v_version(OUTPUT), Some("0.2".to_string())) } #[test] @@ -121,4 +129,20 @@ mod tests { assert_eq!(expected, actual); dir.close() } + + #[test] + fn version_formatting() -> io::Result<()> { + let dir = tempfile::tempdir()?; + File::create(dir.path().join("hello.v"))?.sync_all()?; + let actual = ModuleRenderer::new("vlang") + .path(dir.path()) + .config(toml::toml! { + [vlang] + version_format = "${raw}" + }) + .collect(); + let expected = Some(format!("via {}", Color::Blue.bold().paint("V 0.2 "))); + assert_eq!(expected, actual); + dir.close() + } }