Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add auto-scrolling list example #289

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions _examples/autoscroll.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright 2017 Zack Guo <[email protected]>. 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)
}
}
}