Move timestamp format outside or render

This commit is contained in:
Alvaro Parker 2025-08-14 16:57:42 -04:00
parent b004c052c6
commit 5662056ea3
No known key found for this signature in database

View file

@ -188,6 +188,7 @@ impl Render for StashList {
struct StashEntryMatch { struct StashEntryMatch {
entry: StashEntry, entry: StashEntry,
positions: Vec<usize>, positions: Vec<usize>,
formatted_timestamp: String,
} }
pub struct StashListDelegate { pub struct StashListDelegate {
@ -200,6 +201,7 @@ pub struct StashListDelegate {
modifiers: Modifiers, modifiers: Modifiers,
max_width: Rems, max_width: Rems,
focus_handle: FocusHandle, focus_handle: FocusHandle,
timezone: UtcOffset,
} }
impl StashListDelegate { impl StashListDelegate {
@ -210,6 +212,10 @@ impl StashListDelegate {
_window: &mut Window, _window: &mut Window,
cx: &mut Context<StashList>, cx: &mut Context<StashList>,
) -> Self { ) -> Self {
let timezone =
UtcOffset::from_whole_seconds(chrono::Local::now().offset().local_minus_utc())
.unwrap_or(UtcOffset::UTC);
Self { Self {
matches: vec![], matches: vec![],
repo, repo,
@ -220,6 +226,7 @@ impl StashListDelegate {
modifiers: Default::default(), modifiers: Default::default(),
max_width, max_width,
focus_handle: cx.focus_handle(), focus_handle: cx.focus_handle(),
timezone,
} }
} }
@ -227,6 +234,17 @@ impl StashListDelegate {
format!("#{}: {}", ix, message) format!("#{}: {}", ix, message)
} }
fn format_timestamp(timestamp: i64, timezone: UtcOffset) -> String {
let timestamp =
OffsetDateTime::from_unix_timestamp(timestamp).unwrap_or(OffsetDateTime::now_utc());
time_format::format_localized_timestamp(
timestamp,
OffsetDateTime::now_utc(),
timezone,
time_format::TimestampFormat::EnhancedAbsolute,
)
}
fn drop_stash_at(&self, ix: usize, window: &mut Window, cx: &mut Context<Picker<Self>>) { fn drop_stash_at(&self, ix: usize, window: &mut Window, cx: &mut Context<Picker<Self>>) {
let Some(entry_match) = self.matches.get(ix) else { let Some(entry_match) = self.matches.get(ix) else {
return; return;
@ -304,13 +322,20 @@ impl PickerDelegate for StashListDelegate {
return Task::ready(()); return Task::ready(());
}; };
let timezone = self.timezone;
cx.spawn_in(window, async move |picker, cx| { cx.spawn_in(window, async move |picker, cx| {
let matches: Vec<StashEntryMatch> = if query.is_empty() { let matches: Vec<StashEntryMatch> = if query.is_empty() {
all_stash_entries all_stash_entries
.into_iter() .into_iter()
.map(|entry| StashEntryMatch { .map(|entry| {
let formatted_timestamp = Self::format_timestamp(entry.timestamp, timezone);
StashEntryMatch {
entry, entry,
positions: Vec::new(), positions: Vec::new(),
formatted_timestamp,
}
}) })
.collect() .collect()
} else { } else {
@ -335,9 +360,15 @@ impl PickerDelegate for StashListDelegate {
) )
.await .await
.into_iter() .into_iter()
.map(|candidate| StashEntryMatch { .map(|candidate| {
entry: all_stash_entries[candidate.candidate_id].clone(), let entry = all_stash_entries[candidate.candidate_id].clone();
let formatted_timestamp = Self::format_timestamp(entry.timestamp, timezone);
StashEntryMatch {
entry,
positions: candidate.positions, positions: candidate.positions,
formatted_timestamp,
}
}) })
.collect() .collect()
}; };
@ -418,21 +449,9 @@ impl PickerDelegate for StashListDelegate {
.size(LabelSize::Small) .size(LabelSize::Small)
.color(Color::Muted); .color(Color::Muted);
let timestamp = OffsetDateTime::from_unix_timestamp(entry_match.entry.timestamp)
.unwrap_or(OffsetDateTime::now_utc());
let timezone =
UtcOffset::from_whole_seconds(chrono::Local::now().offset().local_minus_utc())
.unwrap_or(UtcOffset::UTC);
let absolute_timestamp = time_format::format_localized_timestamp(
timestamp,
OffsetDateTime::now_utc(),
timezone,
time_format::TimestampFormat::EnhancedAbsolute,
);
let tooltip_text = format!( let tooltip_text = format!(
"stash@{{{}}} created {}", "stash@{{{}}} created {}",
entry_match.entry.index, absolute_timestamp entry_match.entry.index, entry_match.formatted_timestamp
); );
Some( Some(