chore(textinput): use a getter and setter for width

This commit is contained in:
Christian Rocha
2024-10-31 15:02:05 -04:00
parent 52bd03ed5b
commit 591dfd528e
2 changed files with 23 additions and 13 deletions
+1 -1
View File
@@ -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()
}
+22 -12
View File
@@ -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
@@ -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
@@ -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
@@ -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++
}
}
@@ -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--
}
}
@@ -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))
@@ -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 {