Skip to content

Commit

Permalink
Rework event system
Browse files Browse the repository at this point in the history
* Timers should now be done through Go tickers
* Reworked event names used in Handle()
* Reworked Event type and payloads
  • Loading branch information
cjbassi committed Sep 6, 2018
1 parent 609f0e3 commit b227bd5
Show file tree
Hide file tree
Showing 18 changed files with 307 additions and 373 deletions.
2 changes: 1 addition & 1 deletion _examples/barchart.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {

ui.Render(bc)

ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.Handle("q", "<Insert>", func(ui.Event) {
ui.StopLoop()
})

Expand Down
38 changes: 26 additions & 12 deletions _examples/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main

import (
"math"
"time"

ui "github.com/gizak/termui"
)
Expand All @@ -22,14 +23,21 @@ func main() {
p.TextFgColor = ui.ColorWhite
p.BorderLabel = "Text Box"
p.BorderFg = ui.ColorCyan
p.Handle("/timer/1s", func(e ui.Event) {
cnt := e.Data.(ui.EvtTimer)
if cnt.Count%2 == 0 {
p.TextFgColor = ui.ColorRed
} else {
p.TextFgColor = ui.ColorWhite

pTicker := time.NewTicker(time.Second)
pTickerCount := 1
go func() {
for {
if pTickerCount%2 == 0 {
p.TextFgColor = ui.ColorRed
} else {
p.TextFgColor = ui.ColorWhite
}

pTickerCount++
<-pTicker.C
}
})
}()

listData := []string{"[0] gizak/termui", "[1] editbox.go", "[2] interrupt.go", "[3] keyboard.go", "[4] output.go", "[5] random_out.go", "[6] dashboard.go", "[7] nsf/termbox-go"}

Expand Down Expand Up @@ -136,14 +144,20 @@ func main() {
ui.Render(p, l, g, sls, lc, bc, lc2, p2)
}

ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.Handle("q", func(ui.Event) {
ui.StopLoop()
})

ui.Handle("/timer/1s", func(e ui.Event) {
t := e.Data.(ui.EvtTimer)
draw(int(t.Count))
})
drawTicker := time.NewTicker(time.Second)
drawTickerCount := 1
go func() {
for {
draw(drawTickerCount)

drawTickerCount++
<-drawTicker.C
}
}()

ui.Loop()
}
2 changes: 1 addition & 1 deletion _examples/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func main() {

ui.Render(g0, g1, g2, g3, g4)

ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.Handle("q", func(ui.Event) {
ui.StopLoop()
})

Expand Down
41 changes: 23 additions & 18 deletions _examples/grid.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main

import (
"math"
"time"

ui "github.com/gizak/termui"
)
Expand Down Expand Up @@ -91,29 +92,33 @@ func main() {

ui.Render(ui.Body)

ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.Handle("q", func(ui.Event) {
ui.StopLoop()
})

ui.Handle("/timer/1s", func(e ui.Event) {
t := e.Data.(ui.EvtTimer)
i := t.Count
if i > 103 {
ui.StopLoop()
return
drawTicker := time.NewTicker(time.Second)
drawTickerCount := 1
go func() {
for {
if drawTickerCount > 103 {
ui.StopLoop()
return
}
for _, g := range gs {
g.Percent = (g.Percent + 3) % 100
}
sp.Lines[0].Data = spdata[:100+drawTickerCount]
lc.Data["default"] = sinps[2*drawTickerCount:]
ui.Render(ui.Body)

drawTickerCount++
<-drawTicker.C
}
}()

for _, g := range gs {
g.Percent = (g.Percent + 3) % 100
}

sp.Lines[0].Data = spdata[:100+i]
lc.Data["default"] = sinps[2*i:]
ui.Render(ui.Body)
})

ui.Handle("/sys/wnd/resize", func(e ui.Event) {
ui.Body.Width = ui.TermWidth()
ui.Handle("<Resize>", func(e ui.Event) {
payload := e.Payload.(ui.Resize)
ui.Body.Width = payload.Width
ui.Body.Align()
ui.Clear()
ui.Render(ui.Body)
Expand Down
2 changes: 1 addition & 1 deletion _examples/linechart.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func main() {

ui.Render(lc0, lc1, lc2)

ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.Handle("q", func(ui.Event) {
ui.StopLoop()
})

Expand Down
2 changes: 1 addition & 1 deletion _examples/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func main() {

ui.Render(ls)

ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.Handle("q", func(ui.Event) {
ui.StopLoop()
})

Expand Down
2 changes: 1 addition & 1 deletion _examples/mbarchart.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {

ui.Render(bc)

ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.Handle("q", func(ui.Event) {
ui.StopLoop()
})

Expand Down
2 changes: 1 addition & 1 deletion _examples/par.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func main() {

ui.Render(par0, par1, par2, par3)

ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.Handle("q", func(ui.Event) {
ui.StopLoop()
})

Expand Down
21 changes: 14 additions & 7 deletions _examples/piechart.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,21 @@ func main() {
return fmt.Sprintf("%.02f", v)
}

ui.Handle("/timer/1s", func(e ui.Event) {
if run {
pc.Data, pc.Offset = randomDataAndOffset()
ui.Render(pc)
drawTicker := time.NewTicker(time.Second)
drawTickerCount := 1
go func() {
for {
if run {
pc.Data, pc.Offset = randomDataAndOffset()
ui.Render(pc)
}

drawTickerCount++
<-drawTicker.C
}
})
}()

ui.Handle("/sys/kbd/s", func(ui.Event) {
ui.Handle("s", func(ui.Event) {
run = !run
if run {
pc.BorderLabel = "Pie Chart"
Expand All @@ -54,7 +61,7 @@ func main() {
ui.Render(pc)
})

ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.Handle("q", func(ui.Event) {
ui.StopLoop()
})

Expand Down
2 changes: 1 addition & 1 deletion _examples/sparklines.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func main() {

ui.Render(spls0, spls1, spls2)

ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.Handle("q", func(ui.Event) {
ui.StopLoop()
})

Expand Down
2 changes: 1 addition & 1 deletion _examples/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func main() {

ui.Render(table2)

ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.Handle("q", func(ui.Event) {
ui.StopLoop()
})

Expand Down
6 changes: 3 additions & 3 deletions _examples/tabs.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,17 @@ func main() {

ui.Render(header, tabpane)

ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.Handle("q", func(ui.Event) {
ui.StopLoop()
})

ui.Handle("/sys/kbd/j", func(ui.Event) {
ui.Handle("j", func(ui.Event) {
tabpane.SetActiveLeft()
ui.Clear()
ui.Render(header, tabpane)
})

ui.Handle("/sys/kbd/k", func(ui.Event) {
ui.Handle("k", func(ui.Event) {
tabpane.SetActiveRight()
ui.Clear()
ui.Render(header, tabpane)
Expand Down
17 changes: 12 additions & 5 deletions _examples/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main

import (
"math"
"time"

ui "github.com/gizak/termui"
)
Expand Down Expand Up @@ -134,14 +135,20 @@ func main() {

ui.Render(p, list, g, sp, lc, bc, lc1, p1)

ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.Handle("q", func(ui.Event) {
ui.StopLoop()
})

ui.Handle("/timer/1s", func(e ui.Event) {
t := e.Data.(ui.EvtTimer)
draw(int(t.Count))
})
drawTicker := time.NewTicker(time.Second)
drawTickerCount := 1
go func() {
for {
draw(drawTickerCount)

drawTickerCount++
<-drawTicker.C
}
}()

ui.Loop()
}
42 changes: 25 additions & 17 deletions _examples/ttop.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"sort"
"strconv"
"strings"
"time"

ui "github.com/gizak/termui"
"github.com/gizak/termui/extra"
Expand Down Expand Up @@ -330,35 +331,42 @@ func main() {

ui.Render(header, tabpane)

ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.Handle("q", func(ui.Event) {
ui.StopLoop()
})

ui.Handle("/sys/kbd/j", func(ui.Event) {
ui.Handle("j", func(ui.Event) {
tabpane.SetActiveLeft()
ui.Render(header, tabpane)
})

ui.Handle("/sys/kbd/k", func(ui.Event) {
ui.Handle("k", func(ui.Event) {
tabpane.SetActiveRight()
ui.Render(header, tabpane)
})

ui.Handle("/timer/1s", func(e ui.Event) {
cs, errcs := getCpusStatsMap()
if errcs != nil {
panic(errcs)
drawTicker := time.NewTicker(time.Second)
drawTickerCount := 1
go func() {
for {
cs, errcs := getCpusStatsMap()
if errcs != nil {
panic(errcs)
}
cpusStats.tick(cs)
cpuTabElems.Update(*cpusStats)

ms, errm := getMemStats()
if errm != nil {
panic(errm)
}
memTabElems.Update(ms)
ui.Render(header, tabpane)

drawTickerCount++
<-drawTicker.C
}
cpusStats.tick(cs)
cpuTabElems.Update(*cpusStats)

ms, errm := getMemStats()
if errm != nil {
panic(errm)
}
memTabElems.Update(ms)
ui.Render(header, tabpane)
})
}()

ui.Loop()
}
2 changes: 1 addition & 1 deletion _examples/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func main() {

ui.Render(p)

ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.Handle("q", func(ui.Event) {
ui.StopLoop()
})

Expand Down
Loading

0 comments on commit b227bd5

Please sign in to comment.