Skip to content

Commit

Permalink
fix(textarea): don't hardcode line number gutter width
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Jan 30, 2025
1 parent 60728a7 commit 8c601be
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions textarea/textarea.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/sha256"
"fmt"
"image/color"
"math"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -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)
}

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8c601be

Please sign in to comment.