@ -36,6 +36,9 @@ type InputField struct {
@@ -36,6 +36,9 @@ type InputField struct {
// An optional function which may reject the last character that was entered.
accept func ( text string , ch rune ) bool
// An optional function which is called when the input has changed.
changed func ( text string )
// An optional function which is called when the user indicated that they
// are done entering text. The key which was pressed is provided (tab,
// shift-tab, enter, or escape).
@ -55,6 +58,9 @@ func NewInputField() *InputField {
@@ -55,6 +58,9 @@ func NewInputField() *InputField {
// SetText sets the current text of the input field.
func ( i * InputField ) SetText ( text string ) * InputField {
i . text = text
if i . changed != nil {
i . changed ( text )
}
return i
}
@ -119,6 +125,13 @@ func (i *InputField) SetAcceptanceFunc(handler func(textToCheck string, lastChar
@@ -119,6 +125,13 @@ func (i *InputField) SetAcceptanceFunc(handler func(textToCheck string, lastChar
return i
}
// SetChangedFunc sets a handler which is called whenever the text of the input
// field has changed. It receives the current text (after the change).
func ( i * InputField ) SetChangedFunc ( handler func ( text string ) ) * InputField {
i . changed = handler
return i
}
// SetDoneFunc sets a handler which is called when the user is done entering
// text. The callback function is provided with the key that was pressed, which
// is one of the following:
@ -202,6 +215,14 @@ func (i *InputField) setCursor(screen tcell.Screen) {
@@ -202,6 +215,14 @@ func (i *InputField) setCursor(screen tcell.Screen) {
// InputHandler returns the handler for this primitive.
func ( i * InputField ) InputHandler ( ) func ( event * tcell . EventKey , setFocus func ( p Primitive ) ) {
return func ( event * tcell . EventKey , setFocus func ( p Primitive ) ) {
// Trigger changed events.
currentText := i . text
defer func ( ) {
if i . text != currentText && i . changed != nil {
i . changed ( i . text )
}
} ( )
// Process key event.
switch key := event . Key ( ) ; key {
case tcell . KeyRune : // Regular character.