Return early from WithPadding when there is nothing to pad to

A caller that asks for a padding of zero gets its string back unchanged,
but only after WithPadding has measured it, and measuring means a
Decolorise lookup and a width scan over the result. A later commit pads
four cells of every commit in the list, and asks for a padding of zero
for all four whenever no rebase or bisect is in progress and the date
column is hidden, so make that case cost nothing.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Stefan Haller
2026-09-19 10:16:04 +02:00
co-authored by Claude Opus 5
parent 53e9c73034
commit 43e63bcc24
+6
View File
@@ -36,6 +36,12 @@ func StringWidth(s string) int {
// WithPadding pads a string as much as you want
func WithPadding(str string, padding int, alignment Alignment) string {
if padding <= 0 {
// Nothing to pad to, and measuring the string isn't free: Decolorise
// compiles a regex whenever it is called with a string it hasn't cached.
return str
}
uncoloredStr := Decolorise(str)
width := StringWidth(uncoloredStr)
if padding < width {