[GH-ISSUE #180] Panic if you type anything else after calling InputField.SetText("") #144

Closed
opened 2026-03-04 01:02:21 +03:00 by kerem · 2 comments
Owner

Originally created by @jonathanpittman on GitHub (Nov 7, 2018).
Original GitHub issue: https://github.com/rivo/tview/issues/180

I may simply be doing this wrong. I am using an InputField to implement a command prompt of sorts (e.g. IRC command and message entry). In the InputField's DoneFunc, I am doing this...

case tcell.KeyEnter:
	defer cmdLine.SetText("")

So that regardless of what happens next with the value of the existing text, enter will "clear" the text of the input field to be ready for a new command. This causes a panic as soon as you begin to type some other character after SetText("").

https://github.com/rivo/tview/blob/master/inputfield.go#L365
newText := i.text[:i.cursorPos] + string(r) + i.text[i.cursorPos:]

If I remove the SetText(""), then no panic occurs, but the text remains in the input field. I can hold the delete/backspace key to clear the text and type something else in its place without panic.
Is there a better approach to what I am doing?

Originally created by @jonathanpittman on GitHub (Nov 7, 2018). Original GitHub issue: https://github.com/rivo/tview/issues/180 I may simply be doing this wrong. I am using an InputField to implement a command prompt of sorts (e.g. IRC command and message entry). In the InputField's DoneFunc, I am doing this... ``` case tcell.KeyEnter: defer cmdLine.SetText("") ``` So that regardless of what happens next with the value of the existing text, enter will "clear" the text of the input field to be ready for a new command. This causes a panic as soon as you begin to type some other character after SetText(""). https://github.com/rivo/tview/blob/master/inputfield.go#L365 `newText := i.text[:i.cursorPos] + string(r) + i.text[i.cursorPos:]` If I remove the SetText(""), then no panic occurs, but the text remains in the input field. I can hold the delete/backspace key to clear the text and type something else in its place without panic. Is there a better approach to what I am doing?
kerem closed this issue 2026-03-04 01:02:21 +03:00
Author
Owner

@rivo commented on GitHub (Nov 10, 2018):

Thanks for spotting this. The cursor position must of course be adjusted after calling SetText(), too. I just fixed this. Let me know if it works for you now.

<!-- gh-comment-id:437567333 --> @rivo commented on GitHub (Nov 10, 2018): Thanks for spotting this. The cursor position must of course be adjusted after calling `SetText()`, too. I just fixed this. Let me know if it works for you now.
Author
Owner

@jonathanpittman commented on GitHub (Nov 14, 2018):

It does work. Thanks!

<!-- gh-comment-id:438770015 --> @jonathanpittman commented on GitHub (Nov 14, 2018): It does work. Thanks!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/tview#144
No description provided.