diff --git a/Cargo.lock b/Cargo.lock index e6726b9a0..5bde96f65 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 52d83d774..65b976ceb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/docs/config/README.md b/docs/config/README.md index 732e1528d..e7e8e5f4b 100644 --- a/docs/config/README.md +++ b/docs/config/README.md @@ -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. diff --git a/src/modules/hostname.rs b/src/modules/hostname.rs new file mode 100644 index 000000000..51567bcbb --- /dev/null +++ b/src/modules/hostname.rs @@ -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> { + 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) +} diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 6c6861efe..23d82601f 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -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> { "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); diff --git a/src/print.rs b/src/print.rs index c3d2e8f41..74997b26d 100644 --- a/src/print.rs +++ b/src/print.rs @@ -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", diff --git a/tests/testsuite/hostname.rs b/tests/testsuite/hostname.rs new file mode 100644 index 000000000..8a1a7bbd9 --- /dev/null +++ b/tests/testsuite/hostname.rs @@ -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 { + 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(()) +} diff --git a/tests/testsuite/main.rs b/tests/testsuite/main.rs index cd96b8e85..82da9cccf 100644 --- a/tests/testsuite/main.rs +++ b/tests/testsuite/main.rs @@ -6,6 +6,7 @@ mod directory; mod git_branch; mod git_status; mod golang; +mod hostname; mod jobs; mod line_break; mod modules;