mirror of
https://github.com/starship/starship.git
synced 2025-01-22 06:08:49 +01:00
Merge branch 'master' into conditional-style
This commit is contained in:
commit
82b5b6ea1b
41
.github/config-schema.json
vendored
41
.github/config-schema.json
vendored
@ -1244,6 +1244,20 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"spack": {
|
||||
"default": {
|
||||
"disabled": false,
|
||||
"format": "via [$symbol$environment]($style) ",
|
||||
"style": "blue bold",
|
||||
"symbol": "🅢 ",
|
||||
"truncation_length": 1
|
||||
},
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/SpackConfig"
|
||||
}
|
||||
]
|
||||
},
|
||||
"status": {
|
||||
"default": {
|
||||
"disabled": true,
|
||||
@ -4292,6 +4306,33 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
"SpackConfig": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"truncation_length": {
|
||||
"default": 1,
|
||||
"type": "integer",
|
||||
"format": "uint",
|
||||
"minimum": 0.0
|
||||
},
|
||||
"format": {
|
||||
"default": "via [$symbol$environment]($style) ",
|
||||
"type": "string"
|
||||
},
|
||||
"symbol": {
|
||||
"default": "🅢 ",
|
||||
"type": "string"
|
||||
},
|
||||
"style": {
|
||||
"default": "blue bold",
|
||||
"type": "string"
|
||||
},
|
||||
"disabled": {
|
||||
"default": false,
|
||||
"type": "boolean"
|
||||
}
|
||||
}
|
||||
},
|
||||
"StatusConfig": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
@ -124,6 +124,9 @@ format = '\[[$symbol($version)]($style)\]'
|
||||
[scala]
|
||||
format = '\[[$symbol($version)]($style)\]'
|
||||
|
||||
[spack]
|
||||
format = '\[[$symbol$environment]($style)\]'
|
||||
|
||||
[sudo]
|
||||
format = '\[[as $symbol]\]'
|
||||
|
||||
|
@ -58,5 +58,8 @@ symbol = " "
|
||||
[package]
|
||||
symbol = " "
|
||||
|
||||
[spack]
|
||||
symbol = "🅢 "
|
||||
|
||||
[rust]
|
||||
symbol = " "
|
||||
|
@ -112,6 +112,9 @@ symbol = "rs "
|
||||
[scala]
|
||||
symbol = "scala "
|
||||
|
||||
[spack]
|
||||
symbol = "spack "
|
||||
|
||||
[sudo]
|
||||
symbol = "sudo "
|
||||
|
||||
|
@ -247,6 +247,7 @@ $vagrant\
|
||||
$zig\
|
||||
$nix_shell\
|
||||
$conda\
|
||||
$spack\
|
||||
$memory_usage\
|
||||
$aws\
|
||||
$gcloud\
|
||||
@ -3167,6 +3168,39 @@ and `$SINGULARITY_NAME` is set.
|
||||
format = '[📦 \[$env\]]($style) '
|
||||
```
|
||||
|
||||
## Spack
|
||||
|
||||
The `spack` module shows the current [Spack](https://spack.readthedocs.io/en/latest/) environment, if `$SPACK_ENV` is set.
|
||||
|
||||
### Options
|
||||
|
||||
| Option | Default | Description |
|
||||
| ------------------- | -------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||
| `truncation_length` | `1` | The number of directories the environment path should be truncated to. `0` means no truncation. Also see the [`directory`](#directory) module. |
|
||||
| `symbol` | `"🅢 "` | The symbol used before the environment name. |
|
||||
| `style` | `"bold blue"` | The style for the module. |
|
||||
| `format` | `"via [$symbol$environment]($style) "` | The format for the module. |
|
||||
| `disabled` | `false` | Disables the `spack` module. |
|
||||
|
||||
### Variables
|
||||
|
||||
| Variable | Example | Description |
|
||||
| ----------- | ------------ | ------------------------------------ |
|
||||
| environment | `astronauts` | The current spack environment |
|
||||
| symbol | | Mirrors the value of option `symbol` |
|
||||
| style\* | | Mirrors the value of option `style` |
|
||||
|
||||
*: This variable can only be used as a part of a style string
|
||||
|
||||
### Example
|
||||
|
||||
```toml
|
||||
# ~/.config/starship.toml
|
||||
|
||||
[spack]
|
||||
format = "[$symbol$environment](dimmed blue) "
|
||||
```
|
||||
|
||||
## Status
|
||||
|
||||
The `status` module displays the exit code of the previous command.
|
||||
|
@ -63,6 +63,7 @@ pub mod scala;
|
||||
pub mod shell;
|
||||
pub mod shlvl;
|
||||
pub mod singularity;
|
||||
pub mod spack;
|
||||
mod starship_root;
|
||||
pub mod status;
|
||||
pub mod sudo;
|
||||
@ -210,6 +211,8 @@ pub struct FullConfig<'a> {
|
||||
#[serde(borrow)]
|
||||
singularity: singularity::SingularityConfig<'a>,
|
||||
#[serde(borrow)]
|
||||
spack: spack::SpackConfig<'a>,
|
||||
#[serde(borrow)]
|
||||
status: status::StatusConfig<'a>,
|
||||
#[serde(borrow)]
|
||||
sudo: sudo::SudoConfig<'a>,
|
||||
|
24
src/configs/spack.rs
Normal file
24
src/configs/spack.rs
Normal file
@ -0,0 +1,24 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
#[cfg_attr(feature = "config-schema", derive(schemars::JsonSchema))]
|
||||
#[serde(default)]
|
||||
pub struct SpackConfig<'a> {
|
||||
pub truncation_length: usize,
|
||||
pub format: &'a str,
|
||||
pub symbol: &'a str,
|
||||
pub style: &'a str,
|
||||
pub disabled: bool,
|
||||
}
|
||||
|
||||
impl<'a> Default for SpackConfig<'a> {
|
||||
fn default() -> Self {
|
||||
SpackConfig {
|
||||
truncation_length: 1,
|
||||
format: "via [$symbol$environment]($style) ",
|
||||
symbol: "🅢 ",
|
||||
style: "blue bold",
|
||||
disabled: false,
|
||||
}
|
||||
}
|
||||
}
|
@ -74,6 +74,7 @@ pub const PROMPT_ORDER: &[&str] = &[
|
||||
"buf",
|
||||
"nix_shell",
|
||||
"conda",
|
||||
"spack",
|
||||
"memory_usage",
|
||||
"aws",
|
||||
"gcloud",
|
||||
|
@ -99,6 +99,9 @@ else
|
||||
fi
|
||||
fi
|
||||
|
||||
# Ensure that $COLUMNS gets set
|
||||
shopt -s checkwinsize
|
||||
|
||||
# Set up the start time and STARSHIP_SHELL, which controls shell-specific sequences
|
||||
STARSHIP_START_TIME=$(::STARSHIP:: time)
|
||||
export STARSHIP_SHELL="bash"
|
||||
|
@ -9,7 +9,7 @@ let-env PROMPT_INDICATOR = ""
|
||||
let-env PROMPT_COMMAND = {
|
||||
# jobs are not supported
|
||||
let width = (term size -c | get columns | into string)
|
||||
^::STARSHIP:: prompt --cmd-duration $env.CMD_DURATION_MS --status $env.LAST_EXIT_CODE --terminal-width $width
|
||||
^::STARSHIP:: prompt $"--cmd-duration=($env.CMD_DURATION_MS)" $"--status=($env.LAST_EXIT_CODE)" $"--terminal-width=($width)"
|
||||
}
|
||||
|
||||
# Not well-suited for `starship prompt --right`.
|
||||
|
@ -70,6 +70,7 @@ pub const ALL_MODULES: &[&str] = &[
|
||||
"shell",
|
||||
"shlvl",
|
||||
"singularity",
|
||||
"spack",
|
||||
"status",
|
||||
"sudo",
|
||||
"swift",
|
||||
|
@ -107,7 +107,9 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
let after_repo_root = contracted_path.replacen(repo_path_vec[0], "", 1);
|
||||
let num_segments_after_root = after_repo_root.split('/').count();
|
||||
|
||||
if ((num_segments_after_root - 1) as i64) < config.truncation_length {
|
||||
if config.truncation_length == 0
|
||||
|| ((num_segments_after_root - 1) as i64) < config.truncation_length
|
||||
{
|
||||
let root = repo_path_vec[0];
|
||||
let before = dir_string.replace(&contracted_path, "");
|
||||
[prefix + &before, root.to_string(), after_repo_root]
|
||||
@ -1671,6 +1673,36 @@ mod tests {
|
||||
assert_eq!(expected, actual);
|
||||
tmp_dir.close()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn highlight_git_root_dir_zero_truncation_length() -> io::Result<()> {
|
||||
let (tmp_dir, _) = make_known_tempdir(Path::new("/tmp"))?;
|
||||
let repo_dir = tmp_dir.path().join("above").join("repo");
|
||||
let dir = repo_dir.join("src/sub/path");
|
||||
fs::create_dir_all(&dir)?;
|
||||
init_repo(&repo_dir).unwrap();
|
||||
|
||||
let actual = ModuleRenderer::new("directory")
|
||||
.config(toml::toml! {
|
||||
[directory]
|
||||
truncation_length = 0
|
||||
truncate_to_repo = false
|
||||
repo_root_style = "green"
|
||||
})
|
||||
.path(dir)
|
||||
.collect();
|
||||
let expected = Some(format!(
|
||||
"{}{}repo{} ",
|
||||
Color::Cyan.bold().paint(convert_path_sep(
|
||||
tmp_dir.path().join("above/").to_str().unwrap()
|
||||
)),
|
||||
Color::Green.prefix(),
|
||||
Color::Cyan.bold().paint(convert_path_sep("/src/sub/path"))
|
||||
));
|
||||
assert_eq!(expected, actual);
|
||||
tmp_dir.close()
|
||||
}
|
||||
|
||||
// sample for invalid unicode from https://doc.rust-lang.org/std/ffi/struct.OsStr.html#method.to_string_lossy
|
||||
#[cfg(any(unix, target_os = "redox"))]
|
||||
fn invalid_path() -> PathBuf {
|
||||
|
@ -60,6 +60,7 @@ mod scala;
|
||||
mod shell;
|
||||
mod shlvl;
|
||||
mod singularity;
|
||||
mod spack;
|
||||
mod status;
|
||||
mod sudo;
|
||||
mod swift;
|
||||
@ -150,6 +151,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
|
||||
"shell" => shell::module(context),
|
||||
"shlvl" => shlvl::module(context),
|
||||
"singularity" => singularity::module(context),
|
||||
"spack" => spack::module(context),
|
||||
"swift" => swift::module(context),
|
||||
"status" => status::module(context),
|
||||
"sudo" => sudo::module(context),
|
||||
@ -245,6 +247,7 @@ pub fn description(module: &str) -> &'static str {
|
||||
"shell" => "The currently used shell indicator",
|
||||
"shlvl" => "The current value of SHLVL",
|
||||
"singularity" => "The currently used Singularity image",
|
||||
"spack" => "The current spack environment, if $SPACK_ENV is set",
|
||||
"status" => "The status of the last command",
|
||||
"sudo" => "The sudo credentials are currently cached",
|
||||
"swift" => "The currently installed version of Swift",
|
||||
|
84
src/modules/spack.rs
Normal file
84
src/modules/spack.rs
Normal file
@ -0,0 +1,84 @@
|
||||
use super::{Context, Module, ModuleConfig};
|
||||
|
||||
use super::utils::directory::truncate;
|
||||
use crate::configs::spack::SpackConfig;
|
||||
use crate::formatter::StringFormatter;
|
||||
|
||||
/// Creates a module with the current Spack environment
|
||||
///
|
||||
/// Will display the Spack environment if `$SPACK_ENV` is set.
|
||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
let spack_env = context.get_env("SPACK_ENV").unwrap_or_default();
|
||||
if spack_env.trim().is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut module = context.new_module("spack");
|
||||
let config: SpackConfig = SpackConfig::try_load(module.config);
|
||||
|
||||
let spack_env = truncate(&spack_env, config.truncation_length).unwrap_or(spack_env);
|
||||
|
||||
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
||||
formatter
|
||||
.map_meta(|variable, _| match variable {
|
||||
"symbol" => Some(config.symbol),
|
||||
_ => None,
|
||||
})
|
||||
.map_style(|variable| match variable {
|
||||
"style" => Some(Ok(config.style)),
|
||||
_ => None,
|
||||
})
|
||||
.map(|variable| match variable {
|
||||
"environment" => Some(Ok(spack_env.as_str())),
|
||||
_ => None,
|
||||
})
|
||||
.parse(None, Some(context))
|
||||
});
|
||||
|
||||
module.set_segments(match parsed {
|
||||
Ok(segments) => segments,
|
||||
Err(error) => {
|
||||
log::warn!("Error in module `spack`:\n{}", error);
|
||||
return None;
|
||||
}
|
||||
});
|
||||
|
||||
Some(module)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::test::ModuleRenderer;
|
||||
use ansi_term::Color;
|
||||
|
||||
#[test]
|
||||
fn not_in_env() {
|
||||
let actual = ModuleRenderer::new("spack").collect();
|
||||
|
||||
let expected = None;
|
||||
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn env_set() {
|
||||
let actual = ModuleRenderer::new("spack")
|
||||
.env("SPACK_ENV", "astronauts")
|
||||
.collect();
|
||||
|
||||
let expected = Some(format!("via {} ", Color::Blue.bold().paint("🅢 astronauts")));
|
||||
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn truncate() {
|
||||
let actual = ModuleRenderer::new("spack")
|
||||
.env("SPACK_ENV", "/some/really/long/and/really/annoying/path/that/shouldnt/be/displayed/fully/spack/my_env")
|
||||
.collect();
|
||||
|
||||
let expected = Some(format!("via {} ", Color::Blue.bold().paint("🅢 my_env")));
|
||||
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user