refactor: Refactor git_branch module to use new module config (#535)
This commit is contained in:
parent
be2d5cf1cd
commit
d2eef11148
|
@ -428,7 +428,7 @@ The `git_branch` module shows the active branch of the repo in your current dire
|
||||||
|
|
||||||
[git_branch]
|
[git_branch]
|
||||||
symbol = "🌱 "
|
symbol = "🌱 "
|
||||||
truncation_length = "4"
|
truncation_length = 4
|
||||||
truncation_symbol = ""
|
truncation_symbol = ""
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
|
@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ pub mod conda;
|
||||||
pub mod directory;
|
pub mod directory;
|
||||||
pub mod dotnet;
|
pub mod dotnet;
|
||||||
pub mod env_var;
|
pub mod env_var;
|
||||||
|
pub mod git_branch;
|
||||||
pub mod go;
|
pub mod go;
|
||||||
pub mod hostname;
|
pub mod hostname;
|
||||||
pub mod jobs;
|
pub mod jobs;
|
||||||
|
|
|
@ -1,43 +1,34 @@
|
||||||
use ansi_term::Color;
|
|
||||||
use unicode_segmentation::UnicodeSegmentation;
|
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
|
/// Creates a module with the Git branch in the current directory
|
||||||
///
|
///
|
||||||
/// Will display the branch name if the current directory is a git repo
|
/// Will display the branch name if the current directory is a git repo
|
||||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
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 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 ");
|
module.get_prefix().set_value("on ");
|
||||||
|
|
||||||
let unsafe_truncation_length = module
|
let truncation_symbol = get_graphemes(config.truncation_symbol, 1);
|
||||||
.config_value_i64("truncation_length")
|
module.create_segment("symbol", &config.symbol);
|
||||||
.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);
|
|
||||||
|
|
||||||
// TODO: Once error handling is implemented, warn the user if their config
|
// TODO: Once error handling is implemented, warn the user if their config
|
||||||
// truncation length is nonsensical
|
// truncation length is nonsensical
|
||||||
let len = if unsafe_truncation_length <= 0 {
|
let len = if config.truncation_length <= 0 {
|
||||||
log::debug!(
|
log::warn!(
|
||||||
"[WARN]: \"truncation_length\" should be a positive value, found {}",
|
"\"truncation_length\" should be a positive value, found {}",
|
||||||
unsafe_truncation_length
|
config.truncation_length
|
||||||
);
|
);
|
||||||
std::usize::MAX
|
std::usize::MAX
|
||||||
} else {
|
} else {
|
||||||
unsafe_truncation_length as usize
|
config.truncation_length as usize
|
||||||
};
|
};
|
||||||
|
|
||||||
let repo = context.get_repo().ok()?;
|
let repo = context.get_repo().ok()?;
|
||||||
let branch_name = repo.branch.as_ref()?;
|
let branch_name = repo.branch.as_ref()?;
|
||||||
let truncated_graphemes = get_graphemes(&branch_name, len);
|
let truncated_graphemes = get_graphemes(&branch_name, len);
|
||||||
|
@ -48,7 +39,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||||
truncated_graphemes
|
truncated_graphemes
|
||||||
};
|
};
|
||||||
|
|
||||||
module.new_segment("name", &truncated_and_symbol);
|
module.create_segment(
|
||||||
|
"name",
|
||||||
|
&config.branch_name.with_value(&truncated_and_symbol),
|
||||||
|
);
|
||||||
|
|
||||||
Some(module)
|
Some(module)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue