diff --git a/docs/config/README.md b/docs/config/README.md index b2870b02..2f07909b 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -99,6 +99,7 @@ prompt_order = [ "ruby", "rust", "nix_shell", + "conda", "memory_usage", "aws", "env_var", @@ -276,6 +277,28 @@ min_time = 4 prefix = "underwent " ``` +## Conda + +The `conda` module shows the current conda environment, if `$CONDA_DEFAULT_ENV` is set. +Note: This does not suppress conda's own prompt modifier, you may want to run `conda config --set changeps1 False` + +### Options + +| Variable | Default | Description | +| ---------- | -------------- | -------------------------------------------- | +| `symbol` | `"C "` | The symbol used before the environment name. | +| `style` | `"bold green"` | The style for the module. | +| `disabled` | `false` | Disables the `conda` module. | + +### Example + +```toml +# ~/.config/starship.toml + +[conda] +style = "dimmed green" +``` + ## Directory The `directory` module shows the path to your current directory, truncated to diff --git a/src/configs/conda.rs b/src/configs/conda.rs new file mode 100644 index 00000000..7ce64468 --- /dev/null +++ b/src/configs/conda.rs @@ -0,0 +1,29 @@ +use crate::config::{ModuleConfig, RootModuleConfig, SegmentConfig}; + +use ansi_term::{Color, Style}; +use starship_module_config_derive::ModuleConfig; + +#[derive(Clone, ModuleConfig)] +pub struct CondaConfig<'a> { + pub symbol: SegmentConfig<'a>, + pub environment: SegmentConfig<'a>, + pub style: Style, + pub disabled: bool, +} + +impl<'a> RootModuleConfig<'a> for CondaConfig<'a> { + fn new() -> Self { + CondaConfig { + symbol: SegmentConfig { + value: "C ", + style: None, + }, + environment: SegmentConfig { + value: "", + style: None, + }, + style: Color::Green.bold(), + disabled: false, + } + } +} diff --git a/src/configs/mod.rs b/src/configs/mod.rs index 16461792..a8e8302d 100644 --- a/src/configs/mod.rs +++ b/src/configs/mod.rs @@ -1,6 +1,7 @@ pub mod aws; pub mod battery; pub mod character; +pub mod conda; pub mod dotnet; pub mod kubernetes; pub mod rust; @@ -43,6 +44,7 @@ impl<'a> RootModuleConfig<'a> for StarshipRootConfig<'a> { "rust", // ↑ Toolchain version modules ↑ "nix_shell", + "conda", "memory_usage", "aws", "env_var", diff --git a/src/module.rs b/src/module.rs index b06bf552..846c09ef 100644 --- a/src/module.rs +++ b/src/module.rs @@ -13,6 +13,7 @@ pub const ALL_MODULES: &[&str] = &[ "battery", "character", "cmd_duration", + "conda", "directory", "dotnet", "env_var", diff --git a/src/modules/conda.rs b/src/modules/conda.rs new file mode 100644 index 00000000..4670af9a --- /dev/null +++ b/src/modules/conda.rs @@ -0,0 +1,27 @@ +use std::env; + +use super::{Context, Module}; + +use crate::config::RootModuleConfig; +use crate::configs::conda::CondaConfig; + +/// Creates a module with the current Conda environment +/// +/// Will display the Conda environment iff `$CONDA_DEFAULT_ENV` is set. +pub fn module<'a>(context: &'a Context) -> Option> { + // Reference implementation: https://github.com/denysdovhan/spaceship-prompt/blob/master/sections/conda.zsh + let conda_env = env::var("CONDA_DEFAULT_ENV").ok()?; + if conda_env.is_empty() { + return None; + } + + let mut module = context.new_module("conda"); + let config = CondaConfig::try_load(module.config); + + module.set_style(config.style); + + module.create_segment("symbol", &config.symbol); + module.create_segment("environment", &config.environment.with_value(&conda_env)); + + Some(module) +} diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 5a0b8ddc..9be790cf 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -2,6 +2,7 @@ mod aws; mod character; mod cmd_duration; +mod conda; mod directory; mod dotnet; mod env_var; @@ -37,9 +38,10 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option> { "aws" => aws::module(context), #[cfg(feature = "battery")] "battery" => battery::module(context), - "directory" => directory::module(context), "character" => character::module(context), "cmd_duration" => cmd_duration::module(context), + "conda" => conda::module(context), + "directory" => directory::module(context), "dotnet" => dotnet::module(context), "env_var" => env_var::module(context), "git_branch" => git_branch::module(context), diff --git a/tests/testsuite/conda.rs b/tests/testsuite/conda.rs new file mode 100644 index 00000000..efae25a5 --- /dev/null +++ b/tests/testsuite/conda.rs @@ -0,0 +1,29 @@ +use ansi_term::Color; +use std::io; + +use crate::common; + +#[test] +fn not_in_env() -> io::Result<()> { + let output = common::render_module("conda").env_clear().output()?; + + let expected = ""; + let actual = String::from_utf8(output.stdout).unwrap(); + + assert_eq!(expected, actual); + Ok(()) +} + +#[test] +fn env_set() -> io::Result<()> { + let output = common::render_module("conda") + .env_clear() + .env("CONDA_DEFAULT_ENV", "astronauts") + .output()?; + + let expected = format!("via {} ", Color::Green.bold().paint("C astronauts")); + let actual = String::from_utf8(output.stdout).unwrap(); + + assert_eq!(expected, actual); + Ok(()) +} diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index f96eadcf..6efa3cfb 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -2,6 +2,7 @@ mod aws; mod character; mod cmd_duration; mod common; +mod conda; mod configuration; mod directory; mod dotnet;