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": {
"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": {

View File

@ -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
```

View File

@ -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,
}

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| {
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);
}