Skip to content

Instantly share code, notes, and snippets.

@deinspanjer
Created March 13, 2018 17:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deinspanjer/ebcd96dc7bee70226b1ef5a2250f1417 to your computer and use it in GitHub Desktop.
Save deinspanjer/ebcd96dc7bee70226b1ef5a2250f1417 to your computer and use it in GitHub Desktop.
A toy termui example to get the basic structure down
package main
import (
ui "github.com/gizak/termui" // use ui as an alias
"strconv"
"fmt"
"math/rand"
"container/ring"
)
func main() {
err := ui.Init()
if err != nil {
panic(err)
}
defer ui.Close()
p := ui.NewPar(":PRESS q TO QUIT DEMO, <space> to increment")
p.TextFgColor = ui.ColorWhite
p.BorderLabel = "Text Box"
p.BorderFg = ui.ColorCyan
p.Height = 3
sCS := ui.Sparkline{}
sCS.Height = 1
sCS.Title = "Test"
sCS.Data = make([]int,10,10)
sCE := ui.Sparkline{}
sCE.Height = 1
sCE.Title = "Test"
sCE.Data = make([]int,10,10)
s := ui.NewSparklines(sCS,sCE)
s.Height = 10
s.BorderLabel = "Events"
log := ui.NewPar("Starting...")
log.TextFgColor = ui.ColorWhite
log.BorderLabel = "Text Box"
log.BorderFg = ui.ColorCyan
log.Height = 5
headers := [][]string{
[]string{"Count", "sCS Value", "sCE Value"},
[]string{"","",""},
[]string{"","",""},
[]string{"","",""},
[]string{"","",""},
[]string{"","",""},
}
rows := ring.New(5)
// Initialize the ring with some integer values
for i := 0; i < 5; i++ {
rows.Value = []string{"","",""}
rows = rows.Next()
}
t := ui.NewTable()
t.Rows = headers
t.FgColor = ui.ColorWhite
t.BgColor = ui.ColorDefault
t.Height = 13
ui.Body.AddRows(
ui.NewRow(
ui.NewCol(3,0, p)),
ui.NewRow(
ui.NewCol(9,0, s)),
ui.NewRow(
ui.NewCol(9,0, t)),
ui.NewRow(
ui.NewCol(9,0, log)))
ui.Body.Align()
ui.Render(ui.Body) // feel free to call Render, it's async and non-block
ui.Handle("/sys/kbd/q",func(e ui.Event){
ui.StopLoop()
})
cnt := 0
ui.Handle("/sys/kbd/<space>",func(e ui.Event){
cnt += 1
s.Lines[0].Data = append(s.Lines[0].Data[1:], rand.Intn(100))
s.Lines[1].Data = append(s.Lines[1].Data[1:], rand.Intn(100))
rows.Value = []string{strconv.Itoa(cnt),fmt.Sprintf("%v",s.Lines[0].Data),fmt.Sprintf("%v",s.Lines[1].Data)}
rows = rows.Move(1)
i := 1
rows.Do(func(r interface{}) {
t.Rows[i] = r.([]string)
i += 1
})
ui.Render(ui.Body)
})
ui.Loop()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment