Skip to content

Commit

Permalink
refactor(textarea): Improve setting width
Browse files Browse the repository at this point in the history
When setting the width of the textarea there were some issues
preventing this from working correctly. These problems included:

- If the maximum width needed to be used, the width of the textarea did
  not take into account the prompt and line number width.
- The viewport width did not take into account the style width.

The entire function was confusing to understand and a refactor was
warranted.

As part of this refactor, the bugs mentioned above were fixed and the
code was simplified.

To verify that the logic works as expected, unit tests were expanded to
validate that setting the width works as expected.

Signed-off-by: Michael Lorant <michael.lorant@nine.com.au>
  • Loading branch information
mikelorant committed Apr 6, 2024
1 parent b9e62cb commit cef57bf
Show file tree
Hide file tree
Showing 2 changed files with 609 additions and 19 deletions.
45 changes: 26 additions & 19 deletions textarea/textarea.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (

const (
minHeight = 1
minWidth = 2
defaultHeight = 6
defaultWidth = 40
defaultCharLimit = 400
Expand Down Expand Up @@ -862,32 +861,40 @@ func (m *Model) moveToEnd() {
// It is important that the width of the textarea be exactly the given width
// and no more.
func (m *Model) SetWidth(w int) {
if m.MaxWidth > 0 {
m.viewport.Width = clamp(w, minWidth, m.MaxWidth)
} else {
m.viewport.Width = max(w, minWidth)
// Update prompt width only if there is no prompt function as SetPromptFunc
// updates the prompt width when it is called.
if m.promptFunc == nil {
m.promptWidth = uniseg.StringWidth(m.Prompt)
}

// Since the width of the textarea input is dependent on the width of the
// prompt and line numbers, we need to calculate it by subtracting.
inputWidth := w
if m.ShowLineNumbers {
inputWidth -= uniseg.StringWidth(fmt.Sprintf(m.lineNumberFormat, 0))
}
// Add base style borders and padding to reserved outer width.
reservedOuter := m.style.Base.GetHorizontalFrameSize()

// Account for base style borders and padding.
inputWidth -= m.style.Base.GetHorizontalFrameSize()
// Add prompt width to reserved inner width.
reservedInner := m.promptWidth

if m.promptFunc == nil {
m.promptWidth = uniseg.StringWidth(m.Prompt)
// Add line number width to reserved inner width.
if m.ShowLineNumbers {
const lnWidth = 4 // Up to 3 digits for line number plus 1 margin.
reservedInner += lnWidth
}

inputWidth -= m.promptWidth
// Input width must be at least one more than the reserved inner and outer
// width. This gives us a minimum input width of 1.
minWidth := reservedInner + reservedOuter + 1
inputWidth := max(w, minWidth)

// Input width must be no more than maximum width.
if m.MaxWidth > 0 {
m.width = clamp(inputWidth, minWidth, m.MaxWidth)
} else {
m.width = max(inputWidth, minWidth)
inputWidth = min(inputWidth, m.MaxWidth)
}

// Since the width of the viewport and input area is dependent on the width of
// borders, prompt and line numbers, we need to calculate it by subtracting
// the reserved width from them.

m.viewport.Width = inputWidth - reservedOuter
m.width = inputWidth - reservedOuter - reservedInner
}

// SetPromptFunc supersedes the Prompt field and sets a dynamic prompt
Expand Down
Loading

0 comments on commit cef57bf

Please sign in to comment.