bound textbox input to prevent unbounded memory growth
This commit is contained in:
+9
-2
@@ -7,6 +7,8 @@ import (
|
|||||||
"github.com/nsf/termbox-go"
|
"github.com/nsf/termbox-go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const maxTextboxLen = 2000
|
||||||
|
|
||||||
type Textbox struct {
|
type Textbox struct {
|
||||||
Text string
|
Text string
|
||||||
Fg, Bg Attribute
|
Fg, Bg Attribute
|
||||||
@@ -71,8 +73,10 @@ func (t *Textbox) uiKeyEvent(mod Modifier, key Key) {
|
|||||||
t.Text = ""
|
t.Text = ""
|
||||||
redraw = true
|
redraw = true
|
||||||
case KeySpace:
|
case KeySpace:
|
||||||
t.Text = t.Text + " "
|
if len(t.Text) < maxTextboxLen {
|
||||||
redraw = true
|
t.Text = t.Text + " "
|
||||||
|
redraw = true
|
||||||
|
}
|
||||||
case KeyBackspace:
|
case KeyBackspace:
|
||||||
case KeyBackspace2:
|
case KeyBackspace2:
|
||||||
if len(t.Text) > 0 {
|
if len(t.Text) > 0 {
|
||||||
@@ -88,6 +92,9 @@ func (t *Textbox) uiKeyEvent(mod Modifier, key Key) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *Textbox) uiCharacterEvent(chr rune) {
|
func (t *Textbox) uiCharacterEvent(chr rune) {
|
||||||
|
if len(t.Text) >= maxTextboxLen {
|
||||||
|
return
|
||||||
|
}
|
||||||
t.Text = t.Text + string(chr)
|
t.Text = t.Text + string(chr)
|
||||||
t.uiDraw()
|
t.uiDraw()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user