From f6c1835b1866d4e3b46a8001cc327c436aead721 Mon Sep 17 00:00:00 2001 From: Bill Thiede Date: Mon, 20 Nov 2023 17:41:58 -0800 Subject: [PATCH] Custom formatting of the age string, widen subject column. --- Cargo.lock | 6 +++--- web/Cargo.toml | 1 + web/index.html | 5 +++-- web/src/lib.rs | 29 +++++++++++++++++++++++++++-- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5a59a1a..bfc89ff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -222,15 +222,14 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.28" +version = "0.4.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "95ed24df0632f708f5f6d8082675bef2596f7084dee3dd55f632290bf35bfe0f" +checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38" dependencies = [ "android-tzdata", "iana-time-zone", "js-sys", "num-traits", - "time 0.1.45", "wasm-bindgen", "windows-targets", ] @@ -1243,6 +1242,7 @@ checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55" name = "letterbox" version = "0.1.0" dependencies = [ + "chrono", "console_error_panic_hook", "console_log", "css-inline", diff --git a/web/Cargo.toml b/web/Cargo.toml index 0ae26d3..fe350b7 100644 --- a/web/Cargo.toml +++ b/web/Cargo.toml @@ -27,6 +27,7 @@ itertools = "0.10.5" serde_json = { version = "1.0.93", features = ["unbounded_depth"] } wasm-timer = "0.2.5" css-inline = "0.8.5" +chrono = "0.4.31" [package.metadata.wasm-pack.profile.release] wasm-opt = ['-Os'] diff --git a/web/index.html b/web/index.html index a731ea5..37b5330 100644 --- a/web/index.html +++ b/web/index.html @@ -39,7 +39,7 @@ iframe { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; - width: 15em; + width: 10em; } .index .subject { overflow: hidden; @@ -47,8 +47,9 @@ iframe { white-space: nowrap; } .index .date { - width: 8em; + width: 6em; white-space: nowrap; + text-align: right; } .footer { background-color: #eee; diff --git a/web/src/lib.rs b/web/src/lib.rs index 7aacdfa..85b725f 100644 --- a/web/src/lib.rs +++ b/web/src/lib.rs @@ -7,6 +7,7 @@ use std::{ hash::{Hash, Hasher}, }; +use chrono::{DateTime, Datelike, Duration, Local, Utc}; use itertools::Itertools; use log::{debug, error, info, Level}; use notmuch::{Content, Part, Thread, ThreadNode, ThreadSet}; @@ -481,6 +482,28 @@ fn pretty_authors(authors: &str) -> impl Iterator> + '_ { ) } +fn human_age(timestamp: i64) -> String { + let now = Local::now(); + let ts = DateTime::::from_timestamp(timestamp, 0) + .unwrap() + .with_timezone(&Local); + let age = now - ts; + let weekday = now.weekday(); + let datetime = if age < Duration::minutes(1) { + format!("{} ago", age.num_seconds()) + } else if age < Duration::hours(1) { + format!("{} ago", age.num_minutes()) + } else if age < Duration::days(1) { + ts.format("%H:%M").to_string() + } else if age < Duration::weeks(1) { + ts.format("%a %H:%M").to_string() + } else { + ts.format("%b %e").to_string() + }; + info!("dateime {datetime} TZ: {}", ts.offset()); + datetime +} + fn view_mobile_search_results(query: &str, search_results: &shared::SearchResult) -> Node { if query.is_empty() { set_title("all mail"); @@ -503,6 +526,7 @@ fn view_mobile_search_results(query: &str, search_results: &shared::SearchResult ] */ let tid = r.thread.clone(); + let datetime = human_age(r.timestamp as i64); div![ C!["row"], div![ @@ -513,7 +537,7 @@ fn view_mobile_search_results(query: &str, search_results: &shared::SearchResult span![C!["from", "is-size-7"], pretty_authors(&r.authors)], div![ span![C!["is-size-7"], tags_chiclet(&r.tags, true)], - span![C!["is-size-7", "float-right", "date"], &r.date_relative] + span![C!["is-size-7", "float-right", "date"], datetime] ] ] }); @@ -536,6 +560,7 @@ fn view_search_results(query: &str, search_results: &shared::SearchResult) -> No let summaries = &search_results.summary.0; let rows = summaries.iter().map(|r| { let tid = r.thread.clone(); + let datetime = human_age(r.timestamp as i64); tr![ td![ C!["from"], @@ -554,7 +579,7 @@ fn view_search_results(query: &str, search_results: &shared::SearchResult) -> No &r.subject, ] ], - td![C!["date"], &r.date_relative] + td![C!["date"], datetime] ] }); let first = search_results.page * search_results.results_per_page;