From b8ef8d2628e2b307864aee1f61ab4036d0473b33 Mon Sep 17 00:00:00 2001 From: Pinghao Wu Date: Wed, 27 Jul 2022 20:08:03 +0800 Subject: [PATCH] index: workaround for wide character printing This is a workaround for https://todo.sr.ht/~rjarry/aerc/22, by injecting zero-width spaces in front of wide characters to make up width calculation in fmt.Sprintf. With this applied, columns will still be misaligned a little only if they are left with trailing zero-width spaces when cut. It is better than a large offset caused by each wide characters in the column. References: https://todo.sr.ht/~rjarry/aerc/22 Signed-off-by: Pinghao Wu Acked-by: Koni Marti --- lib/format/format.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/format/format.go b/lib/format/format.go index ca90ac4..cc9716d 100644 --- a/lib/format/format.go +++ b/lib/format/format.go @@ -10,6 +10,7 @@ import ( "git.sr.ht/~rjarry/aerc/models" "github.com/emersion/go-message/mail" + "github.com/mattn/go-runewidth" ) // AddressForHumans formats the address. If the address's name @@ -365,6 +366,22 @@ func ParseMessageFormat(format string, timeFmt string, thisDayTimeFmt string, i = ni + 1 } + const zeroWidthSpace rune = '\u200b' + for i, val := range args { + if s, ok := val.(string); ok { + var out strings.Builder + for _, r := range s { + w := runewidth.RuneWidth(r) + for w > 1 { + out.WriteRune(zeroWidthSpace) + w -= 1 + } + out.WriteRune(r) + } + args[i] = out.String() + } + } + return string(retval), args, nil handle_end_error: