add crate version info to verbose status

master
Aleš Katona 2 years ago
parent 84193de488
commit 46199310f0
Signed by: almindor
GPG Key ID: 2F773149BF38B48F

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

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

@ -17,6 +17,7 @@ pub struct Project {
path: PathBuf,
name: String,
msrv: Option<Version>,
crate_version: Version,
pub repo: Box<dyn Repository>,
profile_map: HashMap<String, String>, // 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<Option<Version>, Error> {
fn parse_version(path: &Path, key: &str) -> Result<Option<Version>, 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<ProjectDisplayOptions>) -> Result<Tree<String>, 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),

Loading…
Cancel
Save