listing: hoist format_date() to ListingTrait method
For reusability. Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>pull/311/head
parent
5a7919bb03
commit
0f3b529459
|
@ -30,7 +30,9 @@ use std::{
|
|||
};
|
||||
|
||||
use futures::future::try_join_all;
|
||||
use melib::{backends::EnvelopeHashBatch, mbox::MboxMetadata, utils::datetime, Address};
|
||||
use melib::{
|
||||
backends::EnvelopeHashBatch, mbox::MboxMetadata, utils::datetime, Address, UnixTimestamp,
|
||||
};
|
||||
use smallvec::SmallVec;
|
||||
|
||||
use super::*;
|
||||
|
@ -895,6 +897,40 @@ pub trait ListingTrait: Component {
|
|||
content: Box::new(msg),
|
||||
});
|
||||
}
|
||||
|
||||
fn format_date(&self, context: &Context, epoch: UnixTimestamp) -> String {
|
||||
let d = std::time::UNIX_EPOCH + std::time::Duration::from_secs(epoch);
|
||||
let now: std::time::Duration = std::time::SystemTime::now()
|
||||
.duration_since(d)
|
||||
.unwrap_or_else(|_| std::time::Duration::new(std::u64::MAX, 0));
|
||||
match now.as_secs() {
|
||||
n if context.settings.listing.recent_dates && n < 60 * 60 => format!(
|
||||
"{} minute{} ago",
|
||||
n / (60),
|
||||
if n / 60 == 1 { "" } else { "s" }
|
||||
),
|
||||
n if context.settings.listing.recent_dates && n < 24 * 60 * 60 => format!(
|
||||
"{} hour{} ago",
|
||||
n / (60 * 60),
|
||||
if n / (60 * 60) == 1 { "" } else { "s" }
|
||||
),
|
||||
n if context.settings.listing.recent_dates && n < 7 * 24 * 60 * 60 => format!(
|
||||
"{} day{} ago",
|
||||
n / (24 * 60 * 60),
|
||||
if n / (24 * 60 * 60) == 1 { "" } else { "s" }
|
||||
),
|
||||
_ => melib::utils::datetime::timestamp_to_string(
|
||||
epoch,
|
||||
context
|
||||
.settings
|
||||
.listing
|
||||
.datetime_fmt
|
||||
.as_deref()
|
||||
.or(Some("%Y-%m-%d %T")),
|
||||
false,
|
||||
),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
|
@ -1063,7 +1063,7 @@ impl CompactListing {
|
|||
root_envelope.subject().trim().to_string()
|
||||
};
|
||||
EntryStrings {
|
||||
date: DateString(ConversationsListing::format_date(context, thread.date())),
|
||||
date: DateString(self.format_date(context, thread.date())),
|
||||
subject: if thread.len() > 1 {
|
||||
SubjectString(format!("{} ({})", subject, thread.len()))
|
||||
} else {
|
||||
|
|
|
@ -22,7 +22,7 @@
|
|||
use std::{collections::BTreeMap, iter::FromIterator};
|
||||
|
||||
use indexmap::IndexSet;
|
||||
use melib::{SortField, SortOrder, TagHash, Threads, UnixTimestamp};
|
||||
use melib::{SortField, SortOrder, TagHash, Threads};
|
||||
|
||||
use super::*;
|
||||
use crate::{components::PageMovement, jobs::JoinHandle};
|
||||
|
@ -795,7 +795,7 @@ impl ConversationsListing {
|
|||
root_envelope.subject().trim().to_string()
|
||||
};
|
||||
EntryStrings {
|
||||
date: DateString(ConversationsListing::format_date(context, thread.date())),
|
||||
date: DateString(self.format_date(context, thread.date())),
|
||||
subject: SubjectString(if thread.len() > 1 {
|
||||
format!("{} ({})", subject, thread.len())
|
||||
} else {
|
||||
|
@ -811,40 +811,6 @@ impl ConversationsListing {
|
|||
}
|
||||
}
|
||||
|
||||
pub(super) fn format_date(context: &Context, epoch: UnixTimestamp) -> String {
|
||||
let d = std::time::UNIX_EPOCH + std::time::Duration::from_secs(epoch);
|
||||
let now: std::time::Duration = std::time::SystemTime::now()
|
||||
.duration_since(d)
|
||||
.unwrap_or_else(|_| std::time::Duration::new(std::u64::MAX, 0));
|
||||
match now.as_secs() {
|
||||
n if context.settings.listing.recent_dates && n < 60 * 60 => format!(
|
||||
"{} minute{} ago",
|
||||
n / (60),
|
||||
if n / 60 == 1 { "" } else { "s" }
|
||||
),
|
||||
n if context.settings.listing.recent_dates && n < 24 * 60 * 60 => format!(
|
||||
"{} hour{} ago",
|
||||
n / (60 * 60),
|
||||
if n / (60 * 60) == 1 { "" } else { "s" }
|
||||
),
|
||||
n if context.settings.listing.recent_dates && n < 7 * 24 * 60 * 60 => format!(
|
||||
"{} day{} ago",
|
||||
n / (24 * 60 * 60),
|
||||
if n / (24 * 60 * 60) == 1 { "" } else { "s" }
|
||||
),
|
||||
_ => melib::utils::datetime::timestamp_to_string(
|
||||
epoch,
|
||||
context
|
||||
.settings
|
||||
.listing
|
||||
.datetime_fmt
|
||||
.as_deref()
|
||||
.or(Some("%Y-%m-%d %T")),
|
||||
false,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn get_thread_under_cursor(&self, cursor: usize) -> Option<ThreadHash> {
|
||||
if self.filter_term.is_empty() {
|
||||
self.rows
|
||||
|
|
|
@ -748,7 +748,7 @@ impl PlainListing {
|
|||
}
|
||||
let subject = e.subject().trim().to_string();
|
||||
EntryStrings {
|
||||
date: DateString(ConversationsListing::format_date(context, e.date())),
|
||||
date: DateString(self.format_date(context, e.date())),
|
||||
subject: SubjectString(subject),
|
||||
flag: FlagString(format!(
|
||||
"{selected}{unseen}{attachments}{whitespace}",
|
||||
|
|
|
@ -902,7 +902,7 @@ impl ThreadListing {
|
|||
}
|
||||
let subject = e.subject().trim().to_string();
|
||||
EntryStrings {
|
||||
date: DateString(ConversationsListing::format_date(context, e.date())),
|
||||
date: DateString(self.format_date(context, e.date())),
|
||||
subject: SubjectString(subject),
|
||||
flag: FlagString((if e.has_attachments() { "📎" } else { "" }).to_string()),
|
||||
from: FromString(address_list!((e.from()) as comma_sep_list)),
|
||||
|
|
Loading…
Reference in New Issue