mirror of
https://github.com/Llewellynvdm/starship.git
synced 2025-01-13 02:11:35 +00:00
feat: Add configuration for hostname truncation (#485)
This commit is contained in:
parent
4634449354
commit
5303fd7684
@ -543,13 +543,14 @@ The `hostname` module shows the system hostname.
|
|||||||
|
|
||||||
### Options
|
### Options
|
||||||
|
|
||||||
| Variable | Default | Description |
|
| Variable | 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. |
|
||||||
| `prefix` | `""` | Prefix to display immediately before the hostname. |
|
| `prefix` | `""` | Prefix to display immediately before the hostname. |
|
||||||
| `suffix` | `""` | Suffix to display immediately after the hostname. |
|
| `suffix` | `""` | Suffix to display immediately after the hostname. |
|
||||||
| `style` | `"bold dimmed green"` | The style 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 |
|
||||||
| `disabled` | `false` | Disables the `hostname` module. |
|
| `style` | `"bold dimmed green"` | The style for the module. |
|
||||||
|
| `disabled` | `false` | Disables the `hostname` module. |
|
||||||
|
|
||||||
### Example
|
### Example
|
||||||
|
|
||||||
@ -560,6 +561,7 @@ The `hostname` module shows the system hostname.
|
|||||||
ssh_only = false
|
ssh_only = false
|
||||||
prefix = "⟪"
|
prefix = "⟪"
|
||||||
suffix = "⟫"
|
suffix = "⟫"
|
||||||
|
trim_at = ".companyname.com"
|
||||||
disabled = false
|
disabled = false
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -8,6 +8,7 @@ pub struct HostnameConfig<'a> {
|
|||||||
pub ssh_only: bool,
|
pub ssh_only: bool,
|
||||||
pub prefix: &'a str,
|
pub prefix: &'a str,
|
||||||
pub suffix: &'a str,
|
pub suffix: &'a str,
|
||||||
|
pub trim_at: &'a str,
|
||||||
pub style: Style,
|
pub style: Style,
|
||||||
pub disabled: bool,
|
pub disabled: bool,
|
||||||
}
|
}
|
||||||
@ -18,6 +19,7 @@ impl<'a> RootModuleConfig<'a> for HostnameConfig<'a> {
|
|||||||
ssh_only: true,
|
ssh_only: true,
|
||||||
prefix: "",
|
prefix: "",
|
||||||
suffix: "",
|
suffix: "",
|
||||||
|
trim_at: ".",
|
||||||
style: Color::Green.bold().dimmed(),
|
style: Color::Green.bold().dimmed(),
|
||||||
disabled: false,
|
disabled: false,
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,20 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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.set_style(config.style);
|
||||||
module.new_segment(
|
module.new_segment(
|
||||||
"hostname",
|
"hostname",
|
||||||
|
@ -15,6 +15,7 @@ fn ssh_only_false() -> io::Result<()> {
|
|||||||
.use_config(toml::toml! {
|
.use_config(toml::toml! {
|
||||||
[hostname]
|
[hostname]
|
||||||
ssh_only = false
|
ssh_only = false
|
||||||
|
trim_at = ""
|
||||||
})
|
})
|
||||||
.output()?;
|
.output()?;
|
||||||
let actual = String::from_utf8(output.stdout).unwrap();
|
let actual = String::from_utf8(output.stdout).unwrap();
|
||||||
@ -48,6 +49,7 @@ fn ssh() -> io::Result<()> {
|
|||||||
.use_config(toml::toml! {
|
.use_config(toml::toml! {
|
||||||
[hostname]
|
[hostname]
|
||||||
ssh_only = true
|
ssh_only = true
|
||||||
|
trim_at = ""
|
||||||
})
|
})
|
||||||
.env("SSH_CONNECTION", "something")
|
.env("SSH_CONNECTION", "something")
|
||||||
.output()?;
|
.output()?;
|
||||||
@ -68,6 +70,7 @@ fn prefix() -> io::Result<()> {
|
|||||||
.use_config(toml::toml! {
|
.use_config(toml::toml! {
|
||||||
[hostname]
|
[hostname]
|
||||||
ssh_only = false
|
ssh_only = false
|
||||||
|
trim_at = ""
|
||||||
prefix = "<"
|
prefix = "<"
|
||||||
})
|
})
|
||||||
.output()?;
|
.output()?;
|
||||||
@ -88,6 +91,7 @@ fn suffix() -> io::Result<()> {
|
|||||||
.use_config(toml::toml! {
|
.use_config(toml::toml! {
|
||||||
[hostname]
|
[hostname]
|
||||||
ssh_only = false
|
ssh_only = false
|
||||||
|
trim_at = ""
|
||||||
suffix = ">"
|
suffix = ">"
|
||||||
})
|
})
|
||||||
.output()?;
|
.output()?;
|
||||||
@ -97,6 +101,47 @@ fn suffix() -> io::Result<()> {
|
|||||||
Ok(())
|
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<String> {
|
fn get_hostname() -> Option<String> {
|
||||||
match gethostname::gethostname().into_string() {
|
match gethostname::gethostname().into_string() {
|
||||||
Ok(hostname) => Some(hostname),
|
Ok(hostname) => Some(hostname),
|
||||||
|
Loading…
Reference in New Issue
Block a user