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.
This commit is contained in:
Thomas O'Donnell 2021-08-15 21:30:58 +02:00 committed by GitHub
parent 7038ae2ec8
commit 9e5fcd1e14
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 27 deletions

View File

@ -3044,21 +3044,22 @@ By default the module will be shown if any of the following conditions are met:
### Options ### Options
| Option | Default | Description | | Option | Default | Description |
| ------------------- | -------------------------------------------- | -------------------------------------------- | | ------------------- | -------------------------------------------- | ------------------------------------------------------------------------- |
| `format` | `"via [$symbol($version )]($style)"` | The format for the module. | | `format` | `"via [$symbol($version )]($style)"` | The format for the module. |
| `symbol` | `"V "` | A format string representing the symbol of V | | `version_format` | `"v${raw}"` | The version format. Available vars are `raw`, `major`, `minor`, & `patch` |
| `detect_extensions` | `["v"]` | Which extensions should trigger this module. | | `symbol` | `"V "` | A format string representing the symbol of V |
| `detect_files` | `["v.mod", "vpkg.json", ".vpkg-lock.json" ]` | Which filenames should trigger this module. | | `detect_extensions` | `["v"]` | Which extensions should trigger this module. |
| `detect_folders` | `[]` | Which folders should trigger this module. | | `detect_files` | `["v.mod", "vpkg.json", ".vpkg-lock.json" ]` | Which filenames should trigger this module. |
| `style` | `"blue bold"` | The style for the module. | | `detect_folders` | `[]` | Which folders should trigger this module. |
| `disabled` | `false` | Disables the `vlang` module. | | `style` | `"blue bold"` | The style for the module. |
| `disabled` | `false` | Disables the `vlang` module. |
### Variables ### Variables
| Variable | Example | Description | | Variable | Example | Description |
| -------- | -------- | ------------------------------------ | | -------- | -------- | ------------------------------------ |
| version | `v0.2` | The version of `v` | | version | `v0.2` | The version of `v` |
| symbol | | Mirrors the value of option `symbol` | | symbol | | Mirrors the value of option `symbol` |
| style\* | | Mirrors the value of option `style` | | style\* | | Mirrors the value of option `style` |

View File

@ -6,6 +6,7 @@ use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig, Serialize)] #[derive(Clone, ModuleConfig, Serialize)]
pub struct RedConfig<'a> { pub struct RedConfig<'a> {
pub format: &'a str, pub format: &'a str,
pub version_format: &'a str,
pub symbol: &'a str, pub symbol: &'a str,
pub style: &'a str, pub style: &'a str,
pub disabled: bool, pub disabled: bool,
@ -18,6 +19,7 @@ impl<'a> Default for RedConfig<'a> {
fn default() -> Self { fn default() -> Self {
RedConfig { RedConfig {
format: "via [$symbol($version )]($style)", format: "via [$symbol($version )]($style)",
version_format: "v${raw}",
symbol: "🔺 ", symbol: "🔺 ",
style: "red bold", style: "red bold",
disabled: false, disabled: false,

View File

@ -6,6 +6,7 @@ use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig, Serialize)] #[derive(Clone, ModuleConfig, Serialize)]
pub struct VConfig<'a> { pub struct VConfig<'a> {
pub format: &'a str, pub format: &'a str,
pub version_format: &'a str,
pub symbol: &'a str, pub symbol: &'a str,
pub style: &'a str, pub style: &'a str,
pub disabled: bool, pub disabled: bool,
@ -18,6 +19,7 @@ impl<'a> Default for VConfig<'a> {
fn default() -> Self { fn default() -> Self {
VConfig { VConfig {
format: "via [$symbol($version )]($style)", format: "via [$symbol($version )]($style)",
version_format: "v${raw}",
symbol: "V ", symbol: "V ",
style: "blue bold", style: "blue bold",
disabled: false, disabled: false,

View File

@ -1,7 +1,7 @@
use super::{Context, Module, RootModuleConfig}; use super::{Context, Module, RootModuleConfig};
use crate::configs::red::RedConfig; use crate::configs::red::RedConfig;
use crate::formatter::StringFormatter; use crate::formatter::{StringFormatter, VersionFormatter};
/// Creates a module with the current Red version /// Creates a module with the current Red version
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
@ -31,7 +31,13 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
.map(|variable| match variable { .map(|variable| match variable {
"version" => context "version" => context
.exec_cmd("red", &["--version"]) .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), .map(Ok),
_ => None, _ => None,
}) })
@ -49,24 +55,13 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
Some(module) Some(module)
} }
fn parse_red_version(red_version: &str) -> String {
format!("v{}", red_version)
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::parse_red_version;
use crate::test::ModuleRenderer; use crate::test::ModuleRenderer;
use ansi_term::Color; use ansi_term::Color;
use std::fs::File; use std::fs::File;
use std::io; 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] #[test]
fn folder_without_red_files() -> io::Result<()> { fn folder_without_red_files() -> io::Result<()> {
let dir = tempfile::tempdir()?; let dir = tempfile::tempdir()?;
@ -95,4 +90,20 @@ mod tests {
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() 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()
}
} }

View File

@ -1,7 +1,7 @@
use super::{Context, Module, RootModuleConfig}; use super::{Context, Module, RootModuleConfig};
use crate::configs::v::VConfig; use crate::configs::v::VConfig;
use crate::formatter::StringFormatter; use crate::formatter::{StringFormatter, VersionFormatter};
/// Creates a module with the current V version /// Creates a module with the current V version
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
@ -31,7 +31,14 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
.map(|variable| match variable { .map(|variable| match variable {
"version" => context "version" => context
.exec_cmd("v", &["version"]) .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), .map(Ok),
_ => None, _ => None,
}) })
@ -51,12 +58,13 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
fn parse_v_version(v_version: &str) -> Option<String> { fn parse_v_version(v_version: &str) -> Option<String> {
let version = v_version let version = v_version
.trim()
// split into ["V", "0.2", "30c0659"] // split into ["V", "0.2", "30c0659"]
.split_whitespace() .split_whitespace()
// return "0.2" // return "0.2"
.nth(1)?; .nth(1)?;
Some(format!("v{}", version)) Some(version.to_owned())
} }
#[cfg(test)] #[cfg(test)]
@ -70,7 +78,7 @@ mod tests {
#[test] #[test]
fn test_parse_v_version() { fn test_parse_v_version() {
const OUTPUT: &str = "V 0.2 30c0659\n"; 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] #[test]
@ -121,4 +129,20 @@ mod tests {
assert_eq!(expected, actual); assert_eq!(expected, actual);
dir.close() 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()
}
} }