From 2bf30dc89fbce6f4da37657b8af6077f15a543d0 Mon Sep 17 00:00:00 2001 From: Thanapat Chotipun <66824385+PatrickChoDev@users.noreply.github.com> Date: Wed, 4 May 2022 20:30:16 +0700 Subject: [PATCH] feat(hostname): add `ssh_symbol` for ssh connections (#3806) --- .github/config-schema.json | 9 +++++++-- docs/config/README.md | 26 ++++++++++++++------------ src/configs/hostname.rs | 4 +++- src/modules/hostname.rs | 31 +++++++++++++++++++++++++++++-- 4 files changed, 53 insertions(+), 17 deletions(-) diff --git a/.github/config-schema.json b/.github/config-schema.json index 8884f817..f6cf1dbd 100644 --- a/.github/config-schema.json +++ b/.github/config-schema.json @@ -638,8 +638,9 @@ "hostname": { "default": { "disabled": false, - "format": "[$hostname]($style) in ", + "format": "[$ssh_symbol$hostname]($style) in ", "ssh_only": true, + "ssh_symbol": "🌐 ", "style": "green dimmed bold", "trim_at": "." }, @@ -2956,12 +2957,16 @@ "default": true, "type": "boolean" }, + "ssh_symbol": { + "default": "🌐 ", + "type": "string" + }, "trim_at": { "default": ".", "type": "string" }, "format": { - "default": "[$hostname]($style) in ", + "default": "[$ssh_symbol$hostname]($style) in ", "type": "string" }, "style": { diff --git a/docs/config/README.md b/docs/config/README.md index 1cc73c80..2e011535 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -1805,20 +1805,22 @@ The `hostname` module shows the system hostname. ### Options -| Option | Default | Description | -| ---------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | -| `ssh_only` | `true` | Only show hostname when connected to an SSH session. | -| `trim_at` | `"."` | String that the hostname is cut off at, after the first match. `"."` will stop after the first dot. `""` will disable any truncation | -| `format` | `"[$hostname]($style) in "` | The format for the module. | -| `style` | `"bold dimmed green"` | The style for the module. | -| `disabled` | `false` | Disables the `hostname` module. | +| Option | Default | Description | +| ------------ | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | +| `ssh_only` | `true` | Only show hostname when connected to an SSH session. | +| `ssh_symbol` | `"🌐 "` | A format string representing the symbol when connected to SSH session. | +| `trim_at` | `"."` | String that the hostname is cut off at, after the first match. `"."` will stop after the first dot. `""` will disable any truncation | +| `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. | ### Variables -| Variable | Example | Description | -| -------- | ---------- | ----------------------------------- | -| hostname | `computer` | The hostname of the computer | -| style\* | | Mirrors the value of option `style` | +| Variable | Example | Description | +| ---------- | ---------- | ----------------------------------------------------- | +| hostname | `computer` | The hostname of the computer | +| style\* | | Mirrors the value of option `style` | +| ssh_symbol | `"🌏 "` | The symbol to represent when connected to SSH session | *: This variable can only be used as a part of a style string @@ -1829,7 +1831,7 @@ The `hostname` module shows the system hostname. [hostname] ssh_only = false -format = "on [$hostname](bold red) " +format = "[$ssh_symbol](bold blue) on [$hostname](bold red) " trim_at = ".companyname.com" disabled = false ``` diff --git a/src/configs/hostname.rs b/src/configs/hostname.rs index 2a49313a..b877ddfb 100644 --- a/src/configs/hostname.rs +++ b/src/configs/hostname.rs @@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize}; #[serde(default)] pub struct HostnameConfig<'a> { pub ssh_only: bool, + pub ssh_symbol: &'a str, pub trim_at: &'a str, pub format: &'a str, pub style: &'a str, @@ -15,8 +16,9 @@ impl<'a> Default for HostnameConfig<'a> { fn default() -> Self { HostnameConfig { ssh_only: true, + ssh_symbol: "🌐 ", trim_at: ".", - format: "[$hostname]($style) in ", + format: "[$ssh_symbol$hostname]($style) in ", style: "green dimmed bold", disabled: false, } diff --git a/src/modules/hostname.rs b/src/modules/hostname.rs index e02cf3d3..d1a2d97f 100644 --- a/src/modules/hostname.rs +++ b/src/modules/hostname.rs @@ -43,6 +43,16 @@ pub fn module<'a>(context: &'a Context) -> Option> { let parsed = StringFormatter::new(config.format).and_then(|formatter| { formatter + .map_meta(|var, _| match var { + "ssh_symbol" => { + if ssh_connection.is_some() { + Some(config.ssh_symbol) + } else { + None + } + } + _ => None, + }) .map_style(|variable| match variable { "style" => Some(Ok(config.style)), _ => None, @@ -86,7 +96,7 @@ mod tests { } #[test] - fn ssh_only_false() { + fn ssh_only_false_no_ssh() { let hostname = get_hostname!(); let actual = ModuleRenderer::new("hostname") .config(toml::toml! { @@ -96,7 +106,21 @@ mod tests { }) .collect(); let expected = Some(format!("{} in ", style().paint(hostname))); + println!("{}", expected.as_ref().unwrap()); + assert_eq!(expected, actual); + } + #[test] + fn ssh_only_false_ssh() { + let hostname = get_hostname!(); + let actual = ModuleRenderer::new("hostname") + .config(toml::toml! { + [hostname] + ssh_only = false + trim_at = "" + }) + .collect(); + let expected = Some(format!("{} in ", style().paint(hostname))); assert_eq!(expected, actual); } @@ -124,7 +148,10 @@ mod tests { }) .env("SSH_CONNECTION", "something") .collect(); - let expected = Some(format!("{} in ", style().paint(hostname))); + let expected = Some(format!( + "{} in ", + style().paint("🌐 ".to_owned() + &hostname) + )); assert_eq!(expected, actual); }