1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-01 09:13:54 +00:00

refactor: Refactor nix_shell and java module to use module config (#606)

This commit is contained in:
Zhenhui Xie 2019-11-06 20:59:12 +08:00 committed by Matan Kushner
parent a24a751a0d
commit 48726fdd2a
5 changed files with 83 additions and 42 deletions

21
src/configs/java.rs Normal file
View File

@ -0,0 +1,21 @@
use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
use ansi_term::{Color, Style};
use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig)]
pub struct JavaConfig<'a> {
pub symbol: SegmentConfig<'a>,
pub style: Style,
pub disabled: bool,
}
impl<'a> RootModuleConfig<'a> for JavaConfig<'a> {
fn new() -> Self {
JavaConfig {
symbol: SegmentConfig::new(""),
style: Color::Red.dimmed(),
disabled: false,
}
}
}

View File

@ -10,9 +10,11 @@ pub mod git_branch;
pub mod git_status; pub mod git_status;
pub mod go; pub mod go;
pub mod hostname; pub mod hostname;
pub mod java;
pub mod jobs; pub mod jobs;
pub mod kubernetes; pub mod kubernetes;
pub mod memory_usage; pub mod memory_usage;
pub mod nix_shell;
pub mod nodejs; pub mod nodejs;
pub mod package; pub mod package;
pub mod python; pub mod python;

25
src/configs/nix_shell.rs Normal file
View File

@ -0,0 +1,25 @@
use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig};
use ansi_term::{Color, Style};
use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig)]
pub struct NixShellConfig<'a> {
pub use_name: bool,
pub impure_msg: SegmentConfig<'a>,
pub pure_msg: SegmentConfig<'a>,
pub style: Style,
pub disabled: bool,
}
impl<'a> RootModuleConfig<'a> for NixShellConfig<'a> {
fn new() -> Self {
NixShellConfig {
use_name: false,
impure_msg: SegmentConfig::new("impure"),
pure_msg: SegmentConfig::new("pure"),
style: Color::Red.bold(),
disabled: false,
}
}
}

View File

@ -1,9 +1,8 @@
use crate::configs::java::JavaConfig;
use std::process::Command; use std::process::Command;
use std::process::Output; use std::process::Output;
use ansi_term::Color; use super::{Context, Module, RootModuleConfig, SegmentConfig};
use super::{Context, Module};
use crate::modules::utils::java_version_parser; use crate::modules::utils::java_version_parser;
@ -25,17 +24,13 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
match get_java_version() { match get_java_version() {
Some(java_version) => { Some(java_version) => {
const JAVA_CHAR: &str = "";
let mut module = context.new_module("java"); let mut module = context.new_module("java");
let module_style = module let config: JavaConfig = JavaConfig::try_load(module.config);
.config_value_style("style") module.set_style(config.style);
.unwrap_or_else(|| Color::Red.dimmed());
module.set_style(module_style);
let formatted_version = format_java_version(java_version)?; let formatted_version = format_java_version(java_version)?;
module.new_segment("symbol", JAVA_CHAR); module.create_segment("symbol", &config.symbol);
module.new_segment("version", &formatted_version); module.create_segment("version", &SegmentConfig::new(&formatted_version));
Some(module) Some(module)
} }

View File

@ -1,7 +1,8 @@
use ansi_term::Color;
use std::env; use std::env;
use super::{Context, Module}; use super::{Context, Module, RootModuleConfig, SegmentConfig};
use crate::configs::nix_shell::NixShellConfig;
// IN_NIX_SHELL should be "pure" or "impure" but lorri uses "1" for "impure" // IN_NIX_SHELL should be "pure" or "impure" but lorri uses "1" for "impure"
// https://github.com/target/lorri/issues/140 // https://github.com/target/lorri/issues/140
@ -23,34 +24,31 @@ use super::{Context, Module};
/// - impure // use_name == false in an impure nix-shell /// - impure // use_name == false in an impure nix-shell
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> { pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("nix_shell"); let mut module = context.new_module("nix_shell");
let config: NixShellConfig = NixShellConfig::try_load(module.config);
env::var("IN_NIX_SHELL") module.set_style(config.style);
.ok()
.and_then(|shell_type| { let shell_type = env::var("IN_NIX_SHELL").ok()?;
if shell_type == "1" || shell_type == "impure" { let shell_type_segment: SegmentConfig = match shell_type.as_ref() {
Some(module.config_value_str("impure_msg").unwrap_or("impure")) "1" | "impure" => config.impure_msg,
} else if shell_type == "pure" { "pure" => config.pure_msg,
Some(module.config_value_str("pure_msg").unwrap_or("pure")) _ => {
} else { return None;
None }
} };
})
.map(|shell_type| { if config.use_name {
if module.config_value_bool("use_name").unwrap_or(false) { if let Ok(name) = env::var("name") {
match env::var("name").ok() { module.create_segment(
Some(name) => format!("{} ({})", name, shell_type), "nix_shell",
None => shell_type.to_string(), &shell_type_segment.with_value(&format!("{} ({})", name, shell_type_segment.value)),
} );
} else { } else {
shell_type.to_string() module.create_segment("nix_shell", &shell_type_segment);
} }
}) } else {
.map(|segment| { module.create_segment("nix_shell", &shell_type_segment);
let module_style = module }
.config_value_style("style")
.unwrap_or_else(|| Color::Red.bold()); Some(module)
module.set_style(module_style);
module.new_segment("nix_shell", &segment);
module
})
} }