termui/_examples/list.go

80 lines
1.4 KiB
Go
Raw Normal View History

2017-01-13 23:07:43 -07:00
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. All rights reserved.
2015-03-20 14:21:50 -06:00
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
// +build ignore
package main
2019-01-23 21:12:10 -07:00
import (
"log"
ui "github.com/gizak/termui"
"github.com/gizak/termui/widgets"
)
func main() {
2019-01-23 21:12:10 -07:00
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
2018-09-06 15:48:09 -06:00
defer ui.Close()
2019-01-23 21:12:10 -07:00
l := widgets.NewList()
l.Title = "List"
l.Rows = []string{
"[0] github.com/gizak/termui",
2019-01-23 21:12:10 -07:00
"[1] [你好,世界](fg:blue)",
"[2] [こんにちは世界](fg:red)",
2019-01-23 22:44:00 -07:00
"[3] [color](fg:white,bg:green) output",
"[4] output.go",
"[5] random_out.go",
"[6] dashboard.go",
"[7] foo",
"[8] bar",
"[9] baz",
2019-01-23 21:12:10 -07:00
}
l.TextStyle = ui.NewStyle(ui.ColorYellow)
2019-01-26 23:32:08 -07:00
l.WrapText = false
l.SetRect(0, 0, 25, 8)
2019-01-23 21:12:10 -07:00
ui.Render(l)
2018-09-06 15:48:09 -06:00
previousKey := ""
uiEvents := ui.PollEvents()
2018-11-28 19:19:34 -07:00
for {
e := <-uiEvents
2018-11-28 19:19:34 -07:00
switch e.ID {
case "q", "<C-c>":
return
2019-01-26 23:32:08 -07:00
case "j", "<Down>":
l.ScrollDown()
case "k", "<Up>":
l.ScrollUp()
case "<C-d>":
l.HalfPageDown()
case "<C-u>":
l.HalfPageUp()
case "<C-f>":
l.PageDown()
case "<C-b>":
l.PageUp()
case "g":
if previousKey == "g" {
l.ScrollTop()
}
case "<Home>":
l.ScrollTop()
case "G", "<End>":
l.ScrollBottom()
2018-11-28 19:19:34 -07:00
}
if previousKey == "g" {
previousKey = ""
} else {
previousKey = e.ID
}
ui.Render(l)
2018-11-28 19:19:34 -07:00
}
}