From 8f22ff5362c3a85ff842cd03e2e8f57858bda66c Mon Sep 17 00:00:00 2001 From: Bojan Djurdjevic Date: Sun, 6 Oct 2019 18:49:19 -0400 Subject: [PATCH] Added project creation date --- src/main.rs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main.rs b/src/main.rs index 511c642eb..6dda6c9fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -182,9 +182,9 @@ fn count_newlines(s: &str) -> usize { /// escape characters for color display. /// /// Colors are specified with {0}, {1}... where the number represents -/// the nth element in the colors Vec provided to the function. +/// the nth element in the colors Vec provided to the function. /// If there are more colors in the ascii than in the Vec it -/// defaults to white. +/// defaults to white. /// The usize in the tuple refers to the extra padding needed /// which comes from the added escape characters. fn colorize_str(line: &str, colors: Vec) -> (String, usize) { @@ -355,9 +355,13 @@ fn main() -> Result<()> { let commits = get_commits(&dir)?; let repo_size = get_packed_size(&dir)?; let last_change = get_last_change(&dir)?; + let project_name = match get_creation_time() { + Some(creation_time) => format!("{}, {}", config.repository_name, creation_time), + None => config.repository_name + }; let info = Info { - project_name: config.repository_name, + project_name, current_commit: current_commit_info, version, dominant_language, @@ -513,7 +517,7 @@ fn get_packed_size(dir: &str) -> Result { None => "??", Some(size_str) => &(size_str[11..]) }; - + let output = Command::new("git") .arg("-C") .arg(dir) @@ -539,7 +543,7 @@ fn get_packed_size(dir: &str) -> Result { else{ let res =repo_size; Ok(res.into()) - } + } } fn is_git_installed() -> bool { @@ -666,6 +670,23 @@ fn get_total_loc(languages: &tokei::Languages) -> usize { .fold(0, |sum, val| sum + val.code) } +fn get_creation_time() -> Option { + let output = Command::new("git") + .arg("log") + .arg("--reverse") + .arg("--pretty=oneline") + .arg("--format=\"%ar\"") + .output() + .expect("Failed to execute git."); + + let output = String::from_utf8_lossy(&output.stdout); + + match output.lines().next() { + Some(val) => Some(val.to_string().replace('"', "")), + None => None + } +} + /// Convert from tokei LanguageType to known Language type . impl From for Language { fn from(language: tokei::LanguageType) -> Self {