feat(hostname): add option to replace hostnames with aliases (#6097)

---------

Co-authored-by: David Knaack <davidkna@users.noreply.github.com>
This commit is contained in:
Eduard Korchmar 2024-08-12 18:26:35 +02:00 committed by GitHub
parent 7b65ad5163
commit 68a8fc9563
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 81 additions and 2 deletions

View File

@ -842,6 +842,7 @@
}, },
"hostname": { "hostname": {
"default": { "default": {
"aliases": {},
"detect_env_vars": [], "detect_env_vars": [],
"disabled": false, "disabled": false,
"format": "[$ssh_symbol$hostname]($style) in ", "format": "[$ssh_symbol$hostname]($style) in ",
@ -4008,6 +4009,13 @@
"disabled": { "disabled": {
"default": false, "default": false,
"type": "boolean" "type": "boolean"
},
"aliases": {
"default": {},
"type": "object",
"additionalProperties": {
"type": "string"
}
} }
}, },
"additionalProperties": false "additionalProperties": false

View File

@ -2355,6 +2355,7 @@ The `hostname` module shows the system hostname.
| `format` | `'[$ssh_symbol$hostname]($style) in '` | The format for the module. | | `format` | `'[$ssh_symbol$hostname]($style) in '` | The format for the module. |
| `style` | `'bold dimmed green'` | The style for the module. | | `style` | `'bold dimmed green'` | The style for the module. |
| `disabled` | `false` | Disables the `hostname` module. | | `disabled` | `false` | Disables the `hostname` module. |
| `aliases` | `{}` | Translate system hostnames to something else. If `trim_at` is specified, only the first part will be matched and replaced. |
### Variables ### Variables
@ -2391,6 +2392,14 @@ detect_env_vars = ['!TMUX', 'SSH_CONNECTION']
disabled = false disabled = false
``` ```
#### Replace the hostname with a nickname
```toml
# ~/.config/starship.toml
[hostname]
aliases = { "Max's MacBook Pro" = "home" }
```
## Java ## Java
The `java` module shows the currently installed version of [Java](https://www.oracle.com/java/). The `java` module shows the currently installed version of [Java](https://www.oracle.com/java/).
@ -4539,7 +4548,7 @@ these variables, one workaround is to set one of them with a dummy value.
| `format` | `'[$user]($style) in '` | The format for the module. | | `format` | `'[$user]($style) in '` | The format for the module. |
| `show_always` | `false` | Always shows the `username` module. | | `show_always` | `false` | Always shows the `username` module. |
| `disabled` | `false` | Disables the `username` module. | | `disabled` | `false` | Disables the `username` module. |
| `aliases` | `{}` | Translate system usernames to something else | | `aliases` | `{}` | Translate system usernames to something else. |
### Variables ### Variables

View File

@ -1,3 +1,4 @@
use indexmap::IndexMap;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
#[derive(Clone, Deserialize, Serialize)] #[derive(Clone, Deserialize, Serialize)]
@ -15,6 +16,7 @@ pub struct HostnameConfig<'a> {
pub format: &'a str, pub format: &'a str,
pub style: &'a str, pub style: &'a str,
pub disabled: bool, pub disabled: bool,
pub aliases: IndexMap<String, &'a str>,
} }
impl<'a> Default for HostnameConfig<'a> { impl<'a> Default for HostnameConfig<'a> {
@ -27,6 +29,7 @@ impl<'a> Default for HostnameConfig<'a> {
format: "[$ssh_symbol$hostname]($style) in ", format: "[$ssh_symbol$hostname]($style) in ",
style: "green dimmed bold", style: "green dimmed bold",
disabled: false, disabled: false,
aliases: IndexMap::new(),
} }
} }
} }

View File

@ -35,7 +35,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
//rustc doesn't let you do an "if" and an "if let" in the same if statement //rustc doesn't let you do an "if" and an "if let" in the same if statement
// if this changes in the future this can become a lot cleaner // if this changes in the future this can become a lot cleaner
let host = if !config.trim_at.is_empty() { let mut host = if !config.trim_at.is_empty() {
if let Some(index) = host.find(config.trim_at) { if let Some(index) = host.find(config.trim_at) {
host.split_at(index).0 host.split_at(index).0
} else { } else {
@ -45,6 +45,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
host.as_ref() host.as_ref()
}; };
if let Some(&alias) = config.aliases.get(host) {
host = alias;
}
let parsed = StringFormatter::new(config.format).and_then(|formatter| { let parsed = StringFormatter::new(config.format).and_then(|formatter| {
formatter formatter
.map_meta(|var, _| match var { .map_meta(|var, _| match var {
@ -259,6 +263,61 @@ mod tests {
assert_eq!(expected, actual); assert_eq!(expected, actual);
} }
#[test]
fn test_alias() {
let hostname = get_hostname!();
let mut toml_config = toml::toml!(
[hostname]
ssh_only = false
trim_at = ""
aliases = {}
);
toml_config["hostname"]["aliases"]
.as_table_mut()
.unwrap()
.insert(
hostname.clone(),
toml::Value::String("homeworld".to_string()),
);
let actual = ModuleRenderer::new("hostname")
.config(toml_config)
.collect();
let expected = Some(format!("{} in ", style().paint("homeworld")));
assert_eq!(expected, actual);
}
#[test]
fn test_alias_with_trim_at() {
let hostname = get_hostname!();
let mut hostname_iter = hostname.graphemes(true);
let remainder = hostname_iter.next().unwrap_or_default();
let trim_at = hostname_iter.collect::<String>();
// Trimmed hostname needs to be non-empty
if remainder.is_empty() {
log::warn!("Skipping test_alias_with_trim_at because hostname is too short");
return;
}
let mut toml_config = toml::toml!(
[hostname]
ssh_only = false
trim_at = trim_at
aliases = {}
);
toml_config["hostname"]["aliases"]
.as_table_mut()
.unwrap()
.insert(remainder.to_string(), toml::Value::String("🌍".to_string()));
let actual = ModuleRenderer::new("hostname")
.config(toml_config)
.collect();
let expected = Some(format!("{} in ", style().paint("🌍")));
assert_eq!(expected, actual);
}
fn style() -> Style { fn style() -> Style {
Color::Green.bold().dimmed() Color::Green.bold().dimmed()
} }