mirror of
https://github.com/starship/starship.git
synced 2026-06-22 02:02:12 +07:00
feat(username): add aliases option (#5855)
* Create place to put it in the config * Initial functional version * Fix grammar * Add option documentation to README * Add test for two aliases and emoji translation * Remove println * Rewrite match as iflet * Improve converting the reference * Format file * Try to restore autoformat of markdown * Replace toml:Map with concrete IndexMap * Update schema * Add option that got lost
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use indexmap::IndexMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Clone, Deserialize, Serialize)]
|
||||
@@ -14,6 +15,7 @@ pub struct UsernameConfig<'a> {
|
||||
pub style_user: &'a str,
|
||||
pub show_always: bool,
|
||||
pub disabled: bool,
|
||||
pub aliases: IndexMap<String, &'a str>,
|
||||
}
|
||||
|
||||
impl<'a> Default for UsernameConfig<'a> {
|
||||
@@ -25,6 +27,7 @@ impl<'a> Default for UsernameConfig<'a> {
|
||||
style_user: "yellow bold",
|
||||
show_always: false,
|
||||
disabled: false,
|
||||
aliases: IndexMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
return None; // [A]
|
||||
}
|
||||
|
||||
if let Some(&alias) = config.aliases.get(&username) {
|
||||
username = alias.to_string();
|
||||
}
|
||||
|
||||
let parsed = StringFormatter::new(config.format).and_then(|formatter| {
|
||||
formatter
|
||||
.map_style(|variable| match variable {
|
||||
@@ -323,4 +327,40 @@ mod tests {
|
||||
|
||||
assert_eq!(expected, actual.as_deref());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_alias() {
|
||||
let actual = ModuleRenderer::new("username")
|
||||
.env(super::USERNAME_ENV_VAR, "astronaut")
|
||||
.config(toml::toml! {
|
||||
[username]
|
||||
show_always = true
|
||||
aliases = { "astronaut" = "skywalker" }
|
||||
|
||||
style_root = ""
|
||||
style_user = ""
|
||||
})
|
||||
.collect();
|
||||
let expected = Some("skywalker in ");
|
||||
|
||||
assert_eq!(expected, actual.as_deref());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_alias_emoji() {
|
||||
let actual = ModuleRenderer::new("username")
|
||||
.env(super::USERNAME_ENV_VAR, "kaas")
|
||||
.config(toml::toml! {
|
||||
[username]
|
||||
show_always = true
|
||||
aliases = { "a" = "b", "kaas" = "🧀" }
|
||||
|
||||
style_root = ""
|
||||
style_user = ""
|
||||
})
|
||||
.collect();
|
||||
let expected = Some("🧀 in ");
|
||||
|
||||
assert_eq!(expected, actual.as_deref());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user