Skip to content

Commit

Permalink
chore(textinput): use a getter and setter for width
Browse files Browse the repository at this point in the history
  • Loading branch information
meowgorithm committed Oct 31, 2024
1 parent 52bd03e commit 591dfd5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -691,7 +691,7 @@ func (m *Model) setSize(width, height int) {
m.width = width
m.height = height
m.Help.Width = width
m.FilterInput.Width = width - promptWidth - lipgloss.Width(m.spinnerView())
m.FilterInput.SetWidth(width - promptWidth - lipgloss.Width(m.spinnerView()))
m.updatePagination()
}

Expand Down
34 changes: 22 additions & 12 deletions textinput/textinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ type Model struct {
// Width is the maximum number of characters that can be displayed at once.
// It essentially treats the text field like a horizontally scrolling
// viewport. If 0 or less this setting is ignored.
Width int
width int

// KeyMap encodes the keybindings recognized by the widget.
KeyMap KeyMap
Expand Down Expand Up @@ -168,6 +168,16 @@ func New() Model {
}
}

// SetWidth sets the width of the text input.
func (m Model) Width() int {
return m.width
}

// SetWidth sets the width of the text input.
func (m *Model) SetWidth(w int) {
m.width = w
}

// SetValue sets the value of the text input.
func (m *Model) SetValue(s string) {
// Clean up any special characters in the input provided by the
Expand Down Expand Up @@ -315,7 +325,7 @@ func (m *Model) insertRunesFromUserInput(v []rune) {
// If a max width is defined, perform some logic to treat the visible area
// as a horizontally scrolling viewport.
func (m *Model) handleOverflow() {
if m.Width <= 0 || uniseg.StringWidth(string(m.value)) <= m.Width {
if m.Width() <= 0 || uniseg.StringWidth(string(m.value)) <= m.Width() {
m.offset = 0
m.offsetRight = len(m.value)
return
Expand All @@ -331,9 +341,9 @@ func (m *Model) handleOverflow() {
i := 0
runes := m.value[m.offset:]

for i < len(runes) && w <= m.Width {
for i < len(runes) && w <= m.Width() {
w += rw.RuneWidth(runes[i])
if w <= m.Width+1 {
if w <= m.Width()+1 {
i++
}
}
Expand All @@ -346,9 +356,9 @@ func (m *Model) handleOverflow() {
runes := m.value[:m.offsetRight]
i := len(runes) - 1

for i > 0 && w < m.Width {
for i > 0 && w < m.Width() {
w += rw.RuneWidth(runes[i])
if w <= m.Width {
if w <= m.Width() {
i--
}
}
Expand Down Expand Up @@ -678,9 +688,9 @@ func (m Model) View() string {
// If a max width and background color were set fill the empty spaces with
// the background color.
valWidth := uniseg.StringWidth(string(value))
if m.Width > 0 && valWidth <= m.Width {
padding := max(0, m.Width-valWidth)
if valWidth+padding <= m.Width && pos < len(value) {
if m.Width() > 0 && valWidth <= m.Width() {
padding := max(0, m.Width()-valWidth)
if valWidth+padding <= m.Width() && pos < len(value) {
padding++
}
v += styleText(strings.Repeat(" ", padding))
Expand All @@ -702,15 +712,15 @@ func (m Model) placeholderView() string {
v += m.Cursor.View()

// If the entire placeholder is already set and no padding is needed, finish
if m.Width < 1 && len(p) <= 1 {
if m.Width() < 1 && len(p) <= 1 {
return m.PromptStyle.Render(m.Prompt) + v
}

// If Width is set then size placeholder accordingly
if m.Width > 0 {
if m.Width() > 0 {
// available width is width - len + cursor offset of 1
minWidth := lipgloss.Width(m.Placeholder)
availWidth := m.Width - minWidth + 1
availWidth := m.Width() - minWidth + 1

// if width < len, 'subtract'(add) number to len and dont add padding
if availWidth < 0 {
Expand Down

0 comments on commit 591dfd5

Please sign in to comment.