kevinschoon-pomo/ui.go

98 lines
1.9 KiB
Go
Raw Normal View History

2018-01-26 16:07:38 +01:00
package main
import (
"fmt"
"github.com/gizak/termui"
)
2018-01-27 16:58:56 +01:00
func status(runner *TaskRunner) termui.GridBufferer {
var text string
2018-01-26 16:07:38 +01:00
switch runner.state {
case RUNNING:
2018-01-27 16:58:56 +01:00
text = fmt.Sprintf(
2018-01-26 16:07:38 +01:00
"%s %s remaining [ pomodoro %d/%d ]",
"X",
runner.TimeRemaining(),
runner.count,
2018-01-27 16:58:56 +01:00
runner.nPomodoros,
2018-01-26 16:07:38 +01:00
)
case BREAKING:
2018-01-27 16:58:56 +01:00
text = "Time to take a break.\nPress [enter] to begin the next Pomodoro!"
2018-01-26 16:07:38 +01:00
case PAUSED:
2018-01-27 16:58:56 +01:00
text = "Press p to resume"
2018-01-26 16:07:38 +01:00
case COMPLETE:
2018-01-27 16:58:56 +01:00
text = "Press q to quit"
2018-01-26 16:07:38 +01:00
}
2018-01-27 16:58:56 +01:00
par := termui.NewPar(text)
par.Height = 10
2018-01-26 16:07:38 +01:00
par.BorderLabel = fmt.Sprintf("Pomo - %s", runner.state)
par.BorderLabelFg = termui.ColorWhite
par.BorderFg = termui.ColorRed
if runner.state == RUNNING {
par.BorderFg = termui.ColorGreen
}
return par
}
2018-01-27 16:58:56 +01:00
func newBlk() termui.GridBufferer {
blk := termui.NewBlock()
blk.Height = termui.TermHeight() / 3
blk.Border = false
return blk
}
func centered(part termui.GridBufferer) *termui.Grid {
grid := termui.NewGrid(
termui.NewRow(
termui.NewCol(12, 0, newBlk()),
),
termui.NewRow(
termui.NewCol(3, 0, newBlk()),
termui.NewCol(6, 0, part),
termui.NewCol(3, 0, newBlk()),
),
termui.NewRow(
termui.NewCol(12, 0, newBlk()),
),
)
grid.BgColor = termui.ThemeAttr("bg")
grid.Width = termui.TermWidth()
grid.Align()
return grid
}
2018-01-26 16:07:38 +01:00
func startUI(runner *TaskRunner) {
err := termui.Init()
if err != nil {
panic(err)
}
defer termui.Close()
2018-01-27 16:58:56 +01:00
termui.Render(centered(status(runner)))
2018-01-26 16:07:38 +01:00
termui.Handle("/timer/1s", func(termui.Event) {
2018-01-27 16:58:56 +01:00
termui.Render(centered(status(runner)))
})
termui.Handle("/sys/wnd/resize", func(termui.Event) {
termui.Render(centered(status(runner)))
2018-01-26 16:07:38 +01:00
})
termui.Handle("/sys/kbd/<enter>", func(termui.Event) {
runner.Toggle()
2018-01-27 16:58:56 +01:00
termui.Render(centered(status(runner)))
2018-01-26 16:07:38 +01:00
})
termui.Handle("/sys/kbd/p", func(termui.Event) {
runner.Pause()
2018-01-27 16:58:56 +01:00
termui.Render(centered(status(runner)))
2018-01-26 16:07:38 +01:00
})
termui.Handle("/sys/kbd/q", func(termui.Event) {
termui.StopLoop()
})
termui.Loop()
}