From e9f4f8d5d71475b493feaa3d9f2fc7d798046881 Mon Sep 17 00:00:00 2001 From: Pablo Diego Date: Wed, 23 Oct 2019 11:08:35 -0300 Subject: [PATCH 1/7] Add git info for user and version --- src/info.rs | 32 +++++++++++++++++++++++++++++++- src/main.rs | 3 +++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/info.rs b/src/info.rs index 1d19a90fd..2c030c6be 100644 --- a/src/info.rs +++ b/src/info.rs @@ -14,6 +14,8 @@ use crate::{AsciiArt, CommitInfo, Configuration, Error, InfoFieldOn}; type Result = std::result::Result; pub struct Info { + git_version: String, + git_username: String, project_name: String, current_commit: CommitInfo, version: String, @@ -39,7 +41,13 @@ impl std::fmt::Display for Info { Some(&c) => c, None => Color::White, }; - + if !self.disable_fields.git_info{ + let mut git_info = String::from(&self.git_username); + git_info.push(':'); + git_info.push_str(&self.git_version); + write_buf(&mut buf, "", &git_info, color)?; + write_buf(&mut buf, "", "--------------", color)?; + } if !self.disable_fields.project { write_buf(&mut buf, "Project: ", &self.project_name, color)?; } @@ -187,6 +195,7 @@ impl Info { disabled: InfoFieldOn, ) -> Result { let authors = Info::get_authors(&dir, 3); + let (git_v, git_user) = Info::get_git_info(); let current_commit_info = Info::get_current_commit_info(&dir)?; let config = Info::get_configuration(&dir)?; let version = Info::get_version(&dir)?; @@ -199,6 +208,8 @@ impl Info { let dominant_language = Language::get_dominant_language(languages_stats.clone()); Ok(Info { + git_version: git_v, + git_username: git_user, project_name: config.repository_name, current_commit: current_commit_info, version, @@ -262,6 +273,25 @@ impl Info { authors } + fn get_git_info() -> (String, String){ + let version = Command::new("git") + .arg("--version") + .output() + .expect("Failed to execute git."); + let version = String::from_utf8_lossy(&version.stdout); + let version = version.to_string().replace('\n',""); + + let username = Command::new("git") + .arg("config") + .arg("--get") + .arg("user.name") + .output() + .expect("Failed to execute git."); + let username = String::from_utf8_lossy(&username.stdout); + let username = username.to_string().replace('\n',""); + (version, username) + } + fn get_current_commit_info(dir: &str) -> Result { let repo = Repository::open(dir).map_err(|_| Error::NotGitRepo)?; let head = repo.head().map_err(|_| Error::ReferenceInfoError)?; diff --git a/src/main.rs b/src/main.rs index c8080da8a..0caeae486 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,6 +38,7 @@ type Result = result::Result; #[derive(Default)] pub struct InfoFieldOn { + git_info:bool, project: bool, head: bool, version: bool, @@ -55,6 +56,7 @@ pub struct InfoFieldOn { #[derive(PartialEq, Eq, EnumString, EnumCount, EnumIter, IntoStaticStr)] #[strum(serialize_all = "snake_case")] enum InfoFields { + GitInfo, Project, HEAD, Version, @@ -188,6 +190,7 @@ Possible values: [{0}{1}{2}{3}{4}{5}{6}{7}{8}{9}{10}{11}{12}{13}{14}{15}]", .unwrap_or(InfoFields::UnrecognizedField); match item { + InfoFields::GitInfo => disable_fields.git_info = true, InfoFields::Project => disable_fields.project = true, InfoFields::HEAD => disable_fields.head = true, InfoFields::Version => disable_fields.version = true, From 50b8763b8044ca5745039890d2ebdcb03005989e Mon Sep 17 00:00:00 2001 From: Pablo Diego Date: Wed, 23 Oct 2019 11:45:20 -0300 Subject: [PATCH 2/7] Fix directory access on get git info --- src/info.rs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/info.rs b/src/info.rs index 2c030c6be..9e14c3575 100644 --- a/src/info.rs +++ b/src/info.rs @@ -195,7 +195,7 @@ impl Info { disabled: InfoFieldOn, ) -> Result { let authors = Info::get_authors(&dir, 3); - let (git_v, git_user) = Info::get_git_info(); + let (git_v, git_user) = Info::get_git_info(&dir); let current_commit_info = Info::get_current_commit_info(&dir)?; let config = Info::get_configuration(&dir)?; let version = Info::get_version(&dir)?; @@ -273,7 +273,7 @@ impl Info { authors } - fn get_git_info() -> (String, String){ + fn get_git_info(dir: &str) -> (String, String){ let version = Command::new("git") .arg("--version") .output() @@ -282,6 +282,8 @@ impl Info { let version = version.to_string().replace('\n',""); let username = Command::new("git") + .arg("-C") + .arg(dir) .arg("config") .arg("--get") .arg("user.name") From 1fb086fcbd0823134ce920338b6f7f1c7dd8e335 Mon Sep 17 00:00:00 2001 From: Pablo Diego Date: Wed, 23 Oct 2019 11:59:18 -0300 Subject: [PATCH 3/7] Format separator based on git info length --- src/info.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/info.rs b/src/info.rs index 9e14c3575..b36914986 100644 --- a/src/info.rs +++ b/src/info.rs @@ -43,10 +43,13 @@ impl std::fmt::Display for Info { }; if !self.disable_fields.git_info{ let mut git_info = String::from(&self.git_username); + git_info.push(':'); git_info.push_str(&self.git_version); write_buf(&mut buf, "", &git_info, color)?; - write_buf(&mut buf, "", "--------------", color)?; + + let separator = "-".repeat(git_info.len()); + write_buf(&mut buf, "", &separator, color)?; } if !self.disable_fields.project { write_buf(&mut buf, "Project: ", &self.project_name, color)?; From daa59b460ad217c0201e061127c98ad12ebf93f7 Mon Sep 17 00:00:00 2001 From: Pablo Diego Date: Wed, 23 Oct 2019 12:16:43 -0300 Subject: [PATCH 4/7] Ignore username when empty or unset --- src/info.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/info.rs b/src/info.rs index b36914986..5c9bd930f 100644 --- a/src/info.rs +++ b/src/info.rs @@ -42,10 +42,13 @@ impl std::fmt::Display for Info { None => Color::White, }; if !self.disable_fields.git_info{ - let mut git_info = String::from(&self.git_username); + let username = if self.git_username == "" { + String::from("") + }else{ + String::from(format!("{}:",self.git_username)) + }; - git_info.push(':'); - git_info.push_str(&self.git_version); + let git_info = format!("{}{}", username, self.git_version); write_buf(&mut buf, "", &git_info, color)?; let separator = "-".repeat(git_info.len()); From 2b5ddf4e4d7f0d945ef61dc30189b009f5ddaee1 Mon Sep 17 00:00:00 2001 From: Pablo Diego Date: Thu, 24 Oct 2019 11:40:38 -0300 Subject: [PATCH 5/7] Removing redundancies in string usage --- src/info.rs | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/info.rs b/src/info.rs index 5c9bd930f..b864f54d2 100644 --- a/src/info.rs +++ b/src/info.rs @@ -45,7 +45,7 @@ impl std::fmt::Display for Info { let username = if self.git_username == "" { String::from("") }else{ - String::from(format!("{}:",self.git_username)) + format!("{}:",self.git_username) }; let git_info = format!("{}{}", username, self.git_version); @@ -284,8 +284,7 @@ impl Info { .arg("--version") .output() .expect("Failed to execute git."); - let version = String::from_utf8_lossy(&version.stdout); - let version = version.to_string().replace('\n',""); + let version = String::from_utf8_lossy(&version.stdout).replace('\n',""); let username = Command::new("git") .arg("-C") @@ -295,8 +294,7 @@ impl Info { .arg("user.name") .output() .expect("Failed to execute git."); - let username = String::from_utf8_lossy(&username.stdout); - let username = username.to_string().replace('\n',""); + let username = String::from_utf8_lossy(&username.stdout).replace('\n',""); (version, username) } From cc6a9c8a9e114c3665f02f302bf8c38392f4443c Mon Sep 17 00:00:00 2001 From: Pablo Diego Date: Thu, 24 Oct 2019 12:21:50 -0300 Subject: [PATCH 6/7] Update usage of write_buf and colors --- src/info.rs | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/info.rs b/src/info.rs index d454e2a9e..88156c12f 100644 --- a/src/info.rs +++ b/src/info.rs @@ -43,17 +43,15 @@ impl std::fmt::Display for Info { None => Color::White, }; if !self.disable_fields.git_info{ - let username = if self.git_username == "" { - String::from("") - }else{ - format!("{}:",self.git_username) - }; - - let git_info = format!("{}{}", username, self.git_version); - write_buf(&mut buf, "", &git_info, color)?; - + let git_info; + if self.git_username != "" { + git_info = format!("{} : {}", self.git_username, self.git_version); + } else { + git_info = self.git_version.clone(); + } + write_buf(&mut buf, &self.get_formatted_info_label(&git_info, color), "")?; let separator = "-".repeat(git_info.len()); - write_buf(&mut buf, "", &separator, color)?; + write_buf(&mut buf, &self.get_formatted_info_label("", color), &separator)?; } if !self.disable_fields.project { write_buf(&mut buf, &self.get_formatted_info_label("Project: ", color), &self.project_name)?; From ae95e9552492b67d172c0a12d50c6c57e70a698b Mon Sep 17 00:00:00 2001 From: Pablo Diego Date: Thu, 24 Oct 2019 14:05:32 -0300 Subject: [PATCH 7/7] Fix separator color --- src/info.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/info.rs b/src/info.rs index 88156c12f..b3c527fee 100644 --- a/src/info.rs +++ b/src/info.rs @@ -45,11 +45,12 @@ impl std::fmt::Display for Info { if !self.disable_fields.git_info{ let git_info; if self.git_username != "" { - git_info = format!("{} : {}", self.git_username, self.git_version); + git_info = format!("{} : {}", self.git_username, self.git_version); + write!(&mut buf, "{}{}", &self.get_formatted_info_label(&self.git_username, color), " : ")?; } else { git_info = self.git_version.clone(); } - write_buf(&mut buf, &self.get_formatted_info_label(&git_info, color), "")?; + write_buf(&mut buf, &self.get_formatted_info_label(&self.git_version, color), "")?; let separator = "-".repeat(git_info.len()); write_buf(&mut buf, &self.get_formatted_info_label("", color), &separator)?; }