1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-12-12 06:07:56 +00:00
starship/src/modules/env_var.rs

43 lines
1.5 KiB
Rust
Raw Normal View History

use std::env;
use super::{Context, Module, SegmentConfig};
use crate::config::RootModuleConfig;
use crate::configs::env_var::EnvVarConfig;
/// Creates a module with the value of the chosen environment variable
///
/// Will display the environment variable's value if all of the following criteria are met:
/// - env_var.disabled is absent or false
/// - env_var.variable is defined
/// - a variable named as the value of env_var.variable is defined
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
let mut module = context.new_module("env_var");
let config: EnvVarConfig = EnvVarConfig::try_load(module.config);
let env_value = get_env_value(config.variable?, config.default)?;
module.set_style(config.style);
module.get_prefix().set_value("with ");
if let Some(symbol) = config.symbol {
module.create_segment("symbol", &symbol);
}
// TODO: Use native prefix and suffix instead of stacking custom ones together with env_value.
let env_var_stacked = format!("{}{}{}", config.prefix, env_value, config.suffix);
module.create_segment("env_var", &SegmentConfig::new(&env_var_stacked));
Some(module)
}
fn get_env_value(name: &str, default: Option<&str>) -> Option<String> {
match env::var_os(name) {
Some(os_value) => match os_value.into_string() {
Ok(value) => Some(value),
Err(_error) => None,
},
None => default.map(|value| value.to_owned()),
}
}