Skip to content

Commit

Permalink
feat(username): add aliases option (#5855)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alper committed Apr 5, 2024
1 parent 335c514 commit 9c1eadd
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .github/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1784,6 +1784,7 @@
},
"username": {
"default": {
"aliases": {},
"detect_env_vars": [],
"disabled": false,
"format": "[$user]($style) in ",
Expand Down Expand Up @@ -6129,6 +6130,13 @@
"disabled": {
"default": false,
"type": "boolean"
},
"aliases": {
"default": {},
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"additionalProperties": false
Expand Down
2 changes: 2 additions & 0 deletions docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/configs/username.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

#[derive(Clone, Deserialize, Serialize)]
Expand All @@ -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> {
Expand All @@ -25,6 +27,7 @@ impl<'a> Default for UsernameConfig<'a> {
style_user: "yellow bold",
show_always: false,
disabled: false,
aliases: IndexMap::new(),
}
}
}
40 changes: 40 additions & 0 deletions src/modules/username.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 9c1eadd

Please sign in to comment.