From 5303fd7684ab6ce43aaec4ee15c0110ca313e0e9 Mon Sep 17 00:00:00 2001 From: Zach Mertes Date: Mon, 14 Oct 2019 12:22:25 -0400 Subject: [PATCH] feat: Add configuration for hostname truncation (#485) --- docs/config/README.md | 16 +++++++------ src/configs/hostname.rs | 2 ++ src/modules/hostname.rs | 14 ++++++++++++ tests/testsuite/hostname.rs | 45 +++++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 7 deletions(-) diff --git a/docs/config/README.md b/docs/config/README.md index 4e19a31a..81c19cea 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -543,13 +543,14 @@ The `hostname` module shows the system hostname. ### Options -| Variable | Default | Description | -| ---------- | --------------------- | ---------------------------------------------------- | -| `ssh_only` | `true` | Only show hostname when connected to an SSH session. | -| `prefix` | `""` | Prefix to display immediately before the hostname. | -| `suffix` | `""` | Suffix to display immediately after the hostname. | -| `style` | `"bold dimmed green"` | The style for the module. | -| `disabled` | `false` | Disables the `hostname` module. | +| Variable | Default | Description | +| ---------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | +| `ssh_only` | `true` | Only show hostname when connected to an SSH session. | +| `prefix` | `""` | Prefix to display immediately before the hostname. | +| `suffix` | `""` | Suffix to display immediately after the hostname. | +| `trim_at` | `"."` | String that the hostname is cut off at, after the first match. `"."` will stop after the first dot. `""` will disable any truncation | +| `style` | `"bold dimmed green"` | The style for the module. | +| `disabled` | `false` | Disables the `hostname` module. | ### Example @@ -560,6 +561,7 @@ The `hostname` module shows the system hostname. ssh_only = false prefix = "⟪" suffix = "⟫" +trim_at = ".companyname.com" disabled = false ``` diff --git a/src/configs/hostname.rs b/src/configs/hostname.rs index fc2d0c07..c47d5c31 100644 --- a/src/configs/hostname.rs +++ b/src/configs/hostname.rs @@ -8,6 +8,7 @@ pub struct HostnameConfig<'a> { pub ssh_only: bool, pub prefix: &'a str, pub suffix: &'a str, + pub trim_at: &'a str, pub style: Style, pub disabled: bool, } @@ -18,6 +19,7 @@ impl<'a> RootModuleConfig<'a> for HostnameConfig<'a> { ssh_only: true, prefix: "", suffix: "", + trim_at: ".", style: Color::Green.bold().dimmed(), disabled: false, } diff --git a/src/modules/hostname.rs b/src/modules/hostname.rs index acb39379..0c5a0f34 100644 --- a/src/modules/hostname.rs +++ b/src/modules/hostname.rs @@ -30,6 +30,20 @@ pub fn module<'a>(context: &'a Context) -> Option> { } }; + let trim_at = module.config_value_str("trim_at").unwrap_or("."); + + //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 != "" { + if let Some(index) = host.find(config.trim_at) { + host.split_at(index).0 + } else { + host.as_ref() + } + } else { + host.as_ref() + }; + module.set_style(config.style); module.new_segment( "hostname", diff --git a/tests/testsuite/hostname.rs b/tests/testsuite/hostname.rs index 8a1a7bbd..18dced11 100644 --- a/tests/testsuite/hostname.rs +++ b/tests/testsuite/hostname.rs @@ -15,6 +15,7 @@ fn ssh_only_false() -> io::Result<()> { .use_config(toml::toml! { [hostname] ssh_only = false + trim_at = "" }) .output()?; let actual = String::from_utf8(output.stdout).unwrap(); @@ -48,6 +49,7 @@ fn ssh() -> io::Result<()> { .use_config(toml::toml! { [hostname] ssh_only = true + trim_at = "" }) .env("SSH_CONNECTION", "something") .output()?; @@ -68,6 +70,7 @@ fn prefix() -> io::Result<()> { .use_config(toml::toml! { [hostname] ssh_only = false + trim_at = "" prefix = "<" }) .output()?; @@ -88,6 +91,7 @@ fn suffix() -> io::Result<()> { .use_config(toml::toml! { [hostname] ssh_only = false + trim_at = "" suffix = ">" }) .output()?; @@ -97,6 +101,47 @@ fn suffix() -> io::Result<()> { Ok(()) } +#[test] +fn no_trim_at() -> io::Result<()> { + let hostname = match get_hostname() { + Some(h) => h, + None => return hostname_not_tested(), + }; + let output = common::render_module("hostname") + .env_clear() + .use_config(toml::toml! { + [hostname] + ssh_only = false + trim_at = "" + }) + .output()?; + let actual = String::from_utf8(output.stdout).unwrap(); + let expected = format!("on {} ", style().paint(hostname)); + assert_eq!(expected, actual); + Ok(()) +} + +#[test] +fn trim_at() -> io::Result<()> { + let hostname = match get_hostname() { + Some(h) => h, + None => return hostname_not_tested(), + }; + let (remainder, trim_at) = hostname.split_at(1); + let output = common::render_module("hostname") + .env_clear() + .use_config(toml::toml! { + [hostname] + ssh_only = false + trim_at = trim_at + }) + .output()?; + let actual = String::from_utf8(output.stdout).unwrap(); + let expected = format!("on {} ", style().paint(remainder)); + assert_eq!(expected, actual); + Ok(()) +} + fn get_hostname() -> Option { match gethostname::gethostname().into_string() { Ok(hostname) => Some(hostname),