From 46199310f0ae19709e66e3f7c976cb6878ede97c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ale=C5=A1=20Katona?= Date: Sun, 18 Sep 2022 10:15:48 -0700 Subject: [PATCH] add crate version info to verbose status --- CHANGELOG.md | 3 +++ src/vcs/colors.rs | 1 + src/vcs/project.rs | 17 ++++++++++++----- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 664bb78..dc202ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased] +### Added +- add crate version info to verbose status + ## [v0.2.0] - 2022-08-04 ### Changed diff --git a/src/vcs/colors.rs b/src/vcs/colors.rs index 7d8662a..98bc2b5 100644 --- a/src/vcs/colors.rs +++ b/src/vcs/colors.rs @@ -4,5 +4,6 @@ pub const PROJECT_COLOR: Color = Color::Red; pub const PROFILE_COLOR: Color = Color::Blue; pub const ERROR_COLOR: Color = Color::DarkRed; pub const CHANGES_COLOR: Color = Color::Cyan; +pub const VERSION_COLOR: Color = Color::DarkGreen; pub const REFS_COLOR: Color = Color::DarkYellow; pub const MSRV_COLOR: Color = Color::DarkCyan; diff --git a/src/vcs/project.rs b/src/vcs/project.rs index 27c6fd0..25b5d4b 100644 --- a/src/vcs/project.rs +++ b/src/vcs/project.rs @@ -17,6 +17,7 @@ pub struct Project { path: PathBuf, name: String, msrv: Option, + crate_version: Version, pub repo: Box, profile_map: HashMap, // vcs profile name to branch names } @@ -48,12 +49,17 @@ impl Project { .to_owned(); let repo = GitRepository::open(&path)?; - let msrv = Self::parse_msrv(&path)?; + let msrv = Self::parse_version(&path, "rust-version")?; + let crate_version = match Self::parse_version(&path, "version")? { + Some(v) => v, + None => return Err(Error::cargo_error("Crate version not found")), + }; Ok(Self { path, name, msrv, + crate_version, repo, profile_map, }) @@ -108,18 +114,18 @@ impl Project { self.msrv.as_ref() } - fn parse_msrv(path: &Path) -> Result, Error> { + fn parse_version(path: &Path, key: &str) -> Result, Error> { let toml_path = path.join("Cargo.toml"); let toml_contents = std::fs::read_to_string(toml_path)?; let cargo: toml::Value = toml::from_str(&toml_contents)?; if let Some(pkg) = cargo.get("package") { - if let Some(value) = pkg.get("rust-version") { + if let Some(value) = pkg.get(key) { if value.is_str() { let mut ver_str = value .as_str() - .ok_or_else(|| Error::cargo_error("Unparsable rust-version in Cargo.toml"))? + .ok_or_else(|| Error::cargo_error("Unparsable version in Cargo.toml"))? .to_owned(); let dots = ver_str.chars().filter(|c| *c == '.').count(); if !(1..=2).contains(&dots) { @@ -141,8 +147,9 @@ impl Project { pub fn display(&self, options: EnumSet) -> Result, Error> { let title = format!( - "{}: {} [{}]", + "{}@{}: {} [{}]", self.name().bold().with(PROJECT_COLOR), + format!("v{}", self.crate_version).with(VERSION_COLOR), self.current_ref()? .unwrap_or_else(|| "???".into()) .with(REFS_COLOR),