diff options
author | AppleTheGolden <scotsbox@protonmail.com> | 2019-10-05 20:25:25 +0200 |
---|---|---|
committer | Kevin Song <chipbuster@users.noreply.github.com> | 2019-10-05 13:25:25 -0500 |
commit | 7657af068070b166bf468909bc1c71e8d9ff1b41 (patch) | |
tree | 0f0b534f40170d52f0ee0b05b1a0dbdddacee0c3 | |
parent | e911e76e6bb79788c2d614718a53bb9082f05103 (diff) |
feat: Add conda module (#469)
-rw-r--r-- | docs/config/README.md | 23 | ||||
-rw-r--r-- | src/configs/conda.rs | 29 | ||||
-rw-r--r-- | src/configs/mod.rs | 2 | ||||
-rw-r--r-- | src/module.rs | 1 | ||||
-rw-r--r-- | src/modules/conda.rs | 27 | ||||
-rw-r--r-- | src/modules/mod.rs | 4 | ||||
-rw-r--r-- | tests/testsuite/conda.rs | 29 | ||||
-rw-r--r-- | tests/testsuite/main.rs | 1 |
8 files changed, 115 insertions, 1 deletions
diff --git a/docs/config/README.md b/docs/config/README.md index b2870b023..2f07909b6 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 000000000..7ce644683 --- /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 16461792d..a8e8302dc 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 b06bf5529..846c09ef5 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 000000000..4670af9a4 --- /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<Module<'a>> { + // 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 5a0b8ddcb..9be790cfe 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<Module<'a>> { "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 000000000..efae25a50 --- /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 f96eadcfd..6efa3cfb8 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; |