kevinschoon-pomo/pkg/internal/ui.go

150 lines
2.2 KiB
Go
Raw Normal View History

2020-09-08 18:35:47 +02:00
package pomo
2018-01-26 16:07:38 +01:00
import (
"fmt"
2021-07-29 00:52:20 +02:00
"time"
2018-07-12 21:03:53 +02:00
2021-07-29 00:52:20 +02:00
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
2018-01-26 16:07:38 +01:00
)
2021-07-29 00:52:20 +02:00
func setContent(wheel *Wheel, status *Status, par *widgets.Paragraph) {
switch status.State {
2018-01-26 16:07:38 +01:00
case RUNNING:
2021-07-29 00:52:20 +02:00
par.Text = fmt.Sprintf(
2018-01-30 03:18:19 +01:00
`[%d/%d] Pomodoros completed
Current Task: %s
2018-01-30 03:18:19 +01:00
%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,
status.TaskMessage,
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
par.Text = fmt.Sprintf(
`It is time to take a break!
2018-01-27 17:22:41 +01:00
Once you are ready, press [Enter]
to begin the next Pomodoro
%s %s break duration
[q] - quit
`,
wheel,
status.Pauseduration,
)
2018-01-26 16:07:38 +01:00
case PAUSED:
par.Text = fmt.Sprintf(`Pomo is suspended.
Current Task: %s
Press [p] to continue.
[q] - quit [p] - unpause
`,
status.TaskMessage,
)
2018-01-26 16:07:38 +01:00
case COMPLETE:
2021-07-29 00:52:20 +02:00
par.Text = `This session has concluded.
2018-01-30 03:18:19 +01:00
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
}
2021-07-29 00:52:20 +02:00
par.Title = fmt.Sprintf("Pomo - %s", status.State)
par.TitleStyle.Fg = ui.ColorWhite
par.BorderStyle.Fg = ui.ColorRed
if status.State == RUNNING {
2021-07-29 00:52:20 +02:00
par.BorderStyle.Fg = ui.ColorGreen
2018-01-26 16:07:38 +01:00
}
2018-01-27 16:58:56 +01:00
}
2020-09-08 18:35:47 +02:00
func StartUI(runner *TaskRunner) {
2021-07-29 00:52:20 +02:00
err := ui.Init()
2018-01-26 16:07:38 +01:00
if err != nil {
panic(err)
}
2021-07-29 00:52:20 +02:00
ticker := time.NewTicker(250 * time.Millisecond)
2018-01-26 16:07:38 +01:00
2021-07-29 00:52:20 +02:00
defer ui.Close()
2018-01-27 16:58:56 +01:00
2021-07-29 00:52:20 +02:00
wheel := Wheel(0)
par := widgets.NewParagraph()
resize := func() {
termWidth, termHeight := ui.TerminalDimensions()
2018-01-27 16:58:56 +01:00
// for RUNNING and PAUSED states
2021-07-29 00:52:20 +02:00
x1 := (termWidth - 50) / 2
x2 := x1 + 50
y1 := (termHeight - 10) / 2
y2 := y1 + 10
2018-01-26 16:07:38 +01:00
switch runner.state {
case BREAKING:
y1 = (termHeight - 11) / 2
y2 = y1 + 11
case COMPLETE:
y1 = (termHeight - 8) / 2
y2 = y1 + 8
}
2021-07-29 00:52:20 +02:00
par.SetRect(x1, y1, x2, y2)
ui.Clear()
}
2018-01-26 16:07:38 +01:00
2021-07-29 00:52:20 +02:00
render := func() {
setContent(&wheel, runner.Status(), par)
ui.Render(par)
}
2018-01-26 16:07:38 +01:00
2021-07-29 00:52:20 +02:00
resize()
render()
events := ui.PollEvents()
for {
laststate := runner.state
2021-07-29 00:52:20 +02:00
select {
case e := <-events:
switch e.ID {
case "q", "<C-c>":
return
case "<Resize>":
resize()
render()
case "<Enter>":
runner.Toggle()
resize()
2021-07-29 00:52:20 +02:00
render()
case "p":
runner.Pause()
render()
}
case <-ticker.C:
if runner.state != laststate {
resize()
laststate = runner.state
}
2021-07-29 00:52:20 +02:00
render()
}
}
2018-01-26 16:07:38 +01:00
}