Skip to content

Instantly share code, notes, and snippets.

@lucymhdavies
Created January 25, 2018 23:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucymhdavies/31b0c5cf13b681aa67b78f9ea8e45754 to your computer and use it in GitHub Desktop.
Save lucymhdavies/31b0c5cf13b681aa67b78f9ea8e45754 to your computer and use it in GitHub Desktop.
Experimenting with marcusolsson/tui-go as a search/list combobox
package main
import (
"log"
"strings"
"github.com/marcusolsson/tui-go"
)
var (
accountsList = []string{
"team_badger",
"superteam",
"my_first_account",
"hal_spacejock",
"super_secret_facebook_competitor",
"jeff",
}
)
func main() {
list := tui.NewList()
for _, account := range accountsList {
list.AddItems(account)
}
respBody := tui.NewVBox(list)
respBody.SetTitle("Accounts")
respBody.SetBorder(true)
resp := tui.NewVBox(respBody)
resp.SetSizePolicy(tui.Expanding, tui.Preferred)
browser := tui.NewHBox(resp)
browser.SetSizePolicy(tui.Preferred, tui.Expanding)
urlEntry := tui.NewEntry()
urlEntry.SetText("")
urlEntry.OnChanged(func(e *tui.Entry) {
//respBodyLbl.SetText(e.Text())
selected := ""
// TODO: get selected item
if list.Selected() != -1 {
selected = list.SelectedItem()
}
// Clear out list
list.RemoveItems()
itemsAdded := 0
// Re-add items
for _, account := range accountsList {
if strings.Contains(account, e.Text()) {
list.AddItems(account)
itemsAdded = itemsAdded + 1
if account == selected {
list.Select(itemsAdded - 1)
}
}
}
// if we only have one item, select it
if itemsAdded == 1 {
list.Select(0)
}
})
urlEntry.OnSubmit(func(e *tui.Entry) {
// TODO: iff list has a single element, use it
})
urlBox := tui.NewHBox(urlEntry)
urlBox.SetTitle("Search Accounts")
urlBox.SetBorder(true)
tui.DefaultFocusChain.Set(urlEntry, list)
root := tui.NewVBox(urlBox, browser)
// Add a list to this
theme := tui.NewTheme()
theme.SetStyle("box.focused.border", tui.Style{Fg: tui.ColorYellow, Bg: tui.ColorDefault})
normal := tui.Style{Bg: tui.ColorBlack, Fg: tui.ColorWhite}
highlight := tui.Style{Bg: tui.ColorWhite, Fg: tui.ColorBlack}
theme.SetStyle("list.item", normal)
theme.SetStyle("list.item.selected", highlight)
ui, err := tui.New(root)
if err != nil {
log.Fatal(err)
}
ui.SetTheme(theme)
ui.SetKeybinding("Esc", func() { ui.Quit() })
if err := ui.Run(); err != nil {
log.Fatal(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment