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

@ -3045,8 +3045,9 @@ 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. |
| `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. |

View File

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

View File

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

View File

@ -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<Module<'a>> {
@ -31,7 +31,13 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
.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<Module<'a>> {
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()
}
}

View File

@ -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<Module<'a>> {
@ -31,7 +31,14 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
.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<Module<'a>> {
fn parse_v_version(v_version: &str) -> Option<String> {
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()
}
}