From 20ebf3663c20200ecc47bb0737ea53407e51682d Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Sun, 19 May 2019 22:51:12 +0530 Subject: improve vcs_status, now displays repo status --- src/main.rs | 48 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index 65a293d..b4972e3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,17 @@ use std::env; use tico::tico; -use git2::Repository; +use git2::{ Repository, Status }; fn main() { + println!("{:?}", env::var("GIT_DIRTY")); print!("{}", cwd()); - match vcs() { - Some(br) => println!(" {}", br), - None => println!() - } + let (branch, status) = match vcs_status() { + Some((x, y)) => { + (x, y) + }, + None => ("".into(), "".into()) + }; + println!(" {} {}", branch, status); println!("{}", prompt_char()); } @@ -32,7 +36,7 @@ fn prompt_char() -> String { } } -fn vcs() -> Option { +fn vcs_status() -> Option<(String, String)> { let current_dir = env::var("PWD").unwrap(); let repo = match Repository::open(current_dir) { @@ -41,12 +45,40 @@ fn vcs() -> Option { }; let reference = repo.head().unwrap(); + let mut branch; if reference.is_branch() { - Some(format!("{}", reference.shorthand().unwrap())) + branch = format!("{}", reference.shorthand().unwrap()); } else { let commit = reference.peel_to_commit().unwrap(); let id = commit.id(); - Some(format!("{}", id)) + branch = format!("{}", id); + } + + let mut repo_stat = String::new(); + + let file_stats = repo.statuses(None).unwrap(); + for file in file_stats.iter() { + match file.status() { + Status::WT_NEW | + Status::WT_MODIFIED | + Status::WT_DELETED | + Status::WT_TYPECHANGE | + Status::WT_RENAMED => { + let stat_char = env::var("GIT_DIRTY").unwrap_or("×".into()); + repo_stat = stat_char; + break; + }, + Status::INDEX_NEW | + Status::INDEX_MODIFIED | + Status::INDEX_DELETED | + Status::INDEX_TYPECHANGE | + Status::INDEX_RENAMED => { + let stat_char = env::var("GIT_CLEAN").unwrap_or("·".into()); + repo_stat = stat_char; + } + _ => { } + } } + return Some((branch, repo_stat)) } -- cgit v1.2.3