From 0d290826bed0c59034cf26b65b718d6fa54eaa81 Mon Sep 17 00:00:00 2001 From: Nouamane Tazi Date: Wed, 27 Jan 2021 23:15:31 +0100 Subject: [PATCH 1/2] Add auto scrolling list example --- _examples/autoscroll.go | 82 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 _examples/autoscroll.go diff --git a/_examples/autoscroll.go b/_examples/autoscroll.go new file mode 100644 index 00000000..7dc1c11a --- /dev/null +++ b/_examples/autoscroll.go @@ -0,0 +1,82 @@ +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) + } + } +} From d5109cf2e0f25fc499ae42922a5abacde7422ea8 Mon Sep 17 00:00:00 2001 From: Nouamane Tazi Date: Wed, 27 Jan 2021 23:17:44 +0100 Subject: [PATCH 2/2] Add copyright comment to the example autoscroll.go --- _examples/autoscroll.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/_examples/autoscroll.go b/_examples/autoscroll.go index 7dc1c11a..23fd585d 100644 --- a/_examples/autoscroll.go +++ b/_examples/autoscroll.go @@ -1,3 +1,9 @@ +// 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 (