crabmail

Static HTML email archive viewer in Rust
git clone git://git.alexwennerberg.com/crabmail
Log | Files | Refs | README | LICENSE

commit bd092a82d4c04415942390649ac741de3edf8de6
parent 9c3f9fa9b1db8964ffd8c4e49d779aad98a4695d
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Mon, 27 Dec 2021 11:44:53 -0800

Add option for relative timestamps in config

Diffstat:
Msrc/arg.rs | 8++++++--
Msrc/config.rs | 2++
Msrc/main.rs | 7++++++-
Msrc/time.rs | 14+++++++-------
4 files changed, 21 insertions(+), 10 deletions(-)

diff --git a/src/arg.rs b/src/arg.rs @@ -19,6 +19,8 @@ fn usage() -> ! { let name = env::args().next().unwrap(); eprintln!( "usage: {} [mbox-file] +FLAGS: +-r use relative timestamps ARGS: -c config file (crabmail.conf) @@ -32,12 +34,13 @@ pub struct Args { pub mbox: PathBuf, pub config: PathBuf, pub out_dir: PathBuf, + pub flags: String, } impl Args { pub fn from_env() -> Self { // Modify as needed - // let mut flags = String::new(); + let mut flags = String::new(); let mut mbox: Option<String> = None; let mut out_dir = "site".into(); let mut config = "crabmail.conf".into(); @@ -58,7 +61,7 @@ impl Args { chars.for_each(|m| match m { 'c' => config = parsenext(args.next()), 'd' => out_dir = parsenext(args.next()), - // 'a' | 'b' => flags.push(m), + 'r' => flags.push(m), _ => { usage(); } @@ -71,6 +74,7 @@ impl Args { None => usage(), }, out_dir, + flags, } } } diff --git a/src/config.rs b/src/config.rs @@ -16,6 +16,7 @@ pub struct Config { // WIP pub ok_attachments: Vec<String>, pub out_dir: PathBuf, + pub relative_times: bool, } pub static INSTANCE: OnceCell<Config> = OnceCell::new(); @@ -63,6 +64,7 @@ impl Config { homepage, out_dir: PathBuf::from(""), ok_attachments, + relative_times: false, }) } } diff --git a/src/main.rs b/src/main.rs @@ -162,6 +162,10 @@ impl<'a> ThreadList<'a> { Ok(()) } pub fn write_to_file(&self) -> Result<()> { + let timestring = match Config::global().relative_times { + false => |t| time::secs_to_date(t).ymd(), + true => |t| time::timeago(t), + }; let tmp = html! { h1(class="page-title") { : &Config::global().list_name; @@ -193,7 +197,7 @@ impl<'a> ThreadList<'a> { } span(class="timeago") { - : format!(" {created} | updated {last}", created=time::secs_to_date(thread.messages[0].date).ymd(), last=time::secs_to_date(thread.last_reply()).ymd()) + : format!(" {created} | updated {last}", created=timestring(thread.messages[0].date), last=timestring(thread.last_reply())) } br; } @@ -472,6 +476,7 @@ fn main() -> Result<()> { let mut config = Config::from_file(&args.config).unwrap(); // TODO better err handling config.out_dir = args.out_dir; + config.relative_times = args.flags.contains('r'); INSTANCE.set(config).unwrap(); let out_dir = &Config::global().out_dir; diff --git a/src/time.rs b/src/time.rs @@ -127,13 +127,13 @@ pub fn timeago(unixtime: u64) -> String { let diff = current_time - unixtime; let amount: u64; let metric: &str; - if diff < 60 { - amount = diff; - metric = "second"; - } else if diff < 60 * 60 { - amount = diff / 60; - metric = "minute"; - } else if diff < 60 * 60 * 24 { + // if diff < 60 { + // amount = diff; + // metric = "second"; + // } else if diff < 60 * 60 { + // amount = diff / 60; + // metric = "minute"; + if diff < 60 * 60 * 24 { amount = diff / (60 * 60); metric = "hour"; } else if diff < SOLAR_YEAR_SECS * 2 {