1
0
mirror of https://github.com/rivo/tview.git synced 2025-04-24 13:48:56 +08:00

Merge db47442382c855c565ecc0fb460c9c137e83ed10 into c76f7879f592d17e9e68a1d795a85faae6cb7414

This commit is contained in:
Eze Olea Figueroa 2024-11-30 23:43:01 +00:00 committed by GitHub
commit 4c888e2bec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 118 additions and 1 deletions

View File

@ -84,6 +84,7 @@ For a presentation highlighting this package, compile and run the program found
- [The personal information dashboard for your terminal. ](https://github.com/wtfutil/wtf)
- [MySQL database to Golang struct](https://github.com/xxjwxc/gormt)
- [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)
- [Browse your AWS ECS Clusters in the Terminal](https://github.com/swartzrock/ecsview)

View File

@ -15,6 +15,9 @@ func main() {
AddItem("Quit", "Press to exit", 'q', func() {
app.Stop()
})
list.UpdateItem(1, "List updated item 2", "Some explanatory text", 'b', nil)
list.UpdateItemMainText(2, "List item main text updated 3")
if err := app.SetRoot(list, true).EnableMouse(true).Run(); err != nil {
panic(err)
}

View File

@ -0,0 +1 @@
![Screenshot](screenshot.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

22
demos/searchModal/main.go Normal file
View File

@ -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)
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

74
list.go
View File

@ -359,6 +359,80 @@ func (l *List) AddItem(mainText, secondaryText string, shortcut rune, selected f
return l
}
// UpdateItem updates an item from the list at the specified index. An index of 0
// will update the item at the beginning, an index of 1 before the second item,
// and so on. Using an index that is out of range will return the unmodified list
//
// An item has a main text which will be highlighted when selected. It also has
// a secondary text which is shown underneath the main text (if it is set to
// visible) but which may remain empty.
//
// The shortcut is a key binding. If the specified rune is entered, the item
// is selected immediately. Set to 0 for no binding.
//
// The "selected" callback will be invoked when the user selects the item. You
// may provide nil if no such callback is needed or if all events are handled
// through the selected callback set with SetSelectedFunc().
//
// The currently selected item will shift its position accordingly. If the list
// was previously empty, a "changed" event is fired because the new item becomes
// selected.
func (l *List) UpdateItem(index int, mainText, secondaryText string, shortcut rune, selected func()) *List {
if l.GetItemCount() < index || index < 0 {
return l
}
element := l.items[index]
element.MainText = mainText
element.SecondaryText = secondaryText
element.Shortcut = shortcut
element.Selected = selected
return l
}
// UpdateItemMainText updates the main text of an item from the list at the specified index.
// An index of 0 will update the item at the beginning, an index of 1 before the second item,
// and so on. Using an index that is out of range will return the unmodified list
func (l *List) UpdateItemMainText(index int, mainText string) *List {
if l.GetItemCount() < index || index < 0 {
return l
}
element := l.items[index]
element.MainText = mainText
return l
}
// UpdateItemSecondaryText updates the secondary text of an item from the list at the specified index.
// An index of 0 will update the item at the beginning, an index of 1 before the second item,
// and so on. Using an index that is out of range will return the unmodified list
func (l *List) UpdateItemSecondaryText(index int, secondaryText string) *List {
if l.GetItemCount() < index || index < 0 {
return l
}
element := l.items[index]
element.SecondaryText = secondaryText
return l
}
// UpdateItemSelectedFunc updates the selected function of an item from the list at the specified index.
// An index of 0 will update the item at the beginning, an index of 1 before the second item,
// and so on. Using an index that is out of range will return the unmodified list
func (l *List) UpdateItemSelectedFunc(index int, selected func()) *List {
if l.GetItemCount() < index || index < 0 {
return l
}
element := l.items[index]
element.Selected = selected
return l
}
// InsertItem adds a new item to the list at the specified index. An index of 0
// will insert the item at the beginning, an index of 1 before the second item,
// and so on. An index of [List.GetItemCount] or higher will insert the item at

View File

@ -105,6 +105,17 @@ func (m *Modal) SetText(text string) *Modal {
return m
}
/// AddInputText 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) 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 {
@ -174,8 +185,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