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 <xdavidwuph@gmail.com>
Acked-by: Koni Marti <koni.marti@gmail.com>
This commit is contained in:
Pinghao Wu 2022-07-27 20:08:03 +08:00 committed by Robin Jarry
parent 3304ea18ba
commit b8ef8d2628

View file

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