summaryrefslogtreecommitdiffstats
path: root/src/format.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/format.rs')
-rw-r--r--src/format.rs51
1 files changed, 42 insertions, 9 deletions
diff --git a/src/format.rs b/src/format.rs
index 50424f0..115ee6c 100644
--- a/src/format.rs
+++ b/src/format.rs
@@ -1,15 +1,39 @@
-use std::os::unix::process::ExitStatusExt as _;
+use crate::prelude::*;
-pub fn exit_status(status: std::process::ExitStatus) -> String {
- if let Some(sig) = status.signal() {
- if let Some(name) = signal_hook::low_level::signal_name(sig) {
- format!("{:4} ", &name[3..])
- } else {
- format!("SIG{} ", sig)
+pub fn path(path: &std::path::Path) -> String {
+ let mut path = path.display().to_string();
+ if let Ok(home) = std::env::var("HOME") {
+ if path.starts_with(&home) {
+ path.replace_range(..home.len(), "~");
}
- } else {
- format!("{:03} ", status.code().unwrap())
}
+ path
+}
+
+pub fn exit_status(status: std::process::ExitStatus) -> String {
+ status.signal().map_or_else(
+ || format!("{:03} ", status.code().unwrap()),
+ |sig| {
+ nix::sys::signal::Signal::try_from(sig).map_or_else(
+ |_| format!("SIG{} ", sig),
+ |sig| format!("{:4} ", &sig.as_str()[3..]),
+ )
+ },
+ )
+}
+
+pub fn time(time: time::OffsetDateTime) -> String {
+ let format = if time::OffsetDateTime::now_utc() - time
+ > std::time::Duration::from_secs(60 * 60 * 24)
+ {
+ time::format_description::parse(
+ "[year]-[month]-[day] [hour]:[minute]:[second]",
+ )
+ .unwrap()
+ } else {
+ time::format_description::parse("[hour]:[minute]:[second]").unwrap()
+ };
+ time.format(&format).unwrap()
}
pub fn duration(dur: std::time::Duration) -> String {
@@ -31,3 +55,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
+}