diff --git a/_examples/autoscroll.go b/_examples/autoscroll.go new file mode 100644 index 00000000..23fd585d --- /dev/null +++ b/_examples/autoscroll.go @@ -0,0 +1,88 @@ +// Copyright 2017 Zack Guo . All rights reserved. +// Use of this source code is governed by a MIT license that can +// be found in the LICENSE file. + +// +build ignore + +package main + +import ( + "fmt" + "log" + "time" + + ui "github.com/gizak/termui/v3" + "github.com/gizak/termui/v3/widgets" +) + +func main() { + if err := ui.Init(); err != nil { + log.Fatalf("failed to initialize termui: %v", err) + } + defer ui.Close() + + l := widgets.NewList() + l.Title = "List" + l.Rows = []string{ + "[0] github.com/gizak/termui/v3", + "[1] [你好,世界](fg:blue)", + "[2] [こんにちは世界](fg:red)", + } + l.TextStyle = ui.NewStyle(ui.ColorYellow) + l.WrapText = false + l.SetRect(0, 0, 25, 8) + + ui.Render(l) + + previousKey := "" + tick := time.NewTicker(1 * time.Second) + uiEvents := ui.PollEvents() + counter := 3 + for { + select { + case <-tick.C: + counter++ + lenRows := len(l.Rows) + l.Rows = append(l.Rows, fmt.Sprintf("[%d] New element", counter)) + + if l.SelectedRow == lenRows-1 { + l.ScrollPageDown() + } + ui.Render(l) + + case e := <-uiEvents: + switch e.ID { + case "q", "": + return + case "j", "": + l.ScrollDown() + case "k", "": + l.ScrollUp() + case "": + l.ScrollHalfPageDown() + case "": + l.ScrollHalfPageUp() + case "": + l.ScrollPageDown() + case "": + l.ScrollPageUp() + case "g": + if previousKey == "g" { + l.ScrollTop() + } + case "": + l.ScrollTop() + case "G", "": + l.ScrollBottom() + } + + if previousKey == "g" { + previousKey = "" + } else { + previousKey = e.ID + } + + ui.Render(l) + } + } +}