1
0
mirror of https://github.com/gizak/termui.git synced 2025-04-24 13:48:50 +08:00

Merge d5109cf2e0f25fc499ae42922a5abacde7422ea8 into 2b8f0c7960e9553acea6d579a740713066da5e13

This commit is contained in:
Nouamane Tazi 2024-01-30 02:58:06 -08:00 committed by GitHub
commit fc3c214cb1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

88
_examples/autoscroll.go Normal file
View File

@ -0,0 +1,88 @@
// Copyright 2017 Zack Guo <zack.y.guo@gmail.com>. 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", "<C-c>":
return
case "j", "<Down>":
l.ScrollDown()
case "k", "<Up>":
l.ScrollUp()
case "<C-d>":
l.ScrollHalfPageDown()
case "<C-u>":
l.ScrollHalfPageUp()
case "<C-f>":
l.ScrollPageDown()
case "<C-b>":
l.ScrollPageUp()
case "g":
if previousKey == "g" {
l.ScrollTop()
}
case "<Home>":
l.ScrollTop()
case "G", "<End>":
l.ScrollBottom()
}
if previousKey == "g" {
previousKey = ""
} else {
previousKey = e.ID
}
ui.Render(l)
}
}
}