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
e9a1749ab4
47
.github/config-schema.json
vendored
47
.github/config-schema.json
vendored
@ -871,6 +871,21 @@
|
||||
}
|
||||
]
|
||||
},
|
||||
"meson": {
|
||||
"default": {
|
||||
"disabled": false,
|
||||
"format": "via [$symbol$project]($style) ",
|
||||
"style": "blue bold",
|
||||
"symbol": "⬢ ",
|
||||
"truncation_length": 4294967295,
|
||||
"truncation_symbol": "…"
|
||||
},
|
||||
"allOf": [
|
||||
{
|
||||
"$ref": "#/definitions/MesonConfig"
|
||||
}
|
||||
]
|
||||
},
|
||||
"nim": {
|
||||
"default": {
|
||||
"detect_extensions": [
|
||||
@ -3682,6 +3697,38 @@
|
||||
},
|
||||
"additionalProperties": false
|
||||
},
|
||||
"MesonConfig": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"truncation_length": {
|
||||
"default": 4294967295,
|
||||
"type": "integer",
|
||||
"format": "uint32",
|
||||
"minimum": 0.0
|
||||
},
|
||||
"truncation_symbol": {
|
||||
"default": "…",
|
||||
"type": "string"
|
||||
},
|
||||
"format": {
|
||||
"default": "via [$symbol$project]($style) ",
|
||||
"type": "string"
|
||||
},
|
||||
"symbol": {
|
||||
"default": "⬢ ",
|
||||
"type": "string"
|
||||
},
|
||||
"style": {
|
||||
"default": "blue bold",
|
||||
"type": "string"
|
||||
},
|
||||
"disabled": {
|
||||
"default": false,
|
||||
"type": "boolean"
|
||||
}
|
||||
},
|
||||
"additionalProperties": false
|
||||
},
|
||||
"NimConfig": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
12
Cargo.lock
generated
12
Cargo.lock
generated
@ -323,9 +323,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "clap"
|
||||
version = "4.0.12"
|
||||
version = "4.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "385007cbbed899260395a4107435fead4cad80684461b3cc78238bdcb0bad58f"
|
||||
checksum = "69d64e88428747154bd8bc378d178377ef4dace7a5735ca1f3855be72f2c2cb5"
|
||||
dependencies = [
|
||||
"atty",
|
||||
"bitflags",
|
||||
@ -349,9 +349,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "4.0.10"
|
||||
version = "4.0.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "db342ce9fda24fb191e2ed4e102055a4d381c1086a06630174cd8da8d5d917ce"
|
||||
checksum = "c42f169caba89a7d512b5418b09864543eeb4d497416c917d7137863bd2076ad"
|
||||
dependencies = [
|
||||
"heck 0.4.0",
|
||||
"proc-macro-error",
|
||||
@ -1470,9 +1470,9 @@ checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70"
|
||||
|
||||
[[package]]
|
||||
name = "home"
|
||||
version = "0.5.3"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2456aef2e6b6a9784192ae780c0f15bc57df0e918585282325e8c8ac27737654"
|
||||
checksum = "747309b4b440c06d57b0b25f2aee03ee9b5e5397d288c60e21fc709bb98a7408"
|
||||
dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
@ -36,7 +36,7 @@ notify = ["notify-rust"]
|
||||
|
||||
[dependencies]
|
||||
chrono = { version = "0.4.22", features = ["clock", "std"] }
|
||||
clap = { version = "4.0.12", features = ["derive", "cargo", "unicode"] }
|
||||
clap = { version = "4.0.13", features = ["derive", "cargo", "unicode"] }
|
||||
clap_complete = "4.0.2"
|
||||
dirs-next = "2.0.0"
|
||||
dunce = "1.0.3"
|
||||
@ -85,7 +85,7 @@ yaml-rust = "0.4.5"
|
||||
process_control = { version = "4.0.0", features = ["crossbeam-channel"] }
|
||||
|
||||
guess_host_triple = "0.1.3"
|
||||
home = "0.5.3"
|
||||
home = "0.5.4"
|
||||
shell-words = "1.1.0"
|
||||
|
||||
[dependencies.schemars]
|
||||
|
@ -85,6 +85,9 @@ format = '\[[$symbol($version)]($style)\]'
|
||||
[memory_usage]
|
||||
format = '\[$symbol[$ram( | $swap)]($style)\]'
|
||||
|
||||
[meson]
|
||||
format = '\[[$symbol$project]($style)\]'
|
||||
|
||||
[nim]
|
||||
format = '\[[$symbol($version)]($style)\]'
|
||||
|
||||
|
@ -49,6 +49,9 @@ symbol = " "
|
||||
[memory_usage]
|
||||
symbol = " "
|
||||
|
||||
[meson]
|
||||
symbol = "喝 "
|
||||
|
||||
[nim]
|
||||
symbol = " "
|
||||
|
||||
@ -73,5 +76,8 @@ symbol = " "
|
||||
[rust]
|
||||
symbol = " "
|
||||
|
||||
[scala]
|
||||
symbol = " "
|
||||
|
||||
[spack]
|
||||
symbol = "🅢 "
|
||||
|
@ -49,6 +49,9 @@ format = 'via [$symbol]($style)'
|
||||
[lua]
|
||||
format = 'via [$symbol]($style)'
|
||||
|
||||
[meson]
|
||||
format = 'via [$symbol]($style)'
|
||||
|
||||
[nim]
|
||||
format = 'via [$symbol]($style)'
|
||||
|
||||
|
@ -17,6 +17,7 @@ $julia\
|
||||
$nodejs\
|
||||
$nim\
|
||||
$rust\
|
||||
$scala\
|
||||
[](fg:#86BBD8 bg:#06969A)\
|
||||
$docker_context\
|
||||
[](fg:#06969A bg:#33658A)\
|
||||
@ -117,6 +118,11 @@ symbol = ""
|
||||
style = "bg:#86BBD8"
|
||||
format = '[ $symbol ($version) ]($style)'
|
||||
|
||||
[scala]
|
||||
symbol = " "
|
||||
style = "bg:#86BBD8"
|
||||
format = '[ $symbol ($version) ]($style)'
|
||||
|
||||
[time]
|
||||
disabled = false
|
||||
time_format = "%R" # Hour:Minute Format
|
||||
|
@ -85,6 +85,9 @@ symbol = "nodejs "
|
||||
[memory_usage]
|
||||
symbol = "memory "
|
||||
|
||||
[meson]
|
||||
symbol = "meson "
|
||||
|
||||
[nim]
|
||||
symbol = "nim "
|
||||
|
||||
|
@ -278,6 +278,7 @@ $zig\
|
||||
$buf\
|
||||
$nix_shell\
|
||||
$conda\
|
||||
$meson\
|
||||
$spack\
|
||||
$memory_usage\
|
||||
$aws\
|
||||
@ -2384,6 +2385,45 @@ symbol = " "
|
||||
style = "bold dimmed green"
|
||||
```
|
||||
|
||||
## Meson
|
||||
|
||||
The `meson` module shows the current Meson developer environment status.
|
||||
|
||||
By default the Meson project name is displayed, if `$MESON_DEVENV` is set.
|
||||
|
||||
### Options
|
||||
|
||||
| Option | Default | Description |
|
||||
| ------------------- | ---------------------------------- | ----------------------------------------------------------------------------------------- |
|
||||
| `truncation_length` | `2^32 - 1` | Truncates a project name to `N` graphemes. |
|
||||
| `truncation_symbol` | `"…"` | The symbol used to indicate a project name was truncated. You can use `""` for no symbol. |
|
||||
| `format` | `"via [$symbol$project]($style) "` | The format for the module. |
|
||||
| `symbol` | `"⬢ "` | The symbol used before displaying the project name. |
|
||||
| `style` | `"blue bold"` | The style for the module. |
|
||||
| `disabled` | `false` | Disables the `meson` module. |
|
||||
|
||||
### Variables
|
||||
|
||||
| Variable | Example | Description |
|
||||
| -------- | ---------- | ------------------------------------ |
|
||||
| project | `starship` | The current Meson project name |
|
||||
| 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
|
||||
|
||||
[meson]
|
||||
disabled = false
|
||||
truncation_symbol = "--"
|
||||
symbol = " "
|
||||
style = "bold dimmed green"
|
||||
```
|
||||
|
||||
## Mercurial Branch
|
||||
|
||||
The `hg_branch` module shows the active branch of the repo in your current directory.
|
||||
|
30
src/configs/meson.rs
Normal file
30
src/configs/meson.rs
Normal file
@ -0,0 +1,30 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
#[cfg_attr(
|
||||
feature = "config-schema",
|
||||
derive(schemars::JsonSchema),
|
||||
schemars(deny_unknown_fields)
|
||||
)]
|
||||
#[serde(default)]
|
||||
pub struct MesonConfig<'a> {
|
||||
pub truncation_length: u32,
|
||||
pub truncation_symbol: &'a str,
|
||||
pub format: &'a str,
|
||||
pub symbol: &'a str,
|
||||
pub style: &'a str,
|
||||
pub disabled: bool,
|
||||
}
|
||||
|
||||
impl<'a> Default for MesonConfig<'a> {
|
||||
fn default() -> Self {
|
||||
MesonConfig {
|
||||
truncation_length: std::u32::MAX,
|
||||
truncation_symbol: "…",
|
||||
format: "via [$symbol$project]($style) ",
|
||||
symbol: "⬢ ",
|
||||
style: "blue bold",
|
||||
disabled: false,
|
||||
}
|
||||
}
|
||||
}
|
@ -46,6 +46,7 @@ pub mod line_break;
|
||||
pub mod localip;
|
||||
pub mod lua;
|
||||
pub mod memory_usage;
|
||||
pub mod meson;
|
||||
pub mod nim;
|
||||
pub mod nix_shell;
|
||||
pub mod nodejs;
|
||||
@ -184,6 +185,8 @@ pub struct FullConfig<'a> {
|
||||
#[serde(borrow)]
|
||||
memory_usage: memory_usage::MemoryConfig<'a>,
|
||||
#[serde(borrow)]
|
||||
meson: meson::MesonConfig<'a>,
|
||||
#[serde(borrow)]
|
||||
nim: nim::NimConfig<'a>,
|
||||
#[serde(borrow)]
|
||||
nix_shell: nix_shell::NixShellConfig<'a>,
|
||||
|
@ -87,6 +87,7 @@ pub const PROMPT_ORDER: &[&str] = &[
|
||||
"buf",
|
||||
"nix_shell",
|
||||
"conda",
|
||||
"meson",
|
||||
"spack",
|
||||
"memory_usage",
|
||||
"aws",
|
||||
|
@ -54,6 +54,7 @@ pub const ALL_MODULES: &[&str] = &[
|
||||
"localip",
|
||||
"lua",
|
||||
"memory_usage",
|
||||
"meson",
|
||||
"nim",
|
||||
"nix_shell",
|
||||
"nodejs",
|
||||
|
98
src/modules/meson.rs
Normal file
98
src/modules/meson.rs
Normal file
@ -0,0 +1,98 @@
|
||||
use super::{Context, Module, ModuleConfig};
|
||||
|
||||
use super::utils::truncate::truncate_text;
|
||||
use crate::configs::meson::MesonConfig;
|
||||
use crate::formatter::StringFormatter;
|
||||
|
||||
/// Creates a module with the current Meson dev environment
|
||||
///
|
||||
/// Will display the Meson environment if `$MESON_DEVENV` and `MESON_PROJECT_NAME` are set.
|
||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
let meson_env = context.get_env("MESON_DEVENV")?;
|
||||
let project_env = context.get_env("MESON_PROJECT_NAME")?;
|
||||
if meson_env != "1" || project_env.trim().is_empty() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let mut module = context.new_module("meson");
|
||||
let config: MesonConfig = MesonConfig::try_load(module.config);
|
||||
|
||||
let truncated_text = truncate_text(
|
||||
&project_env,
|
||||
config.truncation_length as usize,
|
||||
config.truncation_symbol,
|
||||
);
|
||||
|
||||
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 {
|
||||
"project" => Some(Ok(&truncated_text)),
|
||||
_ => None,
|
||||
})
|
||||
.parse(None, Some(context))
|
||||
});
|
||||
|
||||
module.set_segments(match parsed {
|
||||
Ok(segments) => segments,
|
||||
Err(error) => {
|
||||
log::warn!("Error in module `meson`:\n{}", error);
|
||||
return None;
|
||||
}
|
||||
});
|
||||
|
||||
Some(module)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::test::ModuleRenderer;
|
||||
use nu_ansi_term::Color;
|
||||
|
||||
#[test]
|
||||
fn not_in_env() {
|
||||
let actual = ModuleRenderer::new("meson").collect();
|
||||
|
||||
let expected = None;
|
||||
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn env_set() {
|
||||
let actual = ModuleRenderer::new("meson")
|
||||
.env("MESON_DEVENV", "1")
|
||||
.env("MESON_PROJECT_NAME", "starship")
|
||||
.collect();
|
||||
|
||||
let expected = Some(format!("via {} ", Color::Blue.bold().paint("⬢ starship")));
|
||||
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn env_invalid_devenv() {
|
||||
let actual = ModuleRenderer::new("meson")
|
||||
.env("MESON_DEVENV", "0")
|
||||
.env("MESON_PROJECT_NAME", "starship")
|
||||
.collect();
|
||||
let expected = None;
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
#[test]
|
||||
fn env_invalid_project_name() {
|
||||
let actual = ModuleRenderer::new("meson")
|
||||
.env("MESON_DEVENV", "1")
|
||||
.env("MESON_PROJECT_NAME", " ")
|
||||
.collect();
|
||||
let expected = None;
|
||||
assert_eq!(expected, actual);
|
||||
}
|
||||
}
|
@ -43,6 +43,7 @@ mod line_break;
|
||||
mod localip;
|
||||
mod lua;
|
||||
mod memory_usage;
|
||||
mod meson;
|
||||
mod nim;
|
||||
mod nix_shell;
|
||||
mod nodejs;
|
||||
@ -137,6 +138,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
|
||||
"localip" => localip::module(context),
|
||||
"lua" => lua::module(context),
|
||||
"memory_usage" => memory_usage::module(context),
|
||||
"meson" => meson::module(context),
|
||||
"nim" => nim::module(context),
|
||||
"nix_shell" => nix_shell::module(context),
|
||||
"nodejs" => nodejs::module(context),
|
||||
@ -242,6 +244,9 @@ pub fn description(module: &str) -> &'static str {
|
||||
"localip" => "The currently assigned ipv4 address",
|
||||
"lua" => "The currently installed version of Lua",
|
||||
"memory_usage" => "Current system memory and swap usage",
|
||||
"meson" => {
|
||||
"The current Meson environment, if $MESON_DEVENV and $MESON_PROJECT_NAME are set"
|
||||
}
|
||||
"nim" => "The currently installed version of Nim",
|
||||
"nix_shell" => "The nix-shell environment",
|
||||
"nodejs" => "The currently installed version of NodeJS",
|
||||
|
@ -7,3 +7,5 @@ pub mod directory_win;
|
||||
pub mod directory_nix;
|
||||
|
||||
pub mod path;
|
||||
|
||||
pub mod truncate;
|
||||
|
107
src/modules/utils/truncate.rs
Normal file
107
src/modules/utils/truncate.rs
Normal file
@ -0,0 +1,107 @@
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
/// Truncate a string to only have a set number of characters
|
||||
///
|
||||
/// Will truncate a string to only show the last `length` character in the string.
|
||||
/// If a length of `0` is provided, the string will not be truncated and the original
|
||||
/// will be returned.
|
||||
pub fn truncate_text(text: &str, length: usize, truncation_symbol: &str) -> String {
|
||||
if length == 0 {
|
||||
return String::from(text);
|
||||
}
|
||||
|
||||
let truncated_graphemes = get_graphemes(text, length);
|
||||
// The truncation symbol should only be added if we truncated
|
||||
let truncated_and_symbol = if length < graphemes_len(text) {
|
||||
let truncation_symbol = get_graphemes(truncation_symbol, 1);
|
||||
truncated_graphemes + truncation_symbol.as_str()
|
||||
} else {
|
||||
truncated_graphemes
|
||||
};
|
||||
|
||||
truncated_and_symbol
|
||||
}
|
||||
|
||||
fn get_graphemes(text: &str, length: usize) -> String {
|
||||
UnicodeSegmentation::graphemes(text, true)
|
||||
.take(length)
|
||||
.collect::<Vec<&str>>()
|
||||
.concat()
|
||||
}
|
||||
|
||||
fn graphemes_len(text: &str) -> usize {
|
||||
UnicodeSegmentation::graphemes(text, true).count()
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn test_multi_char_truncation_symbol() {
|
||||
let actual = truncate_text("1337_hello_world", 15, "apple");
|
||||
|
||||
assert_eq!("1337_hello_worla", actual);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_changed_truncation_symbol() {
|
||||
test_truncate_length("1337_hello_world", 15, "1337_hello_worl", "%")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_no_truncation_symbol() {
|
||||
test_truncate_length("1337_hello_world", 15, "1337_hello_worl", "")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ascii_boundary_below() {
|
||||
test_truncate_length("1337_hello_world", 15, "1337_hello_worl", "…")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ascii_boundary_on() {
|
||||
test_truncate_length("1337_hello_world", 16, "1337_hello_world", "")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_ascii_boundary_above() {
|
||||
test_truncate_length("1337_hello_world", 17, "1337_hello_world", "")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_one() {
|
||||
test_truncate_length("1337_hello_world", 1, "1", "…")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_negative() {
|
||||
test_truncate_length("1337_hello_world", -1, "1337_hello_world", "")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hindi_truncation() {
|
||||
test_truncate_length("नमस्ते", 3, "नमस्", "…")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hindi_truncation2() {
|
||||
test_truncate_length("नमस्त", 3, "नमस्", "…")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_japanese_truncation() {
|
||||
test_truncate_length("がんばってね", 4, "がんばっ", "…")
|
||||
}
|
||||
|
||||
fn test_truncate_length(
|
||||
text: &str,
|
||||
truncate_length: i64,
|
||||
expected: &str,
|
||||
truncation_symbol: &str,
|
||||
) {
|
||||
let actual = truncate_text(text, truncate_length as usize, truncation_symbol);
|
||||
|
||||
assert_eq!(format!("{}{}", expected, truncation_symbol), actual);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user