diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-12-09 17:15:17 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-12-09 17:15:17 -0500 |
commit | c2c6adf5b755b33e6b844a502cd8d61eabf89126 (patch) | |
tree | 58f7f78f7ff5218e5535da94be992d422e1035b4 /src/env.rs | |
parent | db7ec0a00096a0ba70cb630a7852bf6751e3ce4c (diff) | |
download | nbsh-c2c6adf5b755b33e6b844a502cd8d61eabf89126.tar.gz nbsh-c2c6adf5b755b33e6b844a502cd8d61eabf89126.zip |
refactor
Diffstat (limited to 'src/env.rs')
-rw-r--r-- | src/env.rs | 31 |
1 files changed, 31 insertions, 0 deletions
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<String> { + 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<String> { + Ok(users::get_current_username() + .ok_or_else(|| anyhow::anyhow!("couldn't get username"))? + .to_string_lossy() + .into_owned()) +} + +pub fn hostname() -> anyhow::Result<String> { + 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<String> { + Ok(crate::format::time( + time::OffsetDateTime::now_utc().to_offset(offset), + )) +} |