mirror of
https://github.com/starship/starship.git
synced 2026-06-23 02:05:51 +07:00
feat: Add Environment Variable module (#409)
This commit is contained in:
@@ -12,6 +12,7 @@ pub const ALL_MODULES: &[&str] = &[
|
||||
"character",
|
||||
"cmd_duration",
|
||||
"directory",
|
||||
"env_var",
|
||||
"git_branch",
|
||||
"git_state",
|
||||
"git_status",
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
use ansi_term::Color;
|
||||
use std::env;
|
||||
|
||||
use super::{Context, Module};
|
||||
|
||||
/// 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 module_style = module
|
||||
.config_value_style("style")
|
||||
.unwrap_or_else(|| Color::Black.bold().dimmed());
|
||||
|
||||
let env_name = module.config_value_str("variable")?;
|
||||
|
||||
let default_value = module.config_value_str("default");
|
||||
|
||||
let env_value = get_env_value(env_name, default_value)?;
|
||||
|
||||
let prefix = module.config_value_str("prefix").unwrap_or("").to_owned();
|
||||
let suffix = module.config_value_str("suffix").unwrap_or("").to_owned();
|
||||
|
||||
module.set_style(module_style);
|
||||
module.get_prefix().set_value("with ");
|
||||
module.new_segment_if_config_exists("symbol");
|
||||
module.new_segment("env_var", &format!("{}{}{}", prefix, env_value, suffix));
|
||||
|
||||
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()),
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ mod aws;
|
||||
mod character;
|
||||
mod cmd_duration;
|
||||
mod directory;
|
||||
mod env_var;
|
||||
mod git_branch;
|
||||
mod git_state;
|
||||
mod git_status;
|
||||
@@ -30,6 +31,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
|
||||
match module {
|
||||
"aws" => aws::module(context),
|
||||
"directory" => directory::module(context),
|
||||
"env_var" => env_var::module(context),
|
||||
"character" => character::module(context),
|
||||
"nodejs" => nodejs::module(context),
|
||||
"rust" => rust::module(context),
|
||||
|
||||
+2
-1
@@ -15,7 +15,6 @@ const DEFAULT_PROMPT_ORDER: &[&str] = &[
|
||||
"username",
|
||||
"hostname",
|
||||
"directory",
|
||||
"aws",
|
||||
"git_branch",
|
||||
"git_state",
|
||||
"git_status",
|
||||
@@ -27,6 +26,8 @@ const DEFAULT_PROMPT_ORDER: &[&str] = &[
|
||||
"golang",
|
||||
"java",
|
||||
"nix_shell",
|
||||
"aws",
|
||||
"env_var",
|
||||
"cmd_duration",
|
||||
"line_break",
|
||||
"jobs",
|
||||
|
||||
Reference in New Issue
Block a user