summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAlper Cugun <github@alper.nl>2024-04-05 23:57:13 +0200
committerGitHub <noreply@github.com>2024-04-05 23:57:13 +0200
commit9c1eaddae154daed9e1d00faac414586f7920013 (patch)
treea5b727ad7b008dcc5a2a1cf8b47bca2d358a7bea
parent335c514e9ee5fa5bc8a8294b4804ddf198504b97 (diff)
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
-rw-r--r--.github/config-schema.json8
-rw-r--r--docs/config/README.md2
-rw-r--r--src/configs/username.rs3
-rw-r--r--src/modules/username.rs40
4 files changed, 53 insertions, 0 deletions
diff --git a/.github/config-schema.json b/.github/config-schema.json
index bb5fdd858..9f30f307b 100644
--- a/.github/config-schema.json
+++ b/.github/config-schema.json
@@ -1784,6 +1784,7 @@
},
"username": {
"default": {
+ "aliases": {},
"detect_env_vars": [],
"disabled": false,
"format": "[$user]($style) in ",
@@ -6129,6 +6130,13 @@
"disabled": {
"default": false,
"type": "boolean"
+ },
+ "aliases": {
+ "default": {},
+ "type": "object",
+ "additionalProperties": {
+ "type": "string"
+ }
}
},
"additionalProperties": false
diff --git a/docs/config/README.md b/docs/config/README.md
index 794190b71..dcec83815 100644
--- a/docs/config/README.md
+++ b/docs/config/README.md
@@ -4505,6 +4505,7 @@ these variables, one workaround is to set one of them with a dummy value.
| `format` | `'[$user]($style) in '` | The format for the module. |
| `show_always` | `false` | Always shows the `username` module. |
| `disabled` | `false` | Disables the `username` module. |
+| `aliases` | `{}` | Translate system usernames to something else |
### Variables
@@ -4526,6 +4527,7 @@ style_root = 'black bold'
format = 'user: [$user]($style) '
disabled = false
show_always = true
+aliases = { "corpuser034g" = "matchai" }
```
#### Hide the hostname in remote tmux sessions
diff --git a/src/configs/username.rs b/src/configs/username.rs
index 807dad51b..1a333c1eb 100644
--- a/src/configs/username.rs
+++ b/src/configs/username.rs
@@ -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(),
}
}
}
diff --git a/src/modules/username.rs b/src/modules/username.rs
index 038bbd757..714eec07e 100644
--- a/src/modules/username.rs
+++ b/src/modules/username.rs
@@ -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());
+ }
}