feat(hostname): add ssh_symbol for ssh connections (#3806)

This commit is contained in:
Thanapat Chotipun 2022-05-04 20:30:16 +07:00 committed by GitHub
parent 3ced500c87
commit 2bf30dc89f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 17 deletions

View File

@ -638,8 +638,9 @@
"hostname": { "hostname": {
"default": { "default": {
"disabled": false, "disabled": false,
"format": "[$hostname]($style) in ", "format": "[$ssh_symbol$hostname]($style) in ",
"ssh_only": true, "ssh_only": true,
"ssh_symbol": "🌐 ",
"style": "green dimmed bold", "style": "green dimmed bold",
"trim_at": "." "trim_at": "."
}, },
@ -2956,12 +2957,16 @@
"default": true, "default": true,
"type": "boolean" "type": "boolean"
}, },
"ssh_symbol": {
"default": "🌐 ",
"type": "string"
},
"trim_at": { "trim_at": {
"default": ".", "default": ".",
"type": "string" "type": "string"
}, },
"format": { "format": {
"default": "[$hostname]($style) in ", "default": "[$ssh_symbol$hostname]($style) in ",
"type": "string" "type": "string"
}, },
"style": { "style": {

View File

@ -1805,20 +1805,22 @@ The `hostname` module shows the system hostname.
### Options ### Options
| Option | Default | Description | | Option | Default | Description |
| ---------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | | ------------ | -------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------ |
| `ssh_only` | `true` | Only show hostname when connected to an SSH session. | | `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 | | `ssh_symbol` | `"🌐 "` | A format string representing the symbol when connected to SSH session. |
| `format` | `"[$hostname]($style) in "` | The format for the module. | | `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. | | `format` | `"[$ssh_symbol$hostname]($style) in "` | The format for the module. |
| `disabled` | `false` | Disables the `hostname` module. | | `style` | `"bold dimmed green"` | The style for the module. |
| `disabled` | `false` | Disables the `hostname` module. |
### Variables ### Variables
| Variable | Example | Description | | Variable | Example | Description |
| -------- | ---------- | ----------------------------------- | | ---------- | ---------- | ----------------------------------------------------- |
| hostname | `computer` | The hostname of the computer | | hostname | `computer` | The hostname of the computer |
| style\* | | Mirrors the value of option `style` | | 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 *: 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] [hostname]
ssh_only = false ssh_only = false
format = "on [$hostname](bold red) " format = "[$ssh_symbol](bold blue) on [$hostname](bold red) "
trim_at = ".companyname.com" trim_at = ".companyname.com"
disabled = false disabled = false
``` ```

View File

@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
#[serde(default)] #[serde(default)]
pub struct HostnameConfig<'a> { pub struct HostnameConfig<'a> {
pub ssh_only: bool, pub ssh_only: bool,
pub ssh_symbol: &'a str,
pub trim_at: &'a str, pub trim_at: &'a str,
pub format: &'a str, pub format: &'a str,
pub style: &'a str, pub style: &'a str,
@ -15,8 +16,9 @@ impl<'a> Default for HostnameConfig<'a> {
fn default() -> Self { fn default() -> Self {
HostnameConfig { HostnameConfig {
ssh_only: true, ssh_only: true,
ssh_symbol: "🌐 ",
trim_at: ".", trim_at: ".",
format: "[$hostname]($style) in ", format: "[$ssh_symbol$hostname]($style) in ",
style: "green dimmed bold", style: "green dimmed bold",
disabled: false, disabled: false,
} }

View File

@ -43,6 +43,16 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
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 {
"ssh_symbol" => {
if ssh_connection.is_some() {
Some(config.ssh_symbol)
} else {
None
}
}
_ => None,
})
.map_style(|variable| match variable { .map_style(|variable| match variable {
"style" => Some(Ok(config.style)), "style" => Some(Ok(config.style)),
_ => None, _ => None,
@ -86,7 +96,7 @@ mod tests {
} }
#[test] #[test]
fn ssh_only_false() { fn ssh_only_false_no_ssh() {
let hostname = get_hostname!(); let hostname = get_hostname!();
let actual = ModuleRenderer::new("hostname") let actual = ModuleRenderer::new("hostname")
.config(toml::toml! { .config(toml::toml! {
@ -96,7 +106,21 @@ mod tests {
}) })
.collect(); .collect();
let expected = Some(format!("{} in ", style().paint(hostname))); 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); assert_eq!(expected, actual);
} }
@ -124,7 +148,10 @@ mod tests {
}) })
.env("SSH_CONNECTION", "something") .env("SSH_CONNECTION", "something")
.collect(); .collect();
let expected = Some(format!("{} in ", style().paint(hostname))); let expected = Some(format!(
"{} in ",
style().paint("🌐 ".to_owned() + &hostname)
));
assert_eq!(expected, actual); assert_eq!(expected, actual);
} }