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 | |
parent | db7ec0a00096a0ba70cb630a7852bf6751e3ce4c (diff) | |
download | nbsh-c2c6adf5b755b33e6b844a502cd8d61eabf89126.tar.gz nbsh-c2c6adf5b755b33e6b844a502cd8d61eabf89126.zip |
refactor
-rw-r--r-- | src/env.rs | 31 | ||||
-rw-r--r-- | src/main.rs | 1 | ||||
-rw-r--r-- | src/readline.rs | 22 |
3 files changed, 37 insertions, 17 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), + )) +} diff --git a/src/main.rs b/src/main.rs index 0220ab9..f7687cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ mod action; mod builtins; +mod env; mod format; mod history; mod parse; diff --git a/src/readline.rs b/src/readline.rs index 38c1e51..479c76f 100644 --- a/src/readline.rs +++ b/src/readline.rs @@ -59,25 +59,13 @@ impl Readline { focus: bool, offset: time::UtcOffset, ) -> anyhow::Result<()> { - let mut pwd = std::env::current_dir()?.display().to_string(); - let home = std::env::var("HOME")?; - if pwd.starts_with(&home) { - pwd.replace_range(..home.len(), "~"); - } - let user = users::get_current_username() - .unwrap() - .to_string_lossy() - .into_owned(); - let mut hostname = - hostname::get().unwrap().to_string_lossy().into_owned(); - if let Some(idx) = hostname.find('.') { - hostname.truncate(idx); - } + let pwd = crate::env::pwd()?; + let user = crate::env::user()?; + let hostname = crate::env::hostname()?; + let time = crate::env::time(offset)?; + let id = format!("{}@{}", user, hostname); let idlen: u16 = id.len().try_into().unwrap(); - let time = crate::format::time( - time::OffsetDateTime::now_utc().to_offset(offset), - ); let timelen: u16 = time.len().try_into().unwrap(); out.move_to(self.size.0 - 2, 0); |