Fix panic when pasting into a textinput with no char limit

This commit is contained in:
Christian Rocha 2020-10-24 18:15:44 -04:00 committed by Christian Rocha
parent 4445acbace
commit 03461d6804

View File

@ -150,7 +150,10 @@ func (m *Model) Paste() {
} }
paste := []rune(pasteString) paste := []rune(pasteString)
availSpace := m.CharLimit - len(m.value) var availSpace int
if m.CharLimit > 0 {
availSpace = m.CharLimit - len(m.value)
}
// If the char limit's been reached cancel // If the char limit's been reached cancel
if m.CharLimit > 0 && availSpace <= 0 { if m.CharLimit > 0 && availSpace <= 0 {
@ -159,7 +162,7 @@ func (m *Model) Paste() {
// If there's not enough space to paste the whole thing cut the pasted // If there's not enough space to paste the whole thing cut the pasted
// runes down so they'll fit // runes down so they'll fit
if availSpace < len(paste) { if m.CharLimit > 0 && availSpace < len(paste) {
paste = paste[:len(paste)-availSpace] paste = paste[:len(paste)-availSpace]
} }
@ -172,10 +175,12 @@ func (m *Model) Paste() {
// Insert pasted runes // Insert pasted runes
for _, r := range paste { for _, r := range paste {
head = append(head, r) head = append(head, r)
availSpace--
m.SetCursor(m.pos + 1) m.SetCursor(m.pos + 1)
if m.CharLimit > 0 && availSpace <= 0 { if m.CharLimit > 0 {
break availSpace--
if availSpace <= 0 {
break
}
} }
} }