summaryrefslogtreecommitdiffstats
path: root/src/env.rs
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-01-03 05:33:21 -0500
committerJesse Luehrs <doy@tozt.net>2022-01-03 05:59:31 -0500
commit4142936f657004f88e259583136f25f566b0b1c0 (patch)
treea742607891e39a03587e0b22be73e3bb8dd49b32 /src/env.rs
parenta3af022f75af415709481cd6dcd73f4d33b472e4 (diff)
downloadnbsh-4142936f657004f88e259583136f25f566b0b1c0.tar.gz
nbsh-4142936f657004f88e259583136f25f566b0b1c0.zip
split up code into more files
Diffstat (limited to 'src/env.rs')
-rw-r--r--src/env.rs48
1 files changed, 16 insertions, 32 deletions
diff --git a/src/env.rs b/src/env.rs
index 3ff2576..e88ec7c 100644
--- a/src/env.rs
+++ b/src/env.rs
@@ -1,40 +1,24 @@
-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)
-}
+use std::os::unix::process::ExitStatusExt as _;
-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())
+#[derive(Clone)]
+pub struct Env {
+ latest_status: async_std::process::ExitStatus,
}
-#[allow(clippy::unnecessary_wraps)]
-pub fn prompt_char() -> anyhow::Result<String> {
- if users::get_current_uid() == 0 {
- Ok("#".into())
- } else {
- Ok("$".into())
+impl Env {
+ pub fn new(code: i32) -> Self {
+ Self {
+ latest_status: async_std::process::ExitStatus::from_raw(
+ code << 8,
+ ),
+ }
}
-}
-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);
+ pub fn set_status(&mut self, status: async_std::process::ExitStatus) {
+ self.latest_status = status;
}
- 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),
- ))
+ pub fn latest_status(&self) -> &async_std::process::ExitStatus {
+ &self.latest_status
+ }
}