mirror of
https://github.com/starship/starship.git
synced 2025-02-02 11:29:40 +01:00
fix: Truncate long paths in conda environment names (#694)
Environment names created via conda create -p [path] tend to be too long for comfort, so this truncates them.
This commit is contained in:
parent
60a1319524
commit
3c835ba34b
@ -289,15 +289,21 @@ prefix = "underwent "
|
|||||||
## Conda
|
## Conda
|
||||||
|
|
||||||
The `conda` module shows the current conda environment, if `$CONDA_DEFAULT_ENV` is set.
|
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`
|
|
||||||
|
::: tip
|
||||||
|
|
||||||
|
This does not suppress conda's own prompt modifier, you may want to run `conda config --set changeps1 False`.
|
||||||
|
|
||||||
|
:::
|
||||||
|
|
||||||
### Options
|
### Options
|
||||||
|
|
||||||
| Variable | Default | Description |
|
| Variable | Default | Description |
|
||||||
| ---------- | -------------- | -------------------------------------------- |
|
| ------------------- | -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| `symbol` | `"C "` | The symbol used before the environment name. |
|
| `truncation_length` | `1` | The number of directories the environment path should be truncated to, if the environment was created via `conda create -p [path]`. `0` means no truncation. Also see the [`directory`](#directory) module. |
|
||||||
| `style` | `"bold green"` | The style for the module. |
|
| `symbol` | `"C "` | The symbol used before the environment name. |
|
||||||
| `disabled` | `false` | Disables the `conda` module. |
|
| `style` | `"bold green"` | The style for the module. |
|
||||||
|
| `disabled` | `false` | Disables the `conda` module. |
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ use starship_module_config_derive::ModuleConfig;
|
|||||||
|
|
||||||
#[derive(Clone, ModuleConfig)]
|
#[derive(Clone, ModuleConfig)]
|
||||||
pub struct CondaConfig<'a> {
|
pub struct CondaConfig<'a> {
|
||||||
|
pub truncation_length: usize,
|
||||||
pub symbol: SegmentConfig<'a>,
|
pub symbol: SegmentConfig<'a>,
|
||||||
pub environment: SegmentConfig<'a>,
|
pub environment: SegmentConfig<'a>,
|
||||||
pub style: Style,
|
pub style: Style,
|
||||||
@ -14,6 +15,7 @@ pub struct CondaConfig<'a> {
|
|||||||
impl<'a> RootModuleConfig<'a> for CondaConfig<'a> {
|
impl<'a> RootModuleConfig<'a> for CondaConfig<'a> {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
CondaConfig {
|
CondaConfig {
|
||||||
|
truncation_length: 1,
|
||||||
symbol: SegmentConfig {
|
symbol: SegmentConfig {
|
||||||
value: "C ",
|
value: "C ",
|
||||||
style: None,
|
style: None,
|
||||||
|
@ -2,6 +2,7 @@ use std::env;
|
|||||||
|
|
||||||
use super::{Context, Module};
|
use super::{Context, Module};
|
||||||
|
|
||||||
|
use super::utils::directory::truncate;
|
||||||
use crate::config::RootModuleConfig;
|
use crate::config::RootModuleConfig;
|
||||||
use crate::configs::conda::CondaConfig;
|
use crate::configs::conda::CondaConfig;
|
||||||
|
|
||||||
@ -18,6 +19,8 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|||||||
let mut module = context.new_module("conda");
|
let mut module = context.new_module("conda");
|
||||||
let config = CondaConfig::try_load(module.config);
|
let config = CondaConfig::try_load(module.config);
|
||||||
|
|
||||||
|
let conda_env = truncate(conda_env, config.truncation_length);
|
||||||
|
|
||||||
module.set_style(config.style);
|
module.set_style(config.style);
|
||||||
|
|
||||||
module.create_segment("symbol", &config.symbol);
|
module.create_segment("symbol", &config.symbol);
|
||||||
|
@ -4,6 +4,7 @@ use unicode_segmentation::UnicodeSegmentation;
|
|||||||
|
|
||||||
use super::{Context, Module};
|
use super::{Context, Module};
|
||||||
|
|
||||||
|
use super::utils::directory::truncate;
|
||||||
use crate::config::{RootModuleConfig, SegmentConfig};
|
use crate::config::{RootModuleConfig, SegmentConfig};
|
||||||
use crate::configs::directory::DirectoryConfig;
|
use crate::configs::directory::DirectoryConfig;
|
||||||
|
|
||||||
@ -137,30 +138,6 @@ const fn replace_c_dir(path: String) -> String {
|
|||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Truncate a path to only have a set number of path components
|
|
||||||
///
|
|
||||||
/// Will truncate a path to only show the last `length` components in a path.
|
|
||||||
/// If a length of `0` is provided, the path will not be truncated.
|
|
||||||
fn truncate(dir_string: String, length: usize) -> String {
|
|
||||||
if length == 0 {
|
|
||||||
return dir_string;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut components = dir_string.split('/').collect::<Vec<&str>>();
|
|
||||||
|
|
||||||
// If the first element is "" then there was a leading "/" and we should remove it so we can check the actual count of components
|
|
||||||
if components[0] == "" {
|
|
||||||
components.remove(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
if components.len() <= length {
|
|
||||||
return dir_string;
|
|
||||||
}
|
|
||||||
|
|
||||||
let truncated_components = &components[components.len() - length..];
|
|
||||||
truncated_components.join("/")
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Takes part before contracted path and replaces it with fish style path
|
/// Takes part before contracted path and replaces it with fish style path
|
||||||
///
|
///
|
||||||
/// Will take the first letter of each directory before the contracted path and
|
/// Will take the first letter of each directory before the contracted path and
|
||||||
@ -258,48 +235,6 @@ mod tests {
|
|||||||
assert_eq!(output, "/c");
|
assert_eq!(output, "/c");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn truncate_smaller_path_than_provided_length() {
|
|
||||||
let path = "~/starship";
|
|
||||||
let output = truncate(path.to_string(), 3);
|
|
||||||
assert_eq!(output, "~/starship")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn truncate_same_path_as_provided_length() {
|
|
||||||
let path = "~/starship/engines";
|
|
||||||
let output = truncate(path.to_string(), 3);
|
|
||||||
assert_eq!(output, "~/starship/engines")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn truncate_slightly_larger_path_than_provided_length() {
|
|
||||||
let path = "~/starship/engines/booster";
|
|
||||||
let output = truncate(path.to_string(), 3);
|
|
||||||
assert_eq!(output, "starship/engines/booster")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn truncate_larger_path_than_provided_length() {
|
|
||||||
let path = "~/starship/engines/booster/rocket";
|
|
||||||
let output = truncate(path.to_string(), 3);
|
|
||||||
assert_eq!(output, "engines/booster/rocket")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn truncate_same_path_as_provided_length_from_root() {
|
|
||||||
let path = "/starship/engines/booster";
|
|
||||||
let output = truncate(path.to_string(), 3);
|
|
||||||
assert_eq!(output, "/starship/engines/booster");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn truncate_larger_path_than_provided_length_from_root() {
|
|
||||||
let path = "/starship/engines/booster/rocket";
|
|
||||||
let output = truncate(path.to_string(), 3);
|
|
||||||
assert_eq!(output, "engines/booster/rocket");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn fish_style_with_user_home_contracted_path() {
|
fn fish_style_with_user_home_contracted_path() {
|
||||||
let path = "~/starship/engines/booster/rocket";
|
let path = "~/starship/engines/booster/rocket";
|
||||||
|
70
src/modules/utils/directory.rs
Normal file
70
src/modules/utils/directory.rs
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
/// Truncate a path to only have a set number of path components
|
||||||
|
///
|
||||||
|
/// Will truncate a path to only show the last `length` components in a path.
|
||||||
|
/// If a length of `0` is provided, the path will not be truncated.
|
||||||
|
pub fn truncate(dir_string: String, length: usize) -> String {
|
||||||
|
if length == 0 {
|
||||||
|
return dir_string;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut components = dir_string.split('/').collect::<Vec<&str>>();
|
||||||
|
|
||||||
|
// If the first element is "" then there was a leading "/" and we should remove it so we can check the actual count of components
|
||||||
|
if components[0] == "" {
|
||||||
|
components.remove(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
if components.len() <= length {
|
||||||
|
return dir_string;
|
||||||
|
}
|
||||||
|
|
||||||
|
let truncated_components = &components[components.len() - length..];
|
||||||
|
truncated_components.join("/")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn truncate_smaller_path_than_provided_length() {
|
||||||
|
let path = "~/starship";
|
||||||
|
let output = truncate(path.to_string(), 3);
|
||||||
|
assert_eq!(output, "~/starship")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn truncate_same_path_as_provided_length() {
|
||||||
|
let path = "~/starship/engines";
|
||||||
|
let output = truncate(path.to_string(), 3);
|
||||||
|
assert_eq!(output, "~/starship/engines")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn truncate_slightly_larger_path_than_provided_length() {
|
||||||
|
let path = "~/starship/engines/booster";
|
||||||
|
let output = truncate(path.to_string(), 3);
|
||||||
|
assert_eq!(output, "starship/engines/booster")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn truncate_larger_path_than_provided_length() {
|
||||||
|
let path = "~/starship/engines/booster/rocket";
|
||||||
|
let output = truncate(path.to_string(), 3);
|
||||||
|
assert_eq!(output, "engines/booster/rocket")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn truncate_same_path_as_provided_length_from_root() {
|
||||||
|
let path = "/starship/engines/booster";
|
||||||
|
let output = truncate(path.to_string(), 3);
|
||||||
|
assert_eq!(output, "/starship/engines/booster");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn truncate_larger_path_than_provided_length_from_root() {
|
||||||
|
let path = "/starship/engines/booster/rocket";
|
||||||
|
let output = truncate(path.to_string(), 3);
|
||||||
|
assert_eq!(output, "engines/booster/rocket");
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1,2 @@
|
|||||||
|
pub mod directory;
|
||||||
pub mod java_version_parser;
|
pub mod java_version_parser;
|
||||||
|
@ -30,3 +30,14 @@ fn env_set() -> io::Result<()> {
|
|||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn truncate() -> io::Result<()> {
|
||||||
|
let output = common::render_module("conda").env_clear().env("CONDA_DEFAULT_ENV", "/some/really/long/and/really/annoying/path/that/shouldnt/be/displayed/fully/conda/my_env").output()?;
|
||||||
|
|
||||||
|
let expected = format!("via {} ", Color::Green.bold().paint("C my_env"));
|
||||||
|
let actual = String::from_utf8(output.stdout).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(expected, actual);
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user