diff options
author | Jesse Luehrs <doy@tozt.net> | 2021-12-25 23:17:41 -0500 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2021-12-25 23:17:41 -0500 |
commit | 708b3976e4e70fb4f97bf12a552232ef295fc4a1 (patch) | |
tree | b5d95d1c028666dcf98ab095a74ebb8658c5352e | |
parent | a4a895c8aa24664ee0d0418115015efd10f483c4 (diff) | |
download | nbsh-708b3976e4e70fb4f97bf12a552232ef295fc4a1.tar.gz nbsh-708b3976e4e70fb4f97bf12a552232ef295fc4a1.zip |
improve error formatting
-rw-r--r-- | src/format.rs | 9 | ||||
-rw-r--r-- | src/state/history/builtins.rs | 8 | ||||
-rw-r--r-- | src/state/history/mod.rs | 12 |
3 files changed, 25 insertions, 4 deletions
diff --git a/src/format.rs b/src/format.rs index e552f53..e88f5a9 100644 --- a/src/format.rs +++ b/src/format.rs @@ -37,3 +37,12 @@ pub fn duration(dur: std::time::Duration) -> String { format!("{}ns", nanos) } } + +pub fn io_error(e: &std::io::Error) -> String { + let mut s = format!("{}", e); + if e.raw_os_error().is_some() { + let i = s.rfind('(').unwrap(); + s.truncate(i - 1); + } + s +} diff --git a/src/state/history/builtins.rs b/src/state/history/builtins.rs index 3186bb8..1bf9d00 100644 --- a/src/state/history/builtins.rs +++ b/src/state/history/builtins.rs @@ -92,7 +92,13 @@ async fn cd( Ok(()) => 0, Err(e) => { env.write_vt( - format!("{}: {}: {}", exe.exe(), e, dir.display()).as_bytes(), + format!( + "{}: {}: {}", + exe.exe(), + crate::format::io_error(&e), + dir.display() + ) + .as_bytes(), ) .await; 1 diff --git a/src/state/history/mod.rs b/src/state/history/mod.rs index 581dc58..3949769 100644 --- a/src/state/history/mod.rs +++ b/src/state/history/mod.rs @@ -1,7 +1,6 @@ use async_std::io::{ReadExt as _, WriteExt as _}; use futures_lite::future::FutureExt as _; use pty_process::Command as _; -use std::error::Error as _; use std::os::unix::process::ExitStatusExt as _; mod builtins; @@ -650,9 +649,16 @@ async fn run_exe( Ok(child) => child, Err(e) => { let mut entry = env.entry().await; + let e_str = match e { + pty_process::Error::CreatePty(e) + | pty_process::Error::SetTermSize(e) + | pty_process::Error::Spawn(e) => match e { + pty_process::Source::Io(e) => crate::format::io_error(&e), + _ => e.to_string(), + }, + }; entry.vt.process( - format!("nbsh: {}: {}", e.source().unwrap(), exe.exe()) - .as_bytes(), + format!("nbsh: {}: {}", e_str, exe.exe()).as_bytes(), ); return async_std::process::ExitStatus::from_raw(1 << 8); } |