From d2eef11148c163b8c650993aa3c7cda4b953748c Mon Sep 17 00:00:00 2001 From: Matias Kotlik Date: Tue, 15 Oct 2019 06:48:53 -0500 Subject: [PATCH] refactor: Refactor git_branch module to use new module config (#535) --- docs/config/README.md | 2 +- src/configs/git_branch.rs | 27 ++++++++++++++++++++++++++ src/configs/mod.rs | 1 + src/modules/git_branch.rs | 40 +++++++++++++++++---------------------- 4 files changed, 46 insertions(+), 24 deletions(-) create mode 100644 src/configs/git_branch.rs diff --git a/docs/config/README.md b/docs/config/README.md index 89c4f16a..c8cbee2b 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -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 = "" ``` diff --git a/src/configs/git_branch.rs b/src/configs/git_branch.rs new file mode 100644 index 00000000..ff6d87b2 --- /dev/null +++ b/src/configs/git_branch.rs @@ -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, + } + } +} diff --git a/src/configs/mod.rs b/src/configs/mod.rs index b26c1ebf..4f1a90b5 100644 --- a/src/configs/mod.rs +++ b/src/configs/mod.rs @@ -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; diff --git a/src/modules/git_branch.rs b/src/modules/git_branch.rs index 6d07e890..c0b6274e 100644 --- a/src/modules/git_branch.rs +++ b/src/modules/git_branch.rs @@ -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> { - 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> { truncated_graphemes }; - module.new_segment("name", &truncated_and_symbol); + module.create_segment( + "name", + &config.branch_name.with_value(&truncated_and_symbol), + ); Some(module) }