kevinschoon-pomo/ui.go

124 lines
2.3 KiB
Go
Raw Normal View History

2018-01-26 16:07:38 +01:00
package main
import (
"fmt"
2018-07-12 21:03:53 +02:00
2018-01-26 16:07:38 +01:00
"github.com/gizak/termui"
)
func render(wheel *Wheel, status *Status) termui.GridBufferer {
2018-01-27 16:58:56 +01:00
var text string
switch status.State {
2018-01-26 16:07:38 +01:00
case RUNNING:
2018-01-27 16:58:56 +01:00
text = fmt.Sprintf(
2018-01-30 03:18:19 +01:00
`[%d/%d] Pomodoros completed
%s %s remaining
2018-01-27 17:22:41 +01:00
2018-01-30 04:00:41 +01:00
2018-01-27 17:22:41 +01:00
[q] - quit [p] - pause
`,
status.Count,
status.NPomodoros,
2018-01-30 03:18:19 +01:00
wheel,
status.Remaining,
2018-01-26 16:07:38 +01:00
)
case BREAKING:
2018-01-27 17:22:41 +01:00
text = `It is time to take a break!
Once you are ready, press [enter]
2018-01-30 03:18:19 +01:00
to begin the next Pomodoro.
2018-01-27 17:22:41 +01:00
[q] - quit [p] - pause
`
2018-01-26 16:07:38 +01:00
case PAUSED:
2018-01-30 03:18:19 +01:00
text = `Pomo is suspended.
Press [p] to continue.
2018-01-27 17:22:41 +01:00
2018-01-30 04:00:41 +01:00
2018-01-27 17:22:41 +01:00
[q] - quit [p] - unpause
`
2018-01-26 16:07:38 +01:00
case COMPLETE:
2018-01-30 03:18:19 +01:00
text = `This session has concluded.
Press [q] to exit.
2018-01-27 17:22:41 +01:00
2018-01-30 04:00:41 +01:00
2018-01-27 17:22:41 +01:00
[q] - quit
`
2018-01-26 16:07:38 +01:00
}
2018-01-27 16:58:56 +01:00
par := termui.NewPar(text)
2018-01-27 17:22:41 +01:00
par.Height = 8
par.BorderLabel = fmt.Sprintf("Pomo - %s", status.State)
2018-01-26 16:07:38 +01:00
par.BorderLabelFg = termui.ColorWhite
par.BorderFg = termui.ColorRed
if status.State == RUNNING {
2018-01-26 16:07:38 +01:00
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)
}
2018-01-30 03:18:19 +01:00
wheel := Wheel(0)
2018-01-26 16:07:38 +01:00
defer termui.Close()
termui.Render(centered(render(&wheel, runner.Status())))
2018-01-27 16:58:56 +01:00
2018-01-26 16:07:38 +01:00
termui.Handle("/timer/1s", func(termui.Event) {
termui.Render(centered(render(&wheel, runner.Status())))
2018-01-27 16:58:56 +01:00
})
termui.Handle("/sys/wnd/resize", func(termui.Event) {
termui.Render(centered(render(&wheel, runner.Status())))
2018-01-26 16:07:38 +01:00
})
termui.Handle("/sys/kbd/<enter>", func(termui.Event) {
runner.Toggle()
termui.Render(centered(render(&wheel, runner.Status())))
2018-01-26 16:07:38 +01:00
})
termui.Handle("/sys/kbd/p", func(termui.Event) {
runner.Pause()
termui.Render(centered(render(&wheel, runner.Status())))
2018-01-26 16:07:38 +01:00
})
termui.Handle("/sys/kbd/q", func(termui.Event) {
termui.StopLoop()
})
termui.Loop()
}