mirror of
https://github.com/Llewellynvdm/starship.git
synced 2025-01-12 18:01:23 +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
|
||||
|
||||
| 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
|
||||
```
|
||||
|
||||
|
@ -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,
|
||||
}
|
||||
|
@ -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.new_segment(
|
||||
"hostname",
|
||||
|
@ -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<String> {
|
||||
match gethostname::gethostname().into_string() {
|
||||
Ok(hostname) => Some(hostname),
|
||||
|
Loading…
Reference in New Issue
Block a user