mirror of
https://github.com/starship/starship.git
synced 2026-06-23 02:05:51 +07:00
feat: add version format configuration (#2499)
This commit is contained in:
@@ -7,6 +7,7 @@ use starship_module_config_derive::ModuleConfig;
|
||||
pub struct JavaConfig<'a> {
|
||||
pub disabled: bool,
|
||||
pub format: &'a str,
|
||||
pub version_format: &'a str,
|
||||
pub style: &'a str,
|
||||
pub symbol: &'a str,
|
||||
pub detect_extensions: Vec<&'a str>,
|
||||
@@ -18,6 +19,7 @@ impl<'a> Default for JavaConfig<'a> {
|
||||
fn default() -> Self {
|
||||
JavaConfig {
|
||||
format: "via [$symbol($version )]($style)",
|
||||
version_format: "v${raw}",
|
||||
disabled: false,
|
||||
style: "red dimmed",
|
||||
symbol: "☕ ",
|
||||
|
||||
@@ -6,6 +6,7 @@ use starship_module_config_derive::ModuleConfig;
|
||||
#[derive(Clone, ModuleConfig, Serialize)]
|
||||
pub struct NodejsConfig<'a> {
|
||||
pub format: &'a str,
|
||||
pub version_format: &'a str,
|
||||
pub symbol: &'a str,
|
||||
pub style: &'a str,
|
||||
pub disabled: bool,
|
||||
@@ -19,6 +20,7 @@ impl<'a> Default for NodejsConfig<'a> {
|
||||
fn default() -> Self {
|
||||
NodejsConfig {
|
||||
format: "via [$symbol($version )]($style)",
|
||||
version_format: "v${raw}",
|
||||
symbol: " ",
|
||||
style: "bold green",
|
||||
disabled: false,
|
||||
|
||||
@@ -9,6 +9,7 @@ pub struct PythonConfig<'a> {
|
||||
pub pyenv_prefix: &'a str,
|
||||
pub python_binary: VecOr<&'a str>,
|
||||
pub format: &'a str,
|
||||
pub version_format: &'a str,
|
||||
pub style: &'a str,
|
||||
pub symbol: &'a str,
|
||||
pub disabled: bool,
|
||||
@@ -24,6 +25,7 @@ impl<'a> Default for PythonConfig<'a> {
|
||||
pyenv_prefix: "pyenv ",
|
||||
python_binary: VecOr(vec!["python", "python3", "python2"]),
|
||||
format: "via [${symbol}${pyenv_prefix}(${version} )(\\($virtualenv\\) )]($style)",
|
||||
version_format: "v${raw}",
|
||||
style: "yellow bold",
|
||||
symbol: "🐍 ",
|
||||
disabled: false,
|
||||
|
||||
@@ -6,6 +6,7 @@ use starship_module_config_derive::ModuleConfig;
|
||||
#[derive(Clone, ModuleConfig, Serialize)]
|
||||
pub struct RubyConfig<'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 RubyConfig<'a> {
|
||||
fn default() -> Self {
|
||||
RubyConfig {
|
||||
format: "via [$symbol($version )]($style)",
|
||||
version_format: "v${raw}",
|
||||
symbol: "💎 ",
|
||||
style: "bold red",
|
||||
disabled: false,
|
||||
|
||||
@@ -6,6 +6,7 @@ use starship_module_config_derive::ModuleConfig;
|
||||
#[derive(Clone, ModuleConfig, Serialize)]
|
||||
pub struct RustConfig<'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 RustConfig<'a> {
|
||||
fn default() -> Self {
|
||||
RustConfig {
|
||||
format: "via [$symbol($version )]($style)",
|
||||
version_format: "v${raw}",
|
||||
symbol: "🦀 ",
|
||||
style: "bold red",
|
||||
disabled: false,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
pub mod model;
|
||||
mod parser;
|
||||
pub mod string_formatter;
|
||||
mod version;
|
||||
|
||||
pub use model::{StyleVariableHolder, VariableHolder};
|
||||
pub use string_formatter::StringFormatter;
|
||||
pub use version::VersionFormatter;
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
use super::string_formatter::StringFormatterError;
|
||||
use super::StringFormatter;
|
||||
use versions::Versioning;
|
||||
|
||||
pub struct VersionFormatter<'a> {
|
||||
formatter: StringFormatter<'a>,
|
||||
}
|
||||
|
||||
impl<'a> VersionFormatter<'a> {
|
||||
/// Creates an instance of a VersionFormatter from a format string
|
||||
///
|
||||
/// Like the StringFormatter, this will throw an error when the string isn't
|
||||
/// parseable.
|
||||
pub fn new(format: &'a str) -> Result<Self, StringFormatterError> {
|
||||
let formatter = StringFormatter::new(format)?;
|
||||
|
||||
Ok(Self { formatter })
|
||||
}
|
||||
|
||||
/// Formats a version structure into a readable string
|
||||
///
|
||||
/// No matter what comes in, this will return some usable string
|
||||
pub fn format_version(self, version: &str) -> String {
|
||||
let parsed = Versioning::new(version);
|
||||
let formatted = self
|
||||
.formatter
|
||||
.map(|variable| match variable {
|
||||
"raw" => Some(Ok(version.to_string())),
|
||||
"major" => match parsed.as_ref() {
|
||||
Some(Versioning::Ideal(v)) => Some(Ok(v.major.to_string())),
|
||||
Some(Versioning::General(v)) => Some(Ok(v.nth_lenient(0)?.to_string())),
|
||||
_ => None,
|
||||
},
|
||||
"minor" => match parsed.as_ref() {
|
||||
Some(Versioning::Ideal(v)) => Some(Ok(v.minor.to_string())),
|
||||
Some(Versioning::General(v)) => Some(Ok(v.nth_lenient(1)?.to_string())),
|
||||
_ => None,
|
||||
},
|
||||
"patch" => match parsed.as_ref() {
|
||||
Some(Versioning::Ideal(v)) => Some(Ok(v.patch.to_string())),
|
||||
Some(Versioning::General(v)) => Some(Ok(v.nth_lenient(2)?.to_string())),
|
||||
_ => None,
|
||||
},
|
||||
_ => None,
|
||||
})
|
||||
.parse(None);
|
||||
match formatted {
|
||||
Ok(segments) => segments
|
||||
.iter()
|
||||
.map(|segment| segment.value.as_str())
|
||||
.collect::<Vec<&str>>()
|
||||
.join(""),
|
||||
Err(_) => version.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_semver_full() {
|
||||
const FORMAT_STR: &str = "major:${major} minor:${minor} patch:${patch} raw:${raw}";
|
||||
let result = VersionFormatter::new(FORMAT_STR)
|
||||
.unwrap()
|
||||
.format_version("1.2.3");
|
||||
assert_eq!(result, "major:1 minor:2 patch:3 raw:1.2.3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_semver_partial() {
|
||||
const FORMAT_STR: &str = "major:${major} minor:${minor} patch:${patch} raw:${raw}";
|
||||
let result = VersionFormatter::new(FORMAT_STR)
|
||||
.unwrap()
|
||||
.format_version("1.2");
|
||||
assert_eq!(result, "major:1 minor:2 patch: raw:1.2");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_general() {
|
||||
const FORMAT_STR: &str = "major:${major} minor:${minor} patch:${patch} raw:${raw}";
|
||||
let result = VersionFormatter::new(FORMAT_STR)
|
||||
.unwrap()
|
||||
.format_version("1.2-a.3");
|
||||
assert_eq!(result, "major:1 minor:2 patch: raw:1.2-a.3");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_mess() {
|
||||
const FORMAT_STR: &str = "major:${major} minor:${minor} patch:${patch} raw:${raw}";
|
||||
let result = VersionFormatter::new(FORMAT_STR)
|
||||
.unwrap()
|
||||
.format_version("utter junk");
|
||||
assert_eq!(result, "major: minor: patch: raw:utter junk");
|
||||
}
|
||||
}
|
||||
+63
-20
@@ -1,5 +1,5 @@
|
||||
use crate::configs::java::JavaConfig;
|
||||
use crate::formatter::StringFormatter;
|
||||
use crate::formatter::{StringFormatter, VersionFormatter};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use super::{Context, Module, RootModuleConfig};
|
||||
@@ -35,7 +35,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
})
|
||||
.map(|variable| match variable {
|
||||
"version" => {
|
||||
let java_version = get_java_version(context)?;
|
||||
let java_version = get_java_version(context, &config)?;
|
||||
Some(Ok(java_version))
|
||||
}
|
||||
_ => None,
|
||||
@@ -54,7 +54,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
Some(module)
|
||||
}
|
||||
|
||||
fn get_java_version(context: &Context) -> Option<String> {
|
||||
fn get_java_version(context: &Context, config: &JavaConfig) -> Option<String> {
|
||||
let java_command = context
|
||||
.get_env("JAVA_HOME")
|
||||
.map(PathBuf::from)
|
||||
@@ -74,15 +74,19 @@ fn get_java_version(context: &Context) -> Option<String> {
|
||||
output.stdout
|
||||
};
|
||||
|
||||
parse_java_version(&java_version)
|
||||
parse_java_version(config.version_format, &java_version)
|
||||
}
|
||||
|
||||
fn parse_java_version(java_version: &str) -> Option<String> {
|
||||
fn parse_java_version(version_format: &str, java_version: &str) -> Option<String> {
|
||||
let re = Regex::new(JAVA_VERSION_PATTERN).ok()?;
|
||||
let captures = re.captures(java_version)?;
|
||||
let version = &captures["version"];
|
||||
|
||||
Some(format!("v{}", &version))
|
||||
Some(
|
||||
VersionFormatter::new(version_format)
|
||||
.ok()?
|
||||
.format_version(version),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -97,64 +101,103 @@ mod tests {
|
||||
fn test_parse_java_version_openjdk() {
|
||||
let java_8 = "OpenJDK 64-Bit Server VM (25.222-b10) for linux-amd64 JRE (1.8.0_222-b10), built on Jul 11 2019 10:18:43 by \"openjdk\" with gcc 4.4.7 20120313 (Red Hat 4.4.7-23)";
|
||||
let java_11 = "OpenJDK 64-Bit Server VM (11.0.4+11-post-Ubuntu-1ubuntu219.04) for linux-amd64 JRE (11.0.4+11-post-Ubuntu-1ubuntu219.04), built on Jul 18 2019 18:21:46 by \"build\" with gcc 8.3.0";
|
||||
assert_eq!(parse_java_version(java_11), Some("v11.0.4".to_string()));
|
||||
assert_eq!(parse_java_version(java_8), Some("v1.8.0".to_string()));
|
||||
assert_eq!(
|
||||
parse_java_version("v${raw}", java_11),
|
||||
Some("v11.0.4".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
parse_java_version("v${raw}", java_8),
|
||||
Some("v1.8.0".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_java_version_oracle() {
|
||||
let java_8 = "Java HotSpot(TM) Client VM (25.65-b01) for linux-arm-vfp-hflt JRE (1.8.0_65-b17), built on Oct 6 2015 16:19:04 by \"java_re\" with gcc 4.7.2 20120910 (prerelease)";
|
||||
assert_eq!(parse_java_version(java_8), Some("v1.8.0".to_string()));
|
||||
assert_eq!(
|
||||
parse_java_version("v${raw}", java_8),
|
||||
Some("v1.8.0".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_java_version_redhat() {
|
||||
let java_8 = "OpenJDK 64-Bit Server VM (25.222-b10) for linux-amd64 JRE (1.8.0_222-b10), built on Jul 11 2019 20:48:53 by \"root\" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)";
|
||||
let java_12 = "OpenJDK 64-Bit Server VM (12.0.2+10) for linux-amd64 JRE (12.0.2+10), built on Jul 18 2019 14:41:47 by \"jenkins\" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)";
|
||||
assert_eq!(parse_java_version(java_8), Some("v1.8.0".to_string()));
|
||||
assert_eq!(parse_java_version(java_12), Some("v12.0.2".to_string()));
|
||||
assert_eq!(
|
||||
parse_java_version("v${raw}", java_8),
|
||||
Some("v1.8.0".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
parse_java_version("v${raw}", java_12),
|
||||
Some("v12.0.2".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_java_version_zulu() {
|
||||
let java_8 = "OpenJDK 64-Bit Server VM (25.222-b10) for linux-amd64 JRE (Zulu 8.40.0.25-CA-linux64) (1.8.0_222-b10), built on Jul 11 2019 11:36:39 by \"zulu_re\" with gcc 4.4.7 20120313 (Red Hat 4.4.7-3)";
|
||||
let java_11 = "OpenJDK 64-Bit Server VM (11.0.4+11-LTS) for linux-amd64 JRE (Zulu11.33+15-CA) (11.0.4+11-LTS), built on Jul 11 2019 21:37:17 by \"zulu_re\" with gcc 4.9.2 20150212 (Red Hat 4.9.2-6)";
|
||||
assert_eq!(parse_java_version(java_8), Some("v1.8.0".to_string()));
|
||||
assert_eq!(parse_java_version(java_11), Some("v11.0.4".to_string()));
|
||||
assert_eq!(
|
||||
parse_java_version("v${raw}", java_8),
|
||||
Some("v1.8.0".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
parse_java_version("v${raw}", java_11),
|
||||
Some("v11.0.4".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_java_version_eclipse_openj9() {
|
||||
let java_8 = "Eclipse OpenJ9 OpenJDK 64-bit Server VM (1.8.0_222-b10) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 8.0.222.0, built on Jul 17 2019 21:29:18 by jenkins with g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)";
|
||||
let java_11 = "Eclipse OpenJ9 OpenJDK 64-bit Server VM (11.0.4+11) from linux-amd64 JRE with Extensions for OpenJDK for Eclipse OpenJ9 11.0.4.0, built on Jul 17 2019 21:51:37 by jenkins with g++ (GCC) 7.3.1 20180303 (Red Hat 7.3.1-5)";
|
||||
assert_eq!(parse_java_version(java_8), Some("v1.8.0".to_string()));
|
||||
assert_eq!(parse_java_version(java_11), Some("v11.0.4".to_string()));
|
||||
assert_eq!(
|
||||
parse_java_version("v${raw}", java_8),
|
||||
Some("v1.8.0".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
parse_java_version("v${raw}", java_11),
|
||||
Some("v11.0.4".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_java_version_graalvm() {
|
||||
let java_8 = "OpenJDK 64-Bit GraalVM CE 19.2.0.1 (25.222-b08-jvmci-19.2-b02) for linux-amd64 JRE (8u222), built on Jul 19 2019 17:37:13 by \"buildslave\" with gcc 7.3.0";
|
||||
assert_eq!(parse_java_version(java_8), Some("v8".to_string()));
|
||||
assert_eq!(
|
||||
parse_java_version("v${raw}", java_8),
|
||||
Some("v8".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_java_version_amazon_corretto() {
|
||||
let java_8 = "OpenJDK 64-Bit Server VM (25.222-b10) for linux-amd64 JRE (1.8.0_222-b10), built on Jul 11 2019 20:48:53 by \"root\" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)";
|
||||
let java_11 = "OpenJDK 64-Bit Server VM (11.0.4+11-LTS) for linux-amd64 JRE (11.0.4+11-LTS), built on Jul 11 2019 20:06:11 by \"\" with gcc 7.3.1 20180303 (Red Hat 7.3.1-5)";
|
||||
assert_eq!(parse_java_version(java_8), Some("v1.8.0".to_string()));
|
||||
assert_eq!(parse_java_version(java_11), Some("v11.0.4".to_string()));
|
||||
assert_eq!(
|
||||
parse_java_version("v${raw}", java_8),
|
||||
Some("v1.8.0".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
parse_java_version("v${raw}", java_11),
|
||||
Some("v11.0.4".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_java_version_sapmachine() {
|
||||
let java_11 = "OpenJDK 64-Bit Server VM (11.0.4+11-LTS-sapmachine) for linux-amd64 JRE (11.0.4+11-LTS-sapmachine), built on Jul 17 2019 08:58:43 by \"\" with gcc 7.3.0";
|
||||
assert_eq!(parse_java_version(java_11), Some("v11.0.4".to_string()));
|
||||
assert_eq!(
|
||||
parse_java_version("v${raw}", java_11),
|
||||
Some("v11.0.4".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_java_version_unknown() {
|
||||
let unknown_jre = "Unknown JRE";
|
||||
assert_eq!(parse_java_version(unknown_jre), None);
|
||||
assert_eq!(parse_java_version("v${raw}", unknown_jre), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+13
-6
@@ -1,7 +1,7 @@
|
||||
use super::{Context, Module, RootModuleConfig};
|
||||
|
||||
use crate::configs::nodejs::NodejsConfig;
|
||||
use crate::formatter::StringFormatter;
|
||||
use crate::formatter::{StringFormatter, VersionFormatter};
|
||||
use crate::utils;
|
||||
|
||||
use once_cell::sync::Lazy;
|
||||
@@ -57,11 +57,18 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
_ => None,
|
||||
})
|
||||
.map(|variable| match variable {
|
||||
"version" => nodejs_version
|
||||
.deref()
|
||||
.as_ref()
|
||||
.map(|version| version.trim())
|
||||
.map(Ok),
|
||||
"version" => {
|
||||
let nodejs_detected_version = &nodejs_version
|
||||
.deref()
|
||||
.as_ref()
|
||||
.map(|version| version.trim());
|
||||
match nodejs_detected_version {
|
||||
Some(version) => Some(Ok(VersionFormatter::new(config.version_format)
|
||||
.ok()?
|
||||
.format_version(&version.to_string().drain(1..).collect::<String>()))),
|
||||
None => None,
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
})
|
||||
.parse(None)
|
||||
|
||||
+39
-7
@@ -4,6 +4,7 @@ use std::path::Path;
|
||||
use super::{Context, Module, RootModuleConfig};
|
||||
use crate::configs::python::PythonConfig;
|
||||
use crate::formatter::StringFormatter;
|
||||
use crate::formatter::VersionFormatter;
|
||||
|
||||
/// Creates a module with the current Python version and, if active, virtual environment.
|
||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
@@ -82,17 +83,21 @@ fn get_python_version(context: &Context, config: &PythonConfig) -> Option<String
|
||||
}
|
||||
})?;
|
||||
|
||||
format_python_version(&version)
|
||||
format_python_version(config.version_format, &version)
|
||||
}
|
||||
|
||||
fn format_python_version(python_version: &str) -> Option<String> {
|
||||
fn format_python_version(version_format: &str, python_version: &str) -> Option<String> {
|
||||
let version = python_version
|
||||
// split into ["Python", "3.8.6", ...]
|
||||
.split_whitespace()
|
||||
// return "3.8.6"
|
||||
// get down to "3.8.6"
|
||||
.nth(1)?;
|
||||
|
||||
Some(format!("v{}", version))
|
||||
Some(
|
||||
VersionFormatter::new(version_format)
|
||||
.ok()?
|
||||
.format_version(version),
|
||||
)
|
||||
}
|
||||
|
||||
fn get_python_virtual_env(context: &Context) -> Option<String> {
|
||||
@@ -124,19 +129,46 @@ mod tests {
|
||||
#[test]
|
||||
fn test_format_python_version() {
|
||||
let input = "Python 3.7.2";
|
||||
assert_eq!(format_python_version(input), Some("v3.7.2".to_string()));
|
||||
assert_eq!(
|
||||
format_python_version("v${major}.${minor}.${patch}", input),
|
||||
Some("v3.7.2".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_python_version_truncated() {
|
||||
let input = "Python 3.7.2";
|
||||
assert_eq!(
|
||||
format_python_version("v${major}.${minor}", input),
|
||||
Some("v3.7".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_python_version_is_malformed() {
|
||||
let input = "Python 3.7";
|
||||
assert_eq!(
|
||||
format_python_version("v${major}.${minor}.${patch}", input),
|
||||
Some("v3.7.".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_python_version_anaconda() {
|
||||
let input = "Python 3.6.10 :: Anaconda, Inc.";
|
||||
assert_eq!(format_python_version(input), Some("v3.6.10".to_string()));
|
||||
assert_eq!(
|
||||
format_python_version("v${major}.${minor}.${patch}", input),
|
||||
Some("v3.6.10".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_format_python_version_pypy() {
|
||||
let input = "Python 3.7.9 (7e6e2bb30ac5fbdbd443619cae28c51d5c162a02, Nov 24 2020, 10:03:59)\n[PyPy 7.3.3-beta0 with GCC 10.2.0]";
|
||||
assert_eq!(format_python_version(input), Some("v3.7.9".to_string()));
|
||||
assert_eq!(
|
||||
format_python_version("v${major}.${minor}.${patch}", input),
|
||||
Some("v3.7.9".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
+22
-11
@@ -1,7 +1,7 @@
|
||||
use super::{Context, Module, RootModuleConfig};
|
||||
|
||||
use crate::configs::ruby::RubyConfig;
|
||||
use crate::formatter::StringFormatter;
|
||||
use crate::formatter::{StringFormatter, VersionFormatter};
|
||||
|
||||
/// Creates a module with the current Ruby version
|
||||
///
|
||||
@@ -34,9 +34,11 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
_ => None,
|
||||
})
|
||||
.map(|variable| match variable {
|
||||
"version" => {
|
||||
format_ruby_version(&context.exec_cmd("ruby", &["-v"])?.stdout.as_str()).map(Ok)
|
||||
}
|
||||
"version" => format_ruby_version(
|
||||
&config,
|
||||
&context.exec_cmd("ruby", &["-v"])?.stdout.as_str(),
|
||||
)
|
||||
.map(Ok),
|
||||
_ => None,
|
||||
})
|
||||
.parse(None)
|
||||
@@ -53,7 +55,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
Some(module)
|
||||
}
|
||||
|
||||
fn format_ruby_version(ruby_version: &str) -> Option<String> {
|
||||
fn format_ruby_version(config: &RubyConfig, ruby_version: &str) -> Option<String> {
|
||||
let version = ruby_version
|
||||
// split into ["ruby", "2.6.0p0", "linux/amd64"]
|
||||
.split_whitespace()
|
||||
@@ -64,10 +66,11 @@ fn format_ruby_version(ruby_version: &str) -> Option<String> {
|
||||
// return "2.6.0"
|
||||
.next()?;
|
||||
|
||||
let mut formatted_version = String::with_capacity(version.len() + 1);
|
||||
formatted_version.push('v');
|
||||
formatted_version.push_str(version);
|
||||
Some(formatted_version)
|
||||
Some(
|
||||
VersionFormatter::new(config.version_format)
|
||||
.ok()?
|
||||
.format_version(version),
|
||||
)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -127,16 +130,24 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_format_ruby_version() {
|
||||
let test_config = RubyConfig::default();
|
||||
assert_eq!(
|
||||
format_ruby_version("ruby 2.1.10p492 (2016-04-01 revision 54464) [x86_64-darwin19.0]"),
|
||||
format_ruby_version(
|
||||
&test_config,
|
||||
"ruby 2.1.10p492 (2016-04-01 revision 54464) [x86_64-darwin19.0]"
|
||||
),
|
||||
Some("v2.1.10".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
format_ruby_version("ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]"),
|
||||
format_ruby_version(
|
||||
&test_config,
|
||||
"ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]"
|
||||
),
|
||||
Some("v2.5.1".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
format_ruby_version(
|
||||
&test_config,
|
||||
"ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-linux-musl]"
|
||||
),
|
||||
Some("v2.7.0".to_string())
|
||||
|
||||
+30
-13
@@ -7,7 +7,7 @@ use serde::Deserialize;
|
||||
use super::{Context, Module, RootModuleConfig};
|
||||
|
||||
use crate::configs::rust::RustConfig;
|
||||
use crate::formatter::StringFormatter;
|
||||
use crate::formatter::{StringFormatter, VersionFormatter};
|
||||
|
||||
/// Creates a module with the current Rust version
|
||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
@@ -38,7 +38,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
.map(|variable| match variable {
|
||||
// This may result in multiple calls to `get_module_version` when a user have
|
||||
// multiple `$version` variables defined in `format`.
|
||||
"version" => get_module_version(context).map(Ok),
|
||||
"version" => get_module_version(context, &config).map(Ok),
|
||||
_ => None,
|
||||
})
|
||||
.parse(None)
|
||||
@@ -55,7 +55,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
Some(module)
|
||||
}
|
||||
|
||||
fn get_module_version(context: &Context) -> Option<String> {
|
||||
fn get_module_version(context: &Context, config: &RustConfig) -> Option<String> {
|
||||
// `$CARGO_HOME/bin/rustc(.exe) --version` may attempt installing a rustup toolchain.
|
||||
// https://github.com/starship/starship/issues/417
|
||||
//
|
||||
@@ -77,17 +77,19 @@ fn get_module_version(context: &Context) -> Option<String> {
|
||||
.or_else(|| find_rust_toolchain_file(&context))
|
||||
{
|
||||
match execute_rustup_run_rustc_version(&toolchain) {
|
||||
RustupRunRustcVersionOutcome::RustcVersion(stdout) => format_rustc_version(stdout),
|
||||
RustupRunRustcVersionOutcome::RustcVersion(stdout) => {
|
||||
format_rustc_version(config, stdout)
|
||||
}
|
||||
RustupRunRustcVersionOutcome::ToolchainName(toolchain) => toolchain,
|
||||
RustupRunRustcVersionOutcome::RustupNotWorking => {
|
||||
// If `rustup` is not in `$PATH` or cannot be executed for other reasons, we can
|
||||
// safely execute `rustc --version`.
|
||||
format_rustc_version(execute_rustc_version()?)
|
||||
format_rustc_version(config, execute_rustc_version()?)
|
||||
}
|
||||
RustupRunRustcVersionOutcome::Err => return None,
|
||||
}
|
||||
} else {
|
||||
format_rustc_version(execute_rustc_version()?)
|
||||
format_rustc_version(config, execute_rustc_version()?)
|
||||
};
|
||||
|
||||
Some(module_version)
|
||||
@@ -203,11 +205,15 @@ fn execute_rustc_version() -> Option<String> {
|
||||
}
|
||||
}
|
||||
|
||||
fn format_rustc_version(mut rustc_stdout: String) -> String {
|
||||
fn format_rustc_version(config: &RustConfig, mut rustc_stdout: String) -> String {
|
||||
let offset = &rustc_stdout.find('(').unwrap_or_else(|| rustc_stdout.len());
|
||||
let formatted_version: String = rustc_stdout.drain(..offset).collect();
|
||||
|
||||
format!("v{}", formatted_version.replace("rustc", "").trim())
|
||||
let full_version_string = formatted_version.replace("rustc", "");
|
||||
let version_string = full_version_string.trim();
|
||||
match VersionFormatter::new(config.version_format) {
|
||||
Ok(formatter) => formatter.format_version(&version_string),
|
||||
_ => format!("v{}", version_string),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
@@ -320,17 +326,28 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn test_format_rustc_version() {
|
||||
let test_config: RustConfig = RustConfig::default();
|
||||
|
||||
let nightly_input = String::from("rustc 1.34.0-nightly (b139669f3 2019-04-10)");
|
||||
assert_eq!(format_rustc_version(nightly_input), "v1.34.0-nightly");
|
||||
assert_eq!(
|
||||
format_rustc_version(&test_config, nightly_input),
|
||||
"v1.34.0-nightly"
|
||||
);
|
||||
|
||||
let beta_input = String::from("rustc 1.34.0-beta.1 (2bc1d406d 2019-04-10)");
|
||||
assert_eq!(format_rustc_version(beta_input), "v1.34.0-beta.1");
|
||||
assert_eq!(
|
||||
format_rustc_version(&test_config, beta_input),
|
||||
"v1.34.0-beta.1"
|
||||
);
|
||||
|
||||
let stable_input = String::from("rustc 1.34.0 (91856ed52 2019-04-10)");
|
||||
assert_eq!(format_rustc_version(stable_input), "v1.34.0");
|
||||
assert_eq!(format_rustc_version(&test_config, stable_input), "v1.34.0");
|
||||
|
||||
let version_without_hash = String::from("rustc 1.34.0");
|
||||
assert_eq!(format_rustc_version(version_without_hash), "v1.34.0");
|
||||
assert_eq!(
|
||||
format_rustc_version(&test_config, version_without_hash),
|
||||
"v1.34.0"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
Reference in New Issue
Block a user