2019-09-02 19:56:59 +00:00
|
|
|
use unicode_segmentation::UnicodeSegmentation;
|
2019-04-27 02:07:07 +00:00
|
|
|
|
2019-10-15 11:48:53 +00:00
|
|
|
use super::{Context, Module, RootModuleConfig};
|
|
|
|
|
|
|
|
use crate::configs::git_branch::GitBranchConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
2019-04-27 02:07:07 +00:00
|
|
|
|
2019-07-19 20:18:52 +00:00
|
|
|
/// Creates a module with the Git branch in the current directory
|
2019-04-27 02:07:07 +00:00
|
|
|
///
|
|
|
|
/// Will display the branch name if the current directory is a git repo
|
2019-07-02 20:12:53 +00:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-09-09 23:14:38 +00:00
|
|
|
let mut module = context.new_module("git_branch");
|
2019-10-24 10:31:17 +00:00
|
|
|
let config = GitBranchConfig::try_load(module.config);
|
2019-09-08 00:33:06 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let truncation_symbol = get_first_grapheme(config.truncation_symbol);
|
2019-09-02 19:56:59 +00:00
|
|
|
|
|
|
|
// TODO: Once error handling is implemented, warn the user if their config
|
2020-07-07 22:45:32 +00:00
|
|
|
// truncation length is nonsensical
|
2019-10-15 11:48:53 +00:00
|
|
|
let len = if config.truncation_length <= 0 {
|
|
|
|
log::warn!(
|
|
|
|
"\"truncation_length\" should be a positive value, found {}",
|
|
|
|
config.truncation_length
|
2019-09-02 19:56:59 +00:00
|
|
|
);
|
|
|
|
std::usize::MAX
|
|
|
|
} else {
|
2019-10-15 11:48:53 +00:00
|
|
|
config.truncation_length as usize
|
2019-09-02 19:56:59 +00:00
|
|
|
};
|
2019-10-15 11:48:53 +00:00
|
|
|
|
2019-09-09 23:14:38 +00:00
|
|
|
let repo = context.get_repo().ok()?;
|
2020-04-11 20:10:39 +00:00
|
|
|
let branch_name = repo.branch.as_ref()?;
|
2019-09-02 19:56:59 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let mut graphemes = get_graphemes(&branch_name);
|
|
|
|
let trunc_len = len.min(graphemes.len());
|
|
|
|
|
|
|
|
if trunc_len < graphemes.len() {
|
|
|
|
// The truncation symbol should only be added if we truncate
|
|
|
|
graphemes[trunc_len] = truncation_symbol;
|
|
|
|
graphemes.truncate(trunc_len + 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|var, _| match var {
|
|
|
|
"symbol" => Some(config.symbol),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
|
|
|
"branch" => Some(Ok(graphemes.concat())),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.parse(None)
|
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `git_branch`: \n{}", error);
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
});
|
2019-04-27 02:07:07 +00:00
|
|
|
|
2019-05-14 04:43:11 +00:00
|
|
|
Some(module)
|
2019-04-27 02:07:07 +00:00
|
|
|
}
|
2019-09-02 19:56:59 +00:00
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
fn get_first_grapheme(text: &str) -> &str {
|
2019-09-02 19:56:59 +00:00
|
|
|
UnicodeSegmentation::graphemes(text, true)
|
2020-07-07 22:45:32 +00:00
|
|
|
.next()
|
|
|
|
.unwrap_or("")
|
2019-09-02 19:56:59 +00:00
|
|
|
}
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
fn get_graphemes(text: &str) -> Vec<&str> {
|
|
|
|
UnicodeSegmentation::graphemes(text, true).collect()
|
2019-09-02 19:56:59 +00:00
|
|
|
}
|