aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2018-02-20 03:29:06 -0500
committerJesse Luehrs <doy@tozt.net>2018-02-20 03:30:54 -0500
commit3336ca4b117b3348bb539cf993f57138e34334d7 (patch)
treef45acfc76fc3380ff2e180fc46ebcd5a9b66ffaf
parente8e8b5ebfb685138929a21446959a07fe2f38400 (diff)
downloadfancy-prompt-3336ca4b117b3348bb539cf993f57138e34334d7.tar.gz
fancy-prompt-3336ca4b117b3348bb539cf993f57138e34334d7.zip
implement branch name compression
-rw-r--r--src/prompt.rs31
1 files changed, 28 insertions, 3 deletions
diff --git a/src/prompt.rs b/src/prompt.rs
index e6b2134..c05ec8b 100644
--- a/src/prompt.rs
+++ b/src/prompt.rs
@@ -361,9 +361,34 @@ fn compress_path<T, U>(
}
}
-fn compress_vcs(vcs: &str, _len: usize) -> String {
- // XXX
- String::from(vcs)
+fn compress_vcs(vcs: &str, len: usize) -> String {
+ if vcs.len() > len {
+ let vcs_parts_re = regex::Regex::new(
+ r"^([^:]+):.*:([^:])$"
+ ).unwrap();
+ vcs_parts_re
+ .captures(vcs)
+ .map(|cap| {
+ let prefix_len = cap.get(1)
+ .map(|mat| mat.end() - mat.start())
+ .unwrap_or(0);
+ let suffix_len = cap.get(2)
+ .map(|mat| mat.end() - mat.start())
+ .unwrap_or(0);
+ let branch_len = len - prefix_len - suffix_len - 2;
+ let branch_re = regex::Regex::new(
+ &format!(
+ r"(:[^:]{{{}}})[^:]*([^:]{{3}}:)",
+ (branch_len - 6).to_string()
+ )
+ ).unwrap();
+ branch_re.replace(vcs, "$1...$2").into_owned()
+ })
+ .unwrap_or(vcs.to_string())
+ }
+ else {
+ vcs.to_string()
+ }
}
fn vcs_id(vcs: vcs::VcsType) -> String {