summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatias Kotlik <mdkotlik@gmail.com>2019-10-15 06:48:53 -0500
committerMatan Kushner <hello@matchai.me>2019-10-15 20:48:53 +0900
commitd2eef11148c163b8c650993aa3c7cda4b953748c (patch)
treef630e67b519ea46593d69d213d21c192b62c6792
parentbe2d5cf1cd23d2b33892445f5d73dda5b165e7a7 (diff)
refactor: Refactor git_branch module to use new module config (#535)
-rw-r--r--docs/config/README.md2
-rw-r--r--src/configs/git_branch.rs27
-rw-r--r--src/configs/mod.rs1
-rw-r--r--src/modules/git_branch.rs40
4 files changed, 46 insertions, 24 deletions
diff --git a/docs/config/README.md b/docs/config/README.md
index 89c4f16a4..c8cbee2bf 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 000000000..ff6d87b28
--- /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 b26c1ebf9..4f1a90b51 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 6d07e8905..c0b6274e8 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<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)
}