summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorBruno Bigras <bigras.bruno@gmail.com>2019-08-25 11:41:20 -0400
committerMatan Kushner <hello@matchai.me>2019-08-25 11:41:20 -0400
commitfeb737190eccc9f9b3e8f27dd3c3de8ad201cecd (patch)
tree931d9188bf2a8bf197bb744c851b8a1311ad0bdf /src
parent57e807fec61d1950949474f470ead265b3ae201f (diff)
Add nix-shell support (#173)
Diffstat (limited to 'src')
-rw-r--r--src/modules/mod.rs2
-rw-r--r--src/modules/nix_shell.rs54
-rw-r--r--src/print.rs1
3 files changed, 57 insertions, 0 deletions
diff --git a/src/modules/mod.rs b/src/modules/mod.rs
index 4aa67108e..46929c314 100644
--- a/src/modules/mod.rs
+++ b/src/modules/mod.rs
@@ -8,6 +8,7 @@ mod git_status;
mod golang;
mod jobs;
mod line_break;
+mod nix_shell;
mod nodejs;
mod package;
mod python;
@@ -35,6 +36,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
"battery" => battery::module(context),
"cmd_duration" => cmd_duration::module(context),
"jobs" => jobs::module(context),
+ "nix_shell" => nix_shell::module(context),
_ => {
eprintln!("Error: Unknown module {}. Use starship module --list to list out all supported modules.", module);
diff --git a/src/modules/nix_shell.rs b/src/modules/nix_shell.rs
new file mode 100644
index 000000000..b37fcdcf4
--- /dev/null
+++ b/src/modules/nix_shell.rs
@@ -0,0 +1,54 @@
+use ansi_term::Color;
+use std::env;
+
+use super::{Context, Module};
+
+// IN_NIX_SHELL should be "pure" or "impure" but lorri uses "1" for "impure"
+// https://github.com/target/lorri/issues/140
+
+/// 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:
+/// - use_name (bool) // print the name of the nix-shell
+/// - impure_msg (string) // change the impure msg
+/// - pure_msg (string) // change the pure msg
+///
+/// Will display the following:
+/// - name (pure) // use_name == true in a pure nix-shell
+/// - name (impure) // use_name == true in an impure nix-shell
+/// - pure // use_name == false in a pure nix-shell
+/// - 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")?;
+
+ 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
+ }
+ })
+ .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(),
+ }
+ } else {
+ shell_type.to_string()
+ }
+ })
+ .map(|segment| {
+ let module_color = Color::Red.bold();
+ module.set_style(module_color);
+ module.new_segment("nix_shell", &segment);
+ module
+ })
+}
diff --git a/src/print.rs b/src/print.rs
index dc62dd94d..56ba2287f 100644
--- a/src/print.rs
+++ b/src/print.rs
@@ -22,6 +22,7 @@ const DEFAULT_PROMPT_ORDER: &[&str] = &[
"rust",
"python",
"golang",
+ "nix_shell",
"cmd_duration",
"line_break",
"jobs",