index-format-hook date pattern for current week
Ed Blackman
ed at edgewood.to
Fri Nov 5 00:02:34 UTC 2021
On Tue, Aug 31, 2021 at 05:54:23AM -0700, lists at ifohancroft.com wrote:
[...]
> index-format-hook date "~d<1d" "%[%H:%M]" # If it's from today - I only want
> the time
> index-format-hook date "~d<1w" "%[%d %a]" # If it's from this week - I only
> want the day and the date
[...]
> The rest works as I want it to and as I expect it to, besides the week
> pattern. If any email is from the last seven days, it gets caught by the
> week pattern. I don't want that. I want only emails from the current week to
> get caught by the week pattern, not all emails from the last 7 days.
>
> Here's what is currently happening:
>
> Today is Tuesday, August 31st. In my email, currently, the oldest email
> being caught by the week pattern is from Wednesday, August 25th.
>
> Here's what I want to be happening:
>
> Today is Tuesday, August 31st. In my email, the oldest email that should be
> getting caught by the week pattern should be from Monday, August 30th.
Sorry for the late response, and I don't have a direct answer to your question, but since I didn't see any direct answers I figured I'd share my solution that might be something that you can adapt.
I use a Mutt pipe variable to let me use a script to set the index format, passing the datetime of the message and the current datetime as variables. I then have the script do date calcuations and echo an otherwise fixed index format that has a variable date format.
My current script does what your index-format-hook does: shows the weekday name if the difference between the message and the current datetime is between 7 and 1 days. But since it's a shell script, the test can be changed to something else to do what you want.
Despite running the shell script for each visible message, I don't notice any slowdown.
Here's an untested patch for what you're asking for, using GNU date supporting the %V format string (ISO week number with Monday as the first day of the %week). Note that this runs two more commands ("date" twice) for dates that are less than 7 days old. I doubt that will make it noticibly slower, but test it.
Replace
format="%8[%a %-I%P]" # ' Thu 6pm'
with
# use week day name only within the same ISO week number, with weeks starting on Monday
if [ "$(date +%V -d "@$now")" = "$(date +%V -d "@$msg_date") ]; then
format="%8[%a %-I%P]" # ' Thu 6pm'
else
format="%8[%b %d]" # ' Jan 20'
fi
In ~/.mutt/muttrc:
# Show different date/time formats in index based on message age
# WORKAROUND: '<%s>' used to work, but doesn't in NeoMutt 1.7.2. Width specifier fixes.
set index_format="/home/edgewood/.mutt/bin/format_date '%[%s]' '%1<%s>' |"
~/.mutt/bin/format_date:
#!/bin/bash
# format_date
#
# In .muttrc:
# set index_format="/path/to/format_date '%[%s]' '%<%s>' |"
#
# http://groups.google.com/group/de.comm.software.mailreader.misc/browse_thread/thread/ab966bddc0b424 46/421549103438b830?q=#421549103438b830
# via Andreas Kneib <aporia at web.de>
# mutt-users Message-ID: <20110105233817.GA23989 at andreas.kneib.biz>
# Improvements by
# David Champion <dgc at uchicago.edu>
# Ed Blackman <ed at edgewood.to>
# 2018-10-24: remove annoying ^N and spaces added by NeoMutt 1.7
# arguments are both epoch seconds, so limiting to just digits is safe
msg_date="${1//[!0-9]}" # datetime of message in local timezone in epoch seconds
now="${2//[!0-9]}" # current time in local timezone in epoch seconds
msg_age="$(( ($now - $msg_date) / 86400 ))" # age of message in integer days
if [ $msg_age -ge 30 ]; then
format="%[%m/%d/%y]" # '01/20/11'
elif [ $msg_age -ge 7 ]; then
format="%8[%b %d]" # ' Jan 20'
elif [ $msg_age -ge 1 ]; then
format="%8[%a %-I%P]" # ' Thu 6pm'
else
format="%[ %_I:%M%P]" # ' 6:41pm'
fi
echo "%4C %Z $format %-15.15F (%?l?%4l&%4c?) %?H?[%H]?%s%"
--
Ed Blackman
More information about the Mutt-users
mailing list