diff options
author | Jesse Luehrs <doy@tozt.net> | 2018-02-20 02:55:24 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2018-02-20 03:30:54 -0500 |
commit | e8e8b5ebfb685138929a21446959a07fe2f38400 (patch) | |
tree | 9a165ed3fb44e3201efe8393be1f365e29c26a0e | |
parent | 844d6b09ff1e7ab4ddb4b15dcb04cd0e45c67ce1 (diff) | |
download | fancy-prompt-e8e8b5ebfb685138929a21446959a07fe2f38400.tar.gz fancy-prompt-e8e8b5ebfb685138929a21446959a07fe2f38400.zip |
handle vcs coloring
-rw-r--r-- | src/colors.rs | 2 | ||||
-rw-r--r-- | src/prompt.rs | 29 | ||||
-rw-r--r-- | src/vcs/mod.rs | 11 |
3 files changed, 37 insertions, 5 deletions
diff --git a/src/colors.rs b/src/colors.rs index 2db944c..bb8ea4b 100644 --- a/src/colors.rs +++ b/src/colors.rs @@ -32,6 +32,8 @@ impl Colors { color_map.insert(String::from("path_not_writable"), term::color::YELLOW); color_map.insert(String::from("path_not_exist"), term::color::RED); + color_map.insert(String::from("vcs_dirty"), term::color::RED); + color_map.insert(String::from("vcs_error"), term::color::BRIGHT_RED); color_map.insert(String::from("battery_warn"), term::color::YELLOW); color_map.insert(String::from("battery_crit"), term::color::RED); diff --git a/src/prompt.rs b/src/prompt.rs index 0d0defa..e6b2134 100644 --- a/src/prompt.rs +++ b/src/prompt.rs @@ -43,7 +43,7 @@ impl Prompt { let host = self.data.hostname.clone().unwrap_or(String::from("???")); let max_vcs_len = 20; // "g*+?:mybr...nch:+1-1" - let (vcs, vcs_color) = self.format_vcs(); + let vcs = self.format_vcs(); let vcs = vcs.map(|vcs| { compress_vcs(&vcs, max_vcs_len) }); @@ -82,7 +82,12 @@ impl Prompt { ); self.colors.pad(1); - self.display_path(&path, &path_color(&self.data.pwd), &vcs, &vcs_color); + self.display_path( + &path, + &path_color(&self.data.pwd), + &vcs, + &self.vcs_color(), + ); self.colors.pad(1); self.display_border(max_path_len - path.len() + 1); @@ -185,8 +190,8 @@ impl Prompt { self.colors.print_user(&self.data.user, prompt); } - fn format_vcs(&self) -> (Option<String>, String) { - (self.data.vcs_info.as_ref().map(|vcs_info| { + fn format_vcs(&self) -> Option<String> { + self.data.vcs_info.as_ref().map(|vcs_info| { let mut vcs = String::new(); write!(vcs, "{}", vcs_id(vcs_info.vcs())).unwrap(); @@ -240,7 +245,21 @@ impl Prompt { } vcs - }), String::from("default")) // XXX + }) + } + + fn vcs_color(&self) -> String { + self.data.vcs_info.as_ref().map(|vcs_info| { + if vcs_info.is_error() { + String::from("vcs_error") + } + else if vcs_info.is_dirty() { + String::from("vcs_dirty") + } + else { + String::from("default") + } + }).unwrap_or(String::from("vcs_error")) } } diff --git a/src/vcs/mod.rs b/src/vcs/mod.rs index 02c2eb7..6b94a72 100644 --- a/src/vcs/mod.rs +++ b/src/vcs/mod.rs @@ -23,6 +23,17 @@ pub trait VcsInfo { fn active_operation(&self) -> ActiveOperation; fn branch(&self) -> Option<String>; fn remote_branch_diff(&self) -> Option<(usize, usize)>; + + fn is_dirty(&self) -> bool { + self.has_modified_files() + || self.has_staged_files() + || self.has_new_files() + || !self.remote_branch_diff().is_some() + } + + fn is_error(&self) -> bool { + !self.has_commits() + } } pub fn detect() -> Option<Box<VcsInfo>> { |