Skip to content

Commit

Permalink
feat(hostname): add option to replace hostnames with aliases (starshi…
Browse files Browse the repository at this point in the history
…p#6097)


---------

Co-authored-by: David Knaack <[email protected]>
  • Loading branch information
ekorchmar and davidkna authored Aug 12, 2024
1 parent 7b65ad5 commit 68a8fc9
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .github/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@
},
"hostname": {
"default": {
"aliases": {},
"detect_env_vars": [],
"disabled": false,
"format": "[$ssh_symbol$hostname]($style) in ",
Expand Down Expand Up @@ -4008,6 +4009,13 @@
"disabled": {
"default": false,
"type": "boolean"
},
"aliases": {
"default": {},
"type": "object",
"additionalProperties": {
"type": "string"
}
}
},
"additionalProperties": false
Expand Down
11 changes: 10 additions & 1 deletion docs/config/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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/).
Expand Down Expand Up @@ -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

Expand Down
3 changes: 3 additions & 0 deletions src/configs/hostname.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 @@ -15,6 +16,7 @@ pub struct HostnameConfig<'a> {
pub format: &'a str,
pub style: &'a str,
pub disabled: bool,
pub aliases: IndexMap<String, &'a str>,
}

impl<'a> Default for HostnameConfig<'a> {
Expand All @@ -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(),
}
}
}
61 changes: 60 additions & 1 deletion src/modules/hostname.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
// 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 {
Expand All @@ -45,6 +45,10 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
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 {
Expand Down Expand Up @@ -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::<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 {
Color::Green.bold().dimmed()
}
Expand Down

0 comments on commit 68a8fc9

Please sign in to comment.