2019-08-25 15:41:20 +00:00
|
|
|
use std::env;
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
use super::{Context, Module, RootModuleConfig};
|
2019-11-06 12:59:12 +00:00
|
|
|
|
|
|
|
use crate::configs::nix_shell::NixShellConfig;
|
2020-07-07 22:45:32 +00:00
|
|
|
use crate::formatter::StringFormatter;
|
2019-08-25 15:41:20 +00:00
|
|
|
|
|
|
|
/// Creates a module showing if inside a nix-shell
|
|
|
|
///
|
|
|
|
/// The module will use the `$IN_NIX_SHELL` and `$name` environment variable to
|
|
|
|
/// determine if it's inside a nix-shell and the name of it.
|
|
|
|
///
|
|
|
|
/// The following options are availables:
|
|
|
|
/// - impure_msg (string) // change the impure msg
|
|
|
|
/// - pure_msg (string) // change the pure msg
|
|
|
|
///
|
|
|
|
/// Will display the following:
|
2020-07-07 22:45:32 +00:00
|
|
|
/// - pure (name) // $name == "name" in a pure nix-shell
|
|
|
|
/// - impure (name) // $name == "name" in an impure nix-shell
|
|
|
|
/// - pure // $name == "" in a pure nix-shell
|
|
|
|
/// - impure // $name == "" in an impure nix-shell
|
2019-08-25 15:41:20 +00:00
|
|
|
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
2019-09-09 23:14:38 +00:00
|
|
|
let mut module = context.new_module("nix_shell");
|
2019-11-06 12:59:12 +00:00
|
|
|
let config: NixShellConfig = NixShellConfig::try_load(module.config);
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let shell_name = env::var("name").ok();
|
2019-11-06 12:59:12 +00:00
|
|
|
let shell_type = env::var("IN_NIX_SHELL").ok()?;
|
2020-07-07 22:45:32 +00:00
|
|
|
let shell_type_format = match shell_type.as_ref() {
|
|
|
|
"impure" => config.impure_msg,
|
2019-11-06 12:59:12 +00:00
|
|
|
"pure" => config.pure_msg,
|
|
|
|
_ => {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-07 22:45:32 +00:00
|
|
|
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
|
|
|
formatter
|
|
|
|
.map_meta(|variable, _| match variable {
|
|
|
|
"symbol" => Some(config.symbol),
|
|
|
|
"state" => Some(shell_type_format),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map_style(|variable| match variable {
|
|
|
|
"style" => Some(Ok(config.style)),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.map(|variable| match variable {
|
|
|
|
"name" => shell_name.as_ref().map(Ok),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.parse(None)
|
|
|
|
});
|
|
|
|
|
|
|
|
module.set_segments(match parsed {
|
|
|
|
Ok(segments) => segments,
|
|
|
|
Err(error) => {
|
|
|
|
log::warn!("Error in module `nix_shell`:\n{}", error);
|
|
|
|
return None;
|
2019-11-06 12:59:12 +00:00
|
|
|
}
|
2020-07-07 22:45:32 +00:00
|
|
|
});
|
2019-08-25 15:41:20 +00:00
|
|
|
|
2019-11-06 12:59:12 +00:00
|
|
|
Some(module)
|
2019-08-25 15:41:20 +00:00
|
|
|
}
|