From 68a8fc9563a52c4119241375d181ff68bb3d3da3 Mon Sep 17 00:00:00 2001 From: Eduard Korchmar Date: Mon, 12 Aug 2024 18:26:35 +0200 Subject: [PATCH] feat(hostname): add option to replace hostnames with aliases (#6097) --------- Co-authored-by: David Knaack --- .github/config-schema.json | 8 +++++ docs/config/README.md | 11 ++++++- src/configs/hostname.rs | 3 ++ src/modules/hostname.rs | 61 +++++++++++++++++++++++++++++++++++++- 4 files changed, 81 insertions(+), 2 deletions(-) diff --git a/.github/config-schema.json b/.github/config-schema.json index 5109d7ba..e3cb4b3a 100644 --- a/.github/config-schema.json +++ b/.github/config-schema.json @@ -842,6 +842,7 @@ }, "hostname": { "default": { + "aliases": {}, "detect_env_vars": [], "disabled": false, "format": "[$ssh_symbol$hostname]($style) in ", @@ -4008,6 +4009,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 e48e7381..b13c2af2 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -2355,6 +2355,7 @@ The `hostname` module shows the system hostname. | `format` | `'[$ssh_symbol$hostname]($style) in '` | The format for the module. | | `style` | `'bold dimmed green'` | The style for the 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 @@ -2391,6 +2392,14 @@ detect_env_vars = ['!TMUX', 'SSH_CONNECTION'] disabled = false ``` +#### Replace the hostname with a nickname + +```toml +# ~/.config/starship.toml +[hostname] +aliases = { "Max's MacBook Pro" = "home" } +``` + ## 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. | | `show_always` | `false` | Always shows the `username` module. | | `disabled` | `false` | Disables the `username` module. | -| `aliases` | `{}` | Translate system usernames to something else | +| `aliases` | `{}` | Translate system usernames to something else. | ### Variables diff --git a/src/configs/hostname.rs b/src/configs/hostname.rs index 879afa3c..939e5112 100644 --- a/src/configs/hostname.rs +++ b/src/configs/hostname.rs @@ -1,3 +1,4 @@ +use indexmap::IndexMap; use serde::{Deserialize, Serialize}; #[derive(Clone, Deserialize, Serialize)] @@ -15,6 +16,7 @@ pub struct HostnameConfig<'a> { pub format: &'a str, pub style: &'a str, pub disabled: bool, + pub aliases: IndexMap, } impl<'a> Default for HostnameConfig<'a> { @@ -27,6 +29,7 @@ impl<'a> Default for HostnameConfig<'a> { format: "[$ssh_symbol$hostname]($style) in ", style: "green dimmed bold", disabled: false, + aliases: IndexMap::new(), } } } diff --git a/src/modules/hostname.rs b/src/modules/hostname.rs index 8eec9b5f..c89edb41 100644 --- a/src/modules/hostname.rs +++ b/src/modules/hostname.rs @@ -35,7 +35,7 @@ pub fn module<'a>(context: &'a Context) -> Option> { //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 - 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) { host.split_at(index).0 } else { @@ -45,6 +45,10 @@ pub fn module<'a>(context: &'a Context) -> Option> { host.as_ref() }; + if let Some(&alias) = config.aliases.get(host) { + host = alias; + } + let parsed = StringFormatter::new(config.format).and_then(|formatter| { formatter .map_meta(|var, _| match var { @@ -259,6 +263,61 @@ mod tests { 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::(); + + // 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 { Color::Green.bold().dimmed() }