diff options
author | Titouan Vervack <tivervac@gmail.com> | 2019-09-02 21:56:59 +0200 |
---|---|---|
committer | Matan Kushner <hello@matchai.me> | 2019-09-02 15:56:59 -0400 |
commit | 59e8b1fc927eff4874ea44e381953b86ffc8af7b (patch) | |
tree | 074535b48421d7d4ea2c148e751bb02e99e413f5 /src/modules | |
parent | f8929c2d7d37ef1a511ed2b941ca9b1644f666c7 (diff) |
feat: added truncation_length/symbol to git_branch (#268)
Git branches can become very long (e.g. gitlab auto-generated branch
names), thus it would be nice to be able to truncate them to keep your
prompt lenght in line.
This patch adds two new options to the git_branch module:
* truncation_length: The amount of graphemes to of a gitbranch to
truncate to
* truncation_symbol: The symbol that should be used to indicate that a
branch name was trunctated
To be able to correctly work with UTF-8 graphemes, unicode-segmentation
was added as a dependency.
Diffstat (limited to 'src/modules')
-rw-r--r-- | src/modules/git_branch.rs | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/modules/git_branch.rs b/src/modules/git_branch.rs index e3a12e272..120c58d4e 100644 --- a/src/modules/git_branch.rs +++ b/src/modules/git_branch.rs @@ -1,4 +1,5 @@ use ansi_term::Color; +use unicode_segmentation::UnicodeSegmentation; use super::{Context, Module}; @@ -8,15 +9,54 @@ use super::{Context, Module}; pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { const GIT_BRANCH_CHAR: &str = " "; - let branch_name = context.branch_name.as_ref()?; let segment_color = Color::Purple.bold(); let mut module = context.new_module("git_branch")?; module.set_style(segment_color); module.get_prefix().set_value("on "); + let unsafe_truncation_length = module + .config_value_i64("truncation_length") + .unwrap_or(std::i64::MAX); + let truncation_symbol = get_graphemes( + module.config_value_str("truncation_symbol").unwrap_or("…"), + 1, + ); + module.new_segment("symbol", GIT_BRANCH_CHAR); - module.new_segment("name", branch_name); + + // TODO: Once error handling is implemented, warn the user if their config + // truncation length is nonsensical + let len = if unsafe_truncation_length <= 0 { + log::debug!( + "[WARN]: \"truncation_length\" should be a positive value, found {}", + unsafe_truncation_length + ); + std::usize::MAX + } else { + unsafe_truncation_length as usize + }; + let branch_name = context.branch_name.as_ref()?; + let truncated_graphemes = get_graphemes(&branch_name, len); + // The truncation symbol should only be added if we truncated + let truncated_and_symbol = if len < graphemes_len(&branch_name) { + truncated_graphemes + &truncation_symbol + } else { + truncated_graphemes + }; + + module.new_segment("name", &truncated_and_symbol); Some(module) } + +fn get_graphemes(text: &str, length: usize) -> String { + UnicodeSegmentation::graphemes(text, true) + .take(length) + .collect::<Vec<&str>>() + .concat() +} + +fn graphemes_len(text: &str) -> usize { + UnicodeSegmentation::graphemes(&text[..], true).count() +} |