diff --git a/textarea/textarea.go b/textarea/textarea.go index d29c671a..48f00d30 100644 --- a/textarea/textarea.go +++ b/textarea/textarea.go @@ -4,6 +4,7 @@ import ( "crypto/sha256" "fmt" "image/color" + "math" "strconv" "strings" "time" @@ -988,6 +989,9 @@ func (m *Model) SetWidth(w int) { if m.promptFunc == nil { // XXX: This should account for a styled prompt and use lipglosss.Width // instead of uniseg.StringWidth. + // + // XXX: Do we even need this or can we calculate the prompt width + // at render time? m.promptWidth = uniseg.StringWidth(m.Prompt) } @@ -999,9 +1003,10 @@ func (m *Model) SetWidth(w int) { // Add line number width to reserved inner width. if m.ShowLineNumbers { - // XXX: this should almost certainly not be hardcoded. - const lnWidth = 4 // Up to 3 digits for line number plus 1 margin. - reservedInner += lnWidth + const gap = 1 + + // Number of digits plus 1 cell for the margin. + reservedInner += numDigits(m.MaxWidth) + gap } // Input width must be at least one more than the reserved inner and outer @@ -1610,6 +1615,11 @@ func repeatSpaces(n int) []rune { return []rune(strings.Repeat(string(' '), n)) } +// numDigits returns the number of digits in an integer. +func numDigits(n int) int { + return int(math.Floor(math.Log10(float64(n)) + 1)) +} + func clamp(v, low, high int) int { if high < low { low, high = high, low