summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorMilo <50248166+Milo123459@users.noreply.github.com>2021-05-03 20:50:29 +0100
committerGitHub <noreply@github.com>2021-05-03 21:50:29 +0200
commit48f913ec2309c8fa1f3c012e5aa33f7df0f6e0ad (patch)
treed0a54a5cd27b2f43685812b967cf62b6e407f4a6 /src
parentdead06ba14e30803f0027be8d46cc1f395d5d87e (diff)
feat(vlang): create module (#2577)
* Add V module * Format * Fix tests * fix typo * Update src/configs/v.rs Co-authored-by: Dario Vladović <d.vladimyr@gmail.com> * Update docs/config/README.md Co-authored-by: Dario Vladović <d.vladimyr@gmail.com> * Update docs/config/README.md Co-authored-by: Dario Vladović <d.vladimyr@gmail.com> * Update docs/config/README.md Co-authored-by: Dario Vladović <d.vladimyr@gmail.com> * Use blue bold * v -> vlang * change docs * More vlang fixes * add package support for v * Update docs/config/README.md Co-authored-by: Dario Vladović <d.vladimyr@gmail.com> * use regex * Update src/configs/mod.rs Co-authored-by: David Knaack <davidkna@users.noreply.github.com> * apply patch * fix Co-authored-by: Dario Vladović <d.vladimyr@gmail.com> Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
Diffstat (limited to 'src')
-rw-r--r--src/configs/mod.rs3
-rw-r--r--src/configs/starship_root.rs1
-rw-r--r--src/configs/v.rs29
-rw-r--r--src/module.rs1
-rw-r--r--src/modules/mod.rs3
-rw-r--r--src/modules/package.rs24
-rw-r--r--src/modules/vlang.rs92
-rw-r--r--src/utils.rs4
8 files changed, 157 insertions, 0 deletions
diff --git a/src/configs/mod.rs b/src/configs/mod.rs
index a7663fac1..08fbbfa07 100644
--- a/src/configs/mod.rs
+++ b/src/configs/mod.rs
@@ -59,6 +59,7 @@ pub mod swift;
pub mod terraform;
pub mod time;
pub mod username;
+pub mod v;
pub mod vagrant;
pub mod vcsh;
pub mod zig;
@@ -128,6 +129,7 @@ pub struct FullConfig<'a> {
terraform: terraform::TerraformConfig<'a>,
time: time::TimeConfig<'a>,
username: username::UsernameConfig<'a>,
+ vlang: v::VLangConfig<'a>,
vagrant: vagrant::VagrantConfig<'a>,
zig: zig::ZigConfig<'a>,
custom: IndexMap<String, custom::CustomConfig<'a>>,
@@ -196,6 +198,7 @@ impl<'a> Default for FullConfig<'a> {
time: Default::default(),
username: Default::default(),
vagrant: Default::default(),
+ vlang: Default::default(),
zig: Default::default(),
custom: Default::default(),
}
diff --git a/src/configs/starship_root.rs b/src/configs/starship_root.rs
index 50c206c06..f8cc2c62d 100644
--- a/src/configs/starship_root.rs
+++ b/src/configs/starship_root.rs
@@ -58,6 +58,7 @@ pub const PROMPT_ORDER: &[&str] = &[
"scala",
"swift",
"terraform",
+ "vlang",
"vagrant",
"zig",
// ↑ Toolchain version modules ↑
diff --git a/src/configs/v.rs b/src/configs/v.rs
new file mode 100644
index 000000000..dc8624c69
--- /dev/null
+++ b/src/configs/v.rs
@@ -0,0 +1,29 @@
+use crate::config::ModuleConfig;
+
+use serde::Serialize;
+use starship_module_config_derive::ModuleConfig;
+
+#[derive(Clone, ModuleConfig, Serialize)]
+pub struct VLangConfig<'a> {
+ pub format: &'a str,
+ pub symbol: &'a str,
+ pub style: &'a str,
+ pub disabled: bool,
+ pub detect_extensions: Vec<&'a str>,
+ pub detect_files: Vec<&'a str>,
+ pub detect_folders: Vec<&'a str>,
+}
+
+impl<'a> Default for VLangConfig<'a> {
+ fn default() -> Self {
+ VLangConfig {
+ format: "via [$symbol($version )]($style)",
+ symbol: "V ",
+ style: "blue bold",
+ disabled: false,
+ detect_extensions: vec!["v"],
+ detect_files: vec!["v.mod"],
+ detect_folders: vec![],
+ }
+ }
+}
diff --git a/src/module.rs b/src/module.rs
index cee9f357f..860ca04c9 100644
--- a/src/module.rs
+++ b/src/module.rs
@@ -67,6 +67,7 @@ pub const ALL_MODULES: &[&str] = &[
"username",
"vcsh",
"vagrant",
+ "vlang",
"zig",
];
diff --git a/src/modules/mod.rs b/src/modules/mod.rs
index 1d5eac0e5..b255a7f2a 100644
--- a/src/modules/mod.rs
+++ b/src/modules/mod.rs
@@ -57,6 +57,7 @@ mod username;
mod utils;
mod vagrant;
mod vcsh;
+mod vlang;
mod zig;
#[cfg(feature = "battery")]
@@ -130,6 +131,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"time" => time::module(context),
"crystal" => crystal::module(context),
"username" => username::module(context),
+ "vlang" => vlang::module(context),
"vagrant" => vagrant::module(context),
"vcsh" => vcsh::module(context),
"zig" => zig::module(context),
@@ -210,6 +212,7 @@ pub fn description(module: &str) -> &'static str {
"terraform" => "The currently selected terraform workspace and version",
"time" => "The current local time",
"username" => "The active user's username",
+ "vlang" => "The currently installed version of V",
"vagrant" => "The currently installed version of Vagrant",
"vcsh" => "The currently active VCSH repository",
"zig" => "The currently installed version of Zig",
diff --git a/src/modules/package.rs b/src/modules/package.rs
index bd7372ca6..1f95a7e0a 100644
--- a/src/modules/package.rs
+++ b/src/modules/package.rs
@@ -54,6 +54,13 @@ fn extract_cargo_version(file_contents: &str) -> Option<String> {
Some(formatted_version)
}
+fn extract_vlang_version(file_contents: &str) -> Option<String> {
+ let re = Regex::new(r"(?m)^\s*version\s*:\s*'(?P<version>[^']+)'").unwrap();
+ let caps = re.captures(file_contents)?;
+ let formatted_version = format_version(&caps["version"]);
+ Some(formatted_version)
+}
+
fn extract_package_version(file_contents: &str, display_private: bool) -> Option<String> {
let package_json: json::Value = json::from_str(file_contents).ok()?;
@@ -198,6 +205,8 @@ fn get_package_version(base_dir: &Path, config: &PackageConfig) -> Option<String
extract_maven_version(&pom_file)
} else if let Ok(meson_build) = utils::read_file(base_dir.join("meson.build")) {
extract_meson_version(&meson_build)
+ } else if let Ok(vlang_mod) = utils::read_file(base_dir.join("v.mod")) {
+ extract_vlang_version(&vlang_mod)
} else {
None
}
@@ -818,6 +827,21 @@ end";
project_dir.close()
}
+ #[test]
+ fn test_extract_vlang_version() -> io::Result<()> {
+ let config_name = "v.mod";
+ let config_content = "
+ Module {
+ name: 'starship',
+ author: 'matchai',
+ version: '1.2.3'
+ }";
+ let project_dir = create_project_dir()?;
+ fill_config(&project_dir, config_name, Some(&config_content))?;
+ expect_output(&project_dir, Some("v1.2.3"), None);
+ project_dir.close()
+ }
+
fn create_project_dir() -> io::Result<TempDir> {
tempfile::tempdir()
}
diff --git a/src/modules/vlang.rs b/src/modules/vlang.rs
new file mode 100644
index 000000000..0d7d0e7b0
--- /dev/null
+++ b/src/modules/vlang.rs
@@ -0,0 +1,92 @@
+use super::{Context, Module, RootModuleConfig};
+
+use crate::configs::v::VLangConfig;
+use crate::formatter::StringFormatter;
+
+/// Creates a module with the current V version
+pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
+ let mut module = context.new_module("vlang");
+ let config = VLangConfig::try_load(module.config);
+ let is_v_project = context
+ .try_begin_scan()?
+ .set_extensions(&config.detect_extensions)
+ .is_match();
+
+ if !is_v_project {
+ return None;
+ }
+
+ 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 {
+ "version" => context
+ .exec_cmd("v", &["version"])
+ .and_then(|output| parse_v_version(output.stdout.trim()))
+ .map(Ok),
+ _ => None,
+ })
+ .parse(None)
+ });
+
+ module.set_segments(match parsed {
+ Ok(segments) => segments,
+ Err(error) => {
+ log::warn!("Error in module `vlang`:\n{}", error);
+ return None;
+ }
+ });
+
+ Some(module)
+}
+
+fn parse_v_version(v_version: &str) -> Option<String> {
+ let version = v_version
+ // split into ["V", "0.2", "30c0659"]
+ .split_whitespace()
+ // return "0.2"
+ .nth(1)?;
+
+ Some(format!("v{}", version))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::parse_v_version;
+ use crate::test::ModuleRenderer;
+ use ansi_term::Color;
+ use std::fs::File;
+ use std::io;
+
+ #[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()))
+ }
+
+ #[test]
+ fn folder_without_v_files() -> io::Result<()> {
+ let dir = tempfile::tempdir()?;
+ let actual = ModuleRenderer::new("vlang").path(dir.path()).collect();
+ let expected = None;
+ assert_eq!(expected, actual);
+ dir.close()
+ }
+
+ #[test]
+ fn folder_with_v_files() -> io::Result<()> {
+ let dir = tempfile::tempdir()?;
+ File::create(dir.path().join("hello.v"))?.sync_all()?;
+ let actual = ModuleRenderer::new("vlang").path(dir.path()).collect();
+ let expected = Some(format!("via {}", Color::Blue.bold().paint("V v0.2 ")));
+ assert_eq!(expected, actual);
+ dir.close()
+ }
+}
diff --git a/src/utils.rs b/src/utils.rs
index 32fd342f5..7ef829a31 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -188,6 +188,10 @@ Target: x86_64-apple-darwin19.4.0\n",
stdout: String::from("Vagrant 2.2.10\n"),
stderr: String::default(),
}),
+ "v version" => Some(CommandOutput {
+ stdout: String::from("V 0.2 30c0659"),
+ stderr: String::default()
+ }),
"zig version" => Some(CommandOutput {
stdout: String::from("0.6.0\n"),
stderr: String::default(),