listing: hoist format_date() to ListingTrait method
Tests / Test on ${{ matrix.build }} (linux-amd64, ubuntu-latest, stable, x86_64-unknown-linux-gnu) (pull_request) Successful in 19m32s Details
Tests / Test on ${{ matrix.build }} (linux-amd64, ubuntu-latest, stable, x86_64-unknown-linux-gnu) (push) Successful in 10m27s Details

For reusability.

Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>
pull/311/head
Manos Pitsidianakis 2023-10-21 10:42:24 +03:00
parent 5a7919bb03
commit 0f3b529459
Signed by: Manos Pitsidianakis
GPG Key ID: 7729C7707F7E09D0
5 changed files with 42 additions and 40 deletions

View File

@ -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)]

View File

@ -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 {

View File

@ -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

View File

@ -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}",

View File

@ -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)),