refactor: Refactor git_branch module to use new module config (#535)

This commit is contained in:
Matias Kotlik 2019-10-15 06:48:53 -05:00 committed by Matan Kushner
parent be2d5cf1cd
commit d2eef11148
4 changed files with 46 additions and 24 deletions

View File

@ -428,7 +428,7 @@ The `git_branch` module shows the active branch of the repo in your current dire
[git_branch]
symbol = "🌱 "
truncation_length = "4"
truncation_length = 4
truncation_symbol = ""
```

27
src/configs/git_branch.rs Normal file
View File

@ -0,0 +1,27 @@
use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
use ansi_term::{Color, Style};
use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig)]
pub struct GitBranchConfig<'a> {
pub symbol: SegmentConfig<'a>,
pub truncation_length: i64,
pub truncation_symbol: &'a str,
pub branch_name: SegmentConfig<'a>,
pub style: Style,
pub disabled: bool,
}
impl<'a> RootModuleConfig<'a> for GitBranchConfig<'a> {
fn new() -> Self {
GitBranchConfig {
symbol: SegmentConfig::new(""),
truncation_length: std::i64::MAX,
truncation_symbol: "",
branch_name: SegmentConfig::default(),
style: Color::Purple.bold(),
disabled: false,
}
}
}

View File

@ -6,6 +6,7 @@ pub mod conda;
pub mod directory;
pub mod dotnet;
pub mod env_var;
pub mod git_branch;
pub mod go;
pub mod hostname;
pub mod jobs;

View File

@ -1,43 +1,34 @@
use ansi_term::Color;
use unicode_segmentation::UnicodeSegmentation;
use super::{Context, Module};
use super::{Context, Module, RootModuleConfig};
use crate::configs::git_branch::GitBranchConfig;
/// Creates a module with the Git branch in the current directory
///
/// Will display the branch name if the current directory is a git repo
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
const GIT_BRANCH_CHAR: &str = "";
let mut module = context.new_module("git_branch");
let mut config = GitBranchConfig::try_load(module.config);
module.set_style(config.style);
let segment_color = module
.config_value_style("style")
.unwrap_or_else(|| Color::Purple.bold());
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);
let truncation_symbol = get_graphemes(config.truncation_symbol, 1);
module.create_segment("symbol", &config.symbol);
// 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
let len = if config.truncation_length <= 0 {
log::warn!(
"\"truncation_length\" should be a positive value, found {}",
config.truncation_length
);
std::usize::MAX
} else {
unsafe_truncation_length as usize
config.truncation_length as usize
};
let repo = context.get_repo().ok()?;
let branch_name = repo.branch.as_ref()?;
let truncated_graphemes = get_graphemes(&branch_name, len);
@ -48,7 +39,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
truncated_graphemes
};
module.new_segment("name", &truncated_and_symbol);
module.create_segment(
"name",
&config.branch_name.with_value(&truncated_and_symbol),
);
Some(module)
}