diff --git a/README.md b/README.md index 56e05228..13e28cfe 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,7 @@ For a presentation highlighting this package, compile and run the program found - [MySQL database to Golang struct](https://github.com/xxjwxc/gormt) - [Cryptowatch Go SDK](https://github.com/y3sh/cw-sdk-go) - [Discord, TUI and SIXEL.](https://gitlab.com/diamondburned/6cord) +- [A terminal based blogs reader](https://github.com/ezeoleaf/tblogs) - [A CLI Audio Player](https://www.github.com/dhulihan/grump) - [GLab, a GitLab CLI tool](https://gitlab.com/profclems/glab) diff --git a/demos/searchModal/README.md b/demos/searchModal/README.md new file mode 100644 index 00000000..4a14e6c1 --- /dev/null +++ b/demos/searchModal/README.md @@ -0,0 +1 @@ +![Screenshot](screenshot.png) diff --git a/demos/searchModal/centered.png b/demos/searchModal/centered.png new file mode 100644 index 00000000..7511a05e Binary files /dev/null and b/demos/searchModal/centered.png differ diff --git a/demos/searchModal/main.go b/demos/searchModal/main.go new file mode 100644 index 00000000..344a6127 --- /dev/null +++ b/demos/searchModal/main.go @@ -0,0 +1,22 @@ +// Demo code for the Modal primitive. +package main + +import ( + "github.com/rivo/tview" +) + +func main() { + app := tview.NewApplication() + modal := tview.NewModal(). + SetText("Search a list"). + AddInputText([]string{"Input text:"}). + AddButtons([]string{"Search", "Clear"}). + SetDoneFunc(func(buttonIndex int, buttonLabel string) { + if buttonLabel == "Clear" { + app.Stop() + } + }) + if err := app.SetRoot(modal, false).EnableMouse(true).Run(); err != nil { + panic(err) + } +} diff --git a/demos/searchModal/screenshot.png b/demos/searchModal/screenshot.png new file mode 100644 index 00000000..dcb7c8eb Binary files /dev/null and b/demos/searchModal/screenshot.png differ diff --git a/modal.go b/modal.go index 4e9aa5ce..1b8759cb 100644 --- a/modal.go +++ b/modal.go @@ -94,6 +94,17 @@ func (m *Modal) SetText(text string) *Modal { return m } +// AddInputText adds Input Fields to the window, which are later on identified by their labels. +// a "done" handler so the window can be closed again. +func (m *Modal) AddInputText(labels []string) *Modal { + for index, label := range labels { + func(i int, l string) { + m.form.AddInputField(label, "", 20, nil, nil) + }(index, label) + } + return m +} + // AddButtons adds buttons to the window. There must be at least one button and // a "done" handler so the window can be closed again. func (m *Modal) AddButtons(labels []string) *Modal { @@ -163,8 +174,13 @@ func (m *Modal) Draw(screen tcell.Screen) { m.frame.AddText(line, true, AlignCenter, m.textColor) } + lengthForm := 0 + if len(m.form.items) > 0 { + lengthForm += len(m.form.items) + 1 + } + // Set the modal's position and size. - height := len(lines) + 6 + height := len(lines) + 6 + lengthForm width += 4 x := (screenWidth - width) / 2 y := (screenHeight - height) / 2