feat: Add conda module (#469)

This commit is contained in:
AppleTheGolden 2019-10-05 20:25:25 +02:00 committed by Kevin Song
parent e911e76e6b
commit 7657af0680
8 changed files with 115 additions and 1 deletions

View File

@ -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

29
src/configs/conda.rs Normal file
View File

@ -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,
}
}
}

View File

@ -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",

View File

@ -13,6 +13,7 @@ pub const ALL_MODULES: &[&str] = &[
"battery",
"character",
"cmd_duration",
"conda",
"directory",
"dotnet",
"env_var",

27
src/modules/conda.rs Normal file
View File

@ -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)
}

View File

@ -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),

29
tests/testsuite/conda.rs Normal file
View File

@ -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(())
}

View File

@ -2,6 +2,7 @@ mod aws;
mod character;
mod cmd_duration;
mod common;
mod conda;
mod configuration;
mod directory;
mod dotnet;