From c2c6adf5b755b33e6b844a502cd8d61eabf89126 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Thu, 9 Dec 2021 17:15:17 -0500 Subject: refactor --- src/env.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/env.rs (limited to 'src/env.rs') diff --git a/src/env.rs b/src/env.rs new file mode 100644 index 0000000..676b20e --- /dev/null +++ b/src/env.rs @@ -0,0 +1,31 @@ +pub fn pwd() -> anyhow::Result { + let mut pwd = std::env::current_dir()?.display().to_string(); + if let Ok(home) = std::env::var("HOME") { + if pwd.starts_with(&home) { + pwd.replace_range(..home.len(), "~"); + } + } + Ok(pwd) +} + +pub fn user() -> anyhow::Result { + Ok(users::get_current_username() + .ok_or_else(|| anyhow::anyhow!("couldn't get username"))? + .to_string_lossy() + .into_owned()) +} + +pub fn hostname() -> anyhow::Result { + let mut hostname = hostname::get()?.to_string_lossy().into_owned(); + if let Some(idx) = hostname.find('.') { + hostname.truncate(idx); + } + Ok(hostname) +} + +#[allow(clippy::unnecessary_wraps)] +pub fn time(offset: time::UtcOffset) -> anyhow::Result { + Ok(crate::format::time( + time::OffsetDateTime::now_utc().to_offset(offset), + )) +} -- cgit v1.2.3-54-g00ecf