index: allow dynamic formatting of message dates

When a message has been sent today (or this year) allow formatting the
date differently.

For example, with:

 [ui]
 index-format=%-25.25n   %-25.25D   %s
 timestamp-format=2006 Jan 02, 15:04 GMT-0700
 this-day-time-format=Today at 15:04
 this-year-time-format=Jan 02

The message list would look like this (spaces collapsed):

 Robin Jarry        Today at 16:30             [PATCH 1/2] bindings: prepare for more modifers
 bugzilla@dpdk.org  Oct 26                     [dpdk-dev] [Bug 839] pdump: any subsequent runs of pdump_autotest fail
 Holger Levsen      2020 Mar 15, 13:44 GMT+01  +1 (Re: FTP Team -- call for volunteers)

Signed-off-by: Robin Jarry <robin@jarry.cc>
This commit is contained in:
Robin Jarry 2021-10-26 17:24:45 +02:00
parent fea57839fb
commit 42b4302ba3
6 changed files with 53 additions and 7 deletions

View File

@ -15,6 +15,20 @@ index-format=%D %-17.17n %Z %s
# Default: 2006-01-02 03:04 PM (ISO 8601 + 12 hour time) # Default: 2006-01-02 03:04 PM (ISO 8601 + 12 hour time)
timestamp-format=2006-01-02 03:04 PM timestamp-format=2006-01-02 03:04 PM
#
# Index-only time format for messages that were received/sent today.
# If this is not specified, timestamp-format is used instead.
#
# Default: ""
today-time-format=
#
# Index-only time format for messages that were received/sent this year.
# If this is not specified, timestamp-format is used instead.
#
# Default: ""
this-year-time-format=
# #
# Width of the sidebar, including the border. # Width of the sidebar, including the border.
# #

View File

@ -29,6 +29,8 @@ type GeneralConfig struct {
type UIConfig struct { type UIConfig struct {
IndexFormat string `ini:"index-format"` IndexFormat string `ini:"index-format"`
TimestampFormat string `ini:"timestamp-format"` TimestampFormat string `ini:"timestamp-format"`
ThisDayTimeFormat string `ini:"this-day-time-format"`
ThisYearTimeFormat string `ini:"this-year-time-format"`
ShowHeaders []string `delim:","` ShowHeaders []string `delim:","`
RenderAccountTabs string `ini:"render-account-tabs"` RenderAccountTabs string `ini:"render-account-tabs"`
PinnedTabMarker string `ini:"pinned-tab-marker"` PinnedTabMarker string `ini:"pinned-tab-marker"`
@ -491,8 +493,10 @@ func LoadConfigFromFile(root *string, sharedir string) (*AercConfig, error) {
Ini: file, Ini: file,
Ui: UIConfig{ Ui: UIConfig{
IndexFormat: "%D %-17.17n %s", IndexFormat: "%D %-17.17n %s",
TimestampFormat: "2006-01-02 03:04 PM", TimestampFormat: "2006-01-02 03:04 PM",
ThisDayTimeFormat: "",
ThisYearTimeFormat: "",
ShowHeaders: []string{ ShowHeaders: []string{
"From", "To", "Cc", "Bcc", "Subject", "Date", "From", "To", "Cc", "Bcc", "Subject", "Date",
}, },

View File

@ -38,6 +38,8 @@ func (trig *TriggersConfig) ExecNewEmail(account *AccountConfig,
func(part string) (string, error) { func(part string) (string, error) {
formatstr, args, err := format.ParseMessageFormat( formatstr, args, err := format.ParseMessageFormat(
part, conf.Ui.TimestampFormat, part, conf.Ui.TimestampFormat,
conf.Ui.ThisDayTimeFormat,
conf.Ui.ThisYearTimeFormat,
format.Ctx{ format.Ctx{
FromAddress: account.From, FromAddress: account.From,
AccountName: account.Name, AccountName: account.Name,

View File

@ -87,6 +87,18 @@ These options are configured in the *[ui]* section of aerc.conf.
Default: 2006-01-02 03:04 PM (ISO 8601 + 12 hour time) Default: 2006-01-02 03:04 PM (ISO 8601 + 12 hour time)
*today-time-format*
Index-only time format for messages that were received/sent today.
If this is not specified, *timestamp-format* is used instead.
Default: ""
*this-year-time-format*
Index-only time format for messages that were received/sent this year.
If this is not specified, *timestamp-format* is used instead.
Default: ""
*sidebar-width* *sidebar-width*
Width of the sidebar, including the border. Set to zero to disable the Width of the sidebar, including the border. Set to zero to disable the
sidebar. sidebar.

View File

@ -47,8 +47,8 @@ type Ctx struct {
MsgIsMarked bool MsgIsMarked bool
} }
func ParseMessageFormat(format string, timeFmt string, ctx Ctx) (string, func ParseMessageFormat(format string, timeFmt string, thisDayTimeFmt string,
[]interface{}, error) { thisYearTimeFmt string, ctx Ctx) (string, []interface{}, error) {
retval := make([]byte, 0, len(format)) retval := make([]byte, 0, len(format))
var args []interface{} var args []interface{}
@ -144,7 +144,8 @@ func ParseMessageFormat(format string, timeFmt string, ctx Ctx) (string,
} }
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, args = append(args,
dummyIfZeroDate(date.Local(), timeFmt)) dummyIfZeroDate(date.Local(),
timeFmt, thisDayTimeFmt, thisYearTimeFmt))
case 'D': case 'D':
date := envelope.Date date := envelope.Date
if date.IsZero() { if date.IsZero() {
@ -152,7 +153,8 @@ func ParseMessageFormat(format string, timeFmt string, ctx Ctx) (string,
} }
retval = append(retval, 's') retval = append(retval, 's')
args = append(args, args = append(args,
dummyIfZeroDate(date.Local(), timeFmt)) dummyIfZeroDate(date.Local(),
timeFmt, thisDayTimeFmt, thisYearTimeFmt))
case 'f': case 'f':
if envelope == nil { if envelope == nil {
return "", nil, return "", nil,
@ -374,9 +376,20 @@ handle_end_error:
errors.New("reached end of string while parsing message format") errors.New("reached end of string while parsing message format")
} }
func dummyIfZeroDate(date time.Time, format string) string { func dummyIfZeroDate(date time.Time, format string, todayFormat string,
thisYearFormat string) string {
if date.IsZero() { if date.IsZero() {
return strings.Repeat("?", len(format)) return strings.Repeat("?", len(format))
} }
year, month, day := date.Date()
thisYear, thisMonth, thisDay := time.Now().Date()
if year == thisYear {
if month == thisMonth && day == thisDay && todayFormat != "" {
return date.Format(todayFormat)
}
if thisYearFormat != "" {
return date.Format(thisYearFormat)
}
}
return date.Format(format) return date.Format(format)
} }

View File

@ -155,6 +155,7 @@ func (ml *MessageList) Draw(ctx *ui.Context) {
ctx.Fill(0, row, ctx.Width(), 1, ' ', style) ctx.Fill(0, row, ctx.Width(), 1, ' ', style)
fmtStr, args, err := format.ParseMessageFormat( fmtStr, args, err := format.ParseMessageFormat(
uiConfig.IndexFormat, uiConfig.TimestampFormat, uiConfig.IndexFormat, uiConfig.TimestampFormat,
uiConfig.ThisDayTimeFormat, uiConfig.ThisYearTimeFormat,
format.Ctx{ format.Ctx{
FromAddress: ml.aerc.SelectedAccount().acct.From, FromAddress: ml.aerc.SelectedAccount().acct.From,
AccountName: ml.aerc.SelectedAccount().Name(), AccountName: ml.aerc.SelectedAccount().Name(),