[GH-ISSUE #888] Change text in input field depending on what has been typed in #648

Closed
opened 2026-03-04 01:06:44 +03:00 by kerem · 1 comment
Owner

Originally created by @paololazzari on GitHub (Sep 25, 2023).
Original GitHub issue: https://github.com/rivo/tview/issues/888

I have a tview application which is using both InputField and TextView.
The text shown in the TextView depends on what I type in the InputField, and to do this I am using SetChangedFunc and QueueUpdateDraw. This works well.

package main

import (
	"github.com/gdamore/tcell/v2"
	"github.com/rivo/tview"
)

func main() {
	app := tview.NewApplication()
	input := tview.NewInputField().
		SetFieldBackgroundColor(tcell.ColorDefault)
	view := tview.NewTextView().
		SetDynamicColors(true)
	grid := tview.NewGrid().SetRows(1, 0).
		AddItem(input, 0, 0, 1, 1, 0, 0, true).
		AddItem(view, 1, 0, 1, 1, 0, 0, true)
	app.SetRoot(grid, true).SetFocus(input)

	input.SetChangedFunc(func(text string) {
		go app.QueueUpdateDraw(func() {
			view.SetText(text)
		})
	})

	if err := app.Run(); err != nil {
		panic(err)
	}
}

Now I am looking to change the functionality of the InputField such that if I type foo bar, then bar is removed, resulting in the InputField then having only foo.
What's the best way to achieve this?

Originally created by @paololazzari on GitHub (Sep 25, 2023). Original GitHub issue: https://github.com/rivo/tview/issues/888 I have a `tview` application which is using both `InputField` and `TextView`. The text shown in the TextView depends on what I type in the InputField, and to do this I am using `SetChangedFunc` and `QueueUpdateDraw`. This works well. ```go package main import ( "github.com/gdamore/tcell/v2" "github.com/rivo/tview" ) func main() { app := tview.NewApplication() input := tview.NewInputField(). SetFieldBackgroundColor(tcell.ColorDefault) view := tview.NewTextView(). SetDynamicColors(true) grid := tview.NewGrid().SetRows(1, 0). AddItem(input, 0, 0, 1, 1, 0, 0, true). AddItem(view, 1, 0, 1, 1, 0, 0, true) app.SetRoot(grid, true).SetFocus(input) input.SetChangedFunc(func(text string) { go app.QueueUpdateDraw(func() { view.SetText(text) }) }) if err := app.Run(); err != nil { panic(err) } } ``` Now I am looking to change the functionality of the InputField such that if I type `foo bar`, then `bar` is removed, resulting in the InputField then having only `foo`. What's the best way to achieve this?
kerem closed this issue 2026-03-04 01:06:45 +03:00
Author
Owner

@paololazzari commented on GitHub (Sep 26, 2023):

Brilliant, thank you.

I noticed that this method is present:

func (i *InputField) SetAcceptanceFunc(handler func(textToCheck string, lastChar rune) bool) *InputField

I suppose this method could also be used right?

<!-- gh-comment-id:1734978730 --> @paololazzari commented on GitHub (Sep 26, 2023): Brilliant, thank you. I noticed that this method is present: ``` func (i *InputField) SetAcceptanceFunc(handler func(textToCheck string, lastChar rune) bool) *InputField ``` I suppose this method could also be used right?
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#648
No description provided.