From 03cb6be298a428ae0e75c373ba9bf4fc1fa3759a Mon Sep 17 00:00:00 2001 From: Sir-Photch Date: Sun, 14 Jan 2024 11:35:06 +0100 Subject: [PATCH] include hours and days in expiry message --- src/main.rs | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/src/main.rs b/src/main.rs index a52e878..a61f5f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -156,17 +156,26 @@ fn paste_worker( let paste_dir = Path::new(&args.paste_dir); let paste_timeout = Duration::from_secs(args.paste_expiry_sec); let exceeded_message = format!("Exceeded limit of {} kiB\n", args.paste_len_kib); - let expiry_message = format!( - "{}/_ID_ | 🧦 expires in {}\n", - args.host, - match args.paste_expiry_sec { - x if x > 60 => match x % 60 { - y if y > 0 => format!("{}m {}s", x / 60, y), - _ => format!("{}m", x / 60), - }, - x => format!("{}s", x), - } - ); + + let exp_d = args.paste_expiry_sec / (60 * 60 * 24); + let exp_h = (args.paste_expiry_sec % (60 * 60 * 24)) / 3600; + let exp_m = (args.paste_expiry_sec % 3600) / 60; + let exp_s = args.paste_expiry_sec % 60; + + let mut expiry_message = format!("{}/_ID_ | 🧦 expires in", args.host); + if 0 < exp_d { + expiry_message.push_str(&format!(" {}d", exp_d)); + } + if 0 < exp_h { + expiry_message.push_str(&format!(" {}h", exp_h)); + } + if 0 < exp_m { + expiry_message.push_str(&format!(" {}m", exp_m)); + } + if 0 < exp_s { + expiry_message.push_str(&format!(" {}s", exp_s)); + } + expiry_message.push('\n'); let mut buf = Vec::with_capacity(paste_limit + slack);