mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-11-28 15:56:28 +00:00
refactor: Refactor nix_shell and java module to use module config (#606)
This commit is contained in:
parent
a24a751a0d
commit
48726fdd2a
21
src/configs/java.rs
Normal file
21
src/configs/java.rs
Normal 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,
|
||||
}
|
||||
}
|
||||
}
|
@ -10,9 +10,11 @@ pub mod git_branch;
|
||||
pub mod git_status;
|
||||
pub mod go;
|
||||
pub mod hostname;
|
||||
pub mod java;
|
||||
pub mod jobs;
|
||||
pub mod kubernetes;
|
||||
pub mod memory_usage;
|
||||
pub mod nix_shell;
|
||||
pub mod nodejs;
|
||||
pub mod package;
|
||||
pub mod python;
|
||||
|
25
src/configs/nix_shell.rs
Normal file
25
src/configs/nix_shell.rs
Normal 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,
|
||||
}
|
||||
}
|
||||
}
|
@ -1,9 +1,8 @@
|
||||
use crate::configs::java::JavaConfig;
|
||||
use std::process::Command;
|
||||
use std::process::Output;
|
||||
|
||||
use ansi_term::Color;
|
||||
|
||||
use super::{Context, Module};
|
||||
use super::{Context, Module, RootModuleConfig, SegmentConfig};
|
||||
|
||||
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() {
|
||||
Some(java_version) => {
|
||||
const JAVA_CHAR: &str = "☕ ";
|
||||
|
||||
let mut module = context.new_module("java");
|
||||
let module_style = module
|
||||
.config_value_style("style")
|
||||
.unwrap_or_else(|| Color::Red.dimmed());
|
||||
module.set_style(module_style);
|
||||
let config: JavaConfig = JavaConfig::try_load(module.config);
|
||||
module.set_style(config.style);
|
||||
|
||||
let formatted_version = format_java_version(java_version)?;
|
||||
module.new_segment("symbol", JAVA_CHAR);
|
||||
module.new_segment("version", &formatted_version);
|
||||
module.create_segment("symbol", &config.symbol);
|
||||
module.create_segment("version", &SegmentConfig::new(&formatted_version));
|
||||
|
||||
Some(module)
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
use ansi_term::Color;
|
||||
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"
|
||||
// https://github.com/target/lorri/issues/140
|
||||
@ -23,34 +24,31 @@ use super::{Context, Module};
|
||||
/// - impure // use_name == false in an impure nix-shell
|
||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
let mut module = context.new_module("nix_shell");
|
||||
let config: NixShellConfig = NixShellConfig::try_load(module.config);
|
||||
|
||||
env::var("IN_NIX_SHELL")
|
||||
.ok()
|
||||
.and_then(|shell_type| {
|
||||
if shell_type == "1" || shell_type == "impure" {
|
||||
Some(module.config_value_str("impure_msg").unwrap_or("impure"))
|
||||
} else if shell_type == "pure" {
|
||||
Some(module.config_value_str("pure_msg").unwrap_or("pure"))
|
||||
} else {
|
||||
None
|
||||
module.set_style(config.style);
|
||||
|
||||
let shell_type = env::var("IN_NIX_SHELL").ok()?;
|
||||
let shell_type_segment: SegmentConfig = match shell_type.as_ref() {
|
||||
"1" | "impure" => config.impure_msg,
|
||||
"pure" => config.pure_msg,
|
||||
_ => {
|
||||
return None;
|
||||
}
|
||||
})
|
||||
.map(|shell_type| {
|
||||
if module.config_value_bool("use_name").unwrap_or(false) {
|
||||
match env::var("name").ok() {
|
||||
Some(name) => format!("{} ({})", name, shell_type),
|
||||
None => shell_type.to_string(),
|
||||
};
|
||||
|
||||
if config.use_name {
|
||||
if let Ok(name) = env::var("name") {
|
||||
module.create_segment(
|
||||
"nix_shell",
|
||||
&shell_type_segment.with_value(&format!("{} ({})", name, shell_type_segment.value)),
|
||||
);
|
||||
} else {
|
||||
module.create_segment("nix_shell", &shell_type_segment);
|
||||
}
|
||||
} else {
|
||||
shell_type.to_string()
|
||||
module.create_segment("nix_shell", &shell_type_segment);
|
||||
}
|
||||
})
|
||||
.map(|segment| {
|
||||
let module_style = module
|
||||
.config_value_style("style")
|
||||
.unwrap_or_else(|| Color::Red.bold());
|
||||
module.set_style(module_style);
|
||||
module.new_segment("nix_shell", &segment);
|
||||
module
|
||||
})
|
||||
|
||||
Some(module)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user