mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-12-25 11:20:01 +00:00
feat: add hostname module (#286)
Add a hostname module as requested by @chipbuster. Displays the system hostname as provided by gethostname.
This commit is contained in:
parent
5a0f269d85
commit
84688e4981
11
Cargo.lock
generated
11
Cargo.lock
generated
@ -279,6 +279,15 @@ name = "fuchsia-cprng"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
|
||||
[[package]]
|
||||
name = "gethostname"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
dependencies = [
|
||||
"libc 0.2.58 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.1.6"
|
||||
@ -745,6 +754,7 @@ dependencies = [
|
||||
"battery 0.7.4 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"gethostname 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"git2 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
@ -998,6 +1008,7 @@ dependencies = [
|
||||
"checksum failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "795bd83d3abeb9220f257e597aa0080a508b27533824adf336529648f6abf7e2"
|
||||
"checksum failure_derive 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ea1063915fd7ef4309e222a5a07cf9c319fb9c7836b1f89b85458672dbb127e1"
|
||||
"checksum fuchsia-cprng 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
|
||||
"checksum gethostname 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d4ab273ca2a31eb6ca40b15837ccf1aa59a43c5db69ac10c542be342fae2e01d"
|
||||
"checksum getrandom 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "e65cce4e5084b14874c4e7097f38cab54f47ee554f9194673456ea379dcc4c55"
|
||||
"checksum git2 0.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "327d698f86a7ebdfeb86a4238ccdb004828939d3a3555b6ead679541d14e36c0"
|
||||
"checksum humantime 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3ca7e5f2e110db35f93b837c81797f3714500b81d517bf20c431b16d3ca4f114"
|
||||
|
@ -40,6 +40,7 @@ battery = { version = "0.7.4", optional = true }
|
||||
lazy_static = "1.4.0"
|
||||
path-slash = "0.1.1"
|
||||
unicode-segmentation = "1.3.0"
|
||||
gethostname = "0.2.0"
|
||||
|
||||
[dev-dependencies]
|
||||
tempfile = "3.1.0"
|
||||
|
@ -69,6 +69,7 @@ The `default_prompt_order` configuration option is used to define the order in w
|
||||
```
|
||||
default_prompt_order = [
|
||||
"username",
|
||||
"hostname",
|
||||
"directory",
|
||||
"git_branch",
|
||||
"git_status",
|
||||
@ -268,6 +269,31 @@ renamed = "👅"
|
||||
deleted = "🗑"
|
||||
```
|
||||
|
||||
## Hostname
|
||||
|
||||
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. |
|
||||
| `disabled` | `false` | Disables the `hostname` module. |
|
||||
|
||||
### Example
|
||||
|
||||
```toml
|
||||
# ~/.config/starship.toml
|
||||
|
||||
[hostname]
|
||||
ssh_only = false
|
||||
prefix = "⟪"
|
||||
suffix = "⟫"
|
||||
disabled = false
|
||||
```
|
||||
|
||||
## Golang
|
||||
|
||||
The `golang` module shows the currently installed version of Golang.
|
||||
|
39
src/modules/hostname.rs
Normal file
39
src/modules/hostname.rs
Normal file
@ -0,0 +1,39 @@
|
||||
use ansi_term::{Color, Style};
|
||||
use std::env;
|
||||
use std::process::Command;
|
||||
|
||||
use super::{Context, Module};
|
||||
use std::ffi::OsString;
|
||||
|
||||
/// Creates a module with the system hostname
|
||||
///
|
||||
/// Will display the hostname if all of the following criteria are met:
|
||||
/// - hostname.disabled is absent or false
|
||||
/// - hostname.ssh_only is false OR the user is currently connected as an SSH session (`$SSH_CONNECTION`)
|
||||
pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
||||
let mut module = context.new_module("hostname")?;
|
||||
|
||||
let ssh_connection = env::var("SSH_CONNECTION").ok();
|
||||
if module.config_value_bool("ssh_only").unwrap_or(true) && ssh_connection.is_none() {
|
||||
return None;
|
||||
}
|
||||
|
||||
let os_hostname: OsString = gethostname::gethostname();
|
||||
|
||||
let host = match os_hostname.into_string() {
|
||||
Ok(host) => host,
|
||||
Err(bad) => {
|
||||
log::debug!("hostname is not valid UTF!\n{:?}", bad);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
let prefix = module.config_value_str("prefix").unwrap_or("").to_owned();
|
||||
let suffix = module.config_value_str("suffix").unwrap_or("").to_owned();
|
||||
|
||||
module.set_style(Color::Green.bold().dimmed());
|
||||
module.new_segment("hostname", &format!("{}{}{}", prefix, host, suffix));
|
||||
module.get_prefix().set_value("on ");
|
||||
|
||||
Some(module)
|
||||
}
|
@ -5,6 +5,7 @@ mod directory;
|
||||
mod git_branch;
|
||||
mod git_status;
|
||||
mod golang;
|
||||
mod hostname;
|
||||
mod jobs;
|
||||
mod line_break;
|
||||
mod nix_shell;
|
||||
@ -40,6 +41,7 @@ pub fn handle<'a>(module: &str, context: &'a Context) -> Option<Module<'a>> {
|
||||
"cmd_duration" => cmd_duration::module(context),
|
||||
"jobs" => jobs::module(context),
|
||||
"nix_shell" => nix_shell::module(context),
|
||||
"hostname" => hostname::module(context),
|
||||
|
||||
_ => {
|
||||
eprintln!("Error: Unknown module {}. Use starship module --list to list out all supported modules.", module);
|
||||
|
@ -13,6 +13,7 @@ use crate::modules;
|
||||
// prompt heading of config docs needs to be updated according to changes made here.
|
||||
const DEFAULT_PROMPT_ORDER: &[&str] = &[
|
||||
"username",
|
||||
"hostname",
|
||||
"directory",
|
||||
"git_branch",
|
||||
"git_status",
|
||||
|
117
tests/testsuite/hostname.rs
Normal file
117
tests/testsuite/hostname.rs
Normal file
@ -0,0 +1,117 @@
|
||||
use ansi_term::{Color, Style};
|
||||
use std::io;
|
||||
|
||||
use crate::common;
|
||||
use crate::common::TestCommand;
|
||||
|
||||
#[test]
|
||||
fn ssh_only_false() -> 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
|
||||
})
|
||||
.output()?;
|
||||
let actual = String::from_utf8(output.stdout).unwrap();
|
||||
let expected = format!("on {} ", style().paint(hostname));
|
||||
assert_eq!(expected, actual);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn no_ssh() -> io::Result<()> {
|
||||
let output = common::render_module("hostname")
|
||||
.env_clear()
|
||||
.use_config(toml::toml! {
|
||||
[hostname]
|
||||
ssh_only = true
|
||||
})
|
||||
.output()?;
|
||||
let actual = String::from_utf8(output.stdout).unwrap();
|
||||
assert_eq!("", actual);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ssh() -> 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 = true
|
||||
})
|
||||
.env("SSH_CONNECTION", "something")
|
||||
.output()?;
|
||||
let actual = String::from_utf8(output.stdout).unwrap();
|
||||
let expected = format!("on {} ", style().paint(hostname));
|
||||
assert_eq!(expected, actual);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn prefix() -> 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
|
||||
prefix = "<"
|
||||
})
|
||||
.output()?;
|
||||
let actual = String::from_utf8(output.stdout).unwrap();
|
||||
let expected = format!("on {} ", style().paint(format!("<{}", hostname)));
|
||||
assert_eq!(actual, expected);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn suffix() -> 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
|
||||
suffix = ">"
|
||||
})
|
||||
.output()?;
|
||||
let actual = String::from_utf8(output.stdout).unwrap();
|
||||
let expected = format!("on {} ", style().paint(format!("{}>", hostname)));
|
||||
assert_eq!(actual, expected);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_hostname() -> Option<String> {
|
||||
match gethostname::gethostname().into_string() {
|
||||
Ok(hostname) => Some(hostname),
|
||||
Err(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn style() -> Style {
|
||||
Color::Green.bold().dimmed()
|
||||
}
|
||||
|
||||
fn hostname_not_tested() -> io::Result<()> {
|
||||
println!(
|
||||
"hostname was not tested because gethostname failed! \
|
||||
This could be caused by your hostname containing invalid UTF."
|
||||
);
|
||||
Ok(())
|
||||
}
|
@ -6,6 +6,7 @@ mod directory;
|
||||
mod git_branch;
|
||||
mod git_status;
|
||||
mod golang;
|
||||
mod hostname;
|
||||
mod jobs;
|
||||
mod line_break;
|
||||
mod modules;
|
||||
|
Loading…
Reference in New Issue
Block a user