diff --git a/Cargo.lock b/Cargo.lock index 2e3b7789..d793acba 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -970,16 +970,6 @@ dependencies = [ "version_check", ] -[[package]] -name = "gethostname" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc3655aa6818d65bc620d6911f05aa7b6aeb596291e1e9f79e52df85583d1e30" -dependencies = [ - "rustix", - "windows-targets 0.52.6", -] - [[package]] name = "getrandom" version = "0.2.15" @@ -2762,7 +2752,6 @@ dependencies = [ "deelevate", "dirs 5.0.1", "dunce", - "gethostname", "gix", "gix-features", "guess_host_triple", @@ -2804,6 +2793,7 @@ dependencies = [ "urlencoding", "versions", "which", + "whoami", "windows 0.58.0", "winres", "yaml-rust2", @@ -3293,6 +3283,12 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "wasite" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" + [[package]] name = "wasm-bindgen" version = "0.2.95" @@ -3360,6 +3356,16 @@ dependencies = [ "winsafe", ] +[[package]] +name = "whoami" +version = "1.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "372d5b87f58ec45c384ba03563b03544dc5fadc3983e434b286913f5b4a9bb6d" +dependencies = [ + "redox_syscall", + "wasite", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 5b89f8aa..a4dabc0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,7 @@ license = "ISC" readme = "README.md" repository = "https://github.com/starship/starship" # Note: MSRV is only intended as a hint, and only the latest version is officially supported in starship. -rust-version = "1.74" +rust-version = "1.76" description = """ The minimal, blazing-fast, and infinitely customizable prompt for any shell! ☄🌌️ """ @@ -47,7 +47,6 @@ clap = { version = "4.5.20", features = ["derive", "cargo", "unicode"] } clap_complete = "4.5.33" dirs = "5.0.1" dunce = "1.0.5" -gethostname = "0.5.0" # default feature restriction addresses https://github.com/starship/starship/issues/4251 gix = { version = "0.66.0", default-features = false, features = ["max-performance-safe", "revision"] } gix-features = { version = "0.38.2", optional = true } @@ -88,6 +87,7 @@ unicode-width = "0.2.0" urlencoding = "2.1.3" versions = "6.3.2" which = "6.0.3" +whoami = { version = "1.5.2", default-features = false } yaml-rust2 = "0.9.0" guess_host_triple = "0.1.4" diff --git a/src/modules/hostname.rs b/src/modules/hostname.rs index c89edb41..c5392573 100644 --- a/src/modules/hostname.rs +++ b/src/modules/hostname.rs @@ -1,10 +1,11 @@ use super::{Context, Module}; -use std::ffi::OsString; use crate::config::ModuleConfig; use crate::configs::hostname::HostnameConfig; use crate::formatter::StringFormatter; +use whoami::fallible::hostname; + /// Creates a module with the system hostname /// /// Will display the hostname if all of the following criteria are met: @@ -23,15 +24,9 @@ pub fn module<'a>(context: &'a Context) -> Option> { return None; } - let os_hostname: OsString = gethostname::gethostname(); - - let host = match os_hostname.into_string() { - Ok(host) => host, - Err(bad) => { - log::warn!("hostname is not valid UTF!\n{:?}", bad); - return None; - } - }; + let host = hostname() + .inspect_err(|e| log::warn!("Failed to get hostname: {e}")) + .ok()?; //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 @@ -91,7 +86,7 @@ mod tests { macro_rules! get_hostname { () => { - if let Ok(hostname) = gethostname::gethostname().into_string() { + if let Ok(hostname) = whoami::fallible::hostname() { hostname } else { println!( diff --git a/src/modules/username.rs b/src/modules/username.rs index f2c5164e..8ba3509e 100644 --- a/src/modules/username.rs +++ b/src/modules/username.rs @@ -19,6 +19,13 @@ const USERNAME_ENV_VAR: &str = "USERNAME"; /// Does not display the username: /// - If the option `username.detect_env_vars` is set with a negated environment variable [A] pub fn module<'a>(context: &'a Context) -> Option> { + #[cfg(not(test))] + let mut username = whoami::fallible::username() + .inspect_err(|e| log::debug!("Failed to get username {e:?}")) + .ok() + .or_else(|| context.get_env(USERNAME_ENV_VAR))?; + + #[cfg(test)] let mut username = context.get_env(USERNAME_ENV_VAR)?; let mut module = context.new_module("username");