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) {
|
2018-02-04 04:13:46 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
%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
|
|
|
|
`,
|
2018-02-04 04:13:46 +01:00
|
|
|
status.Count,
|
|
|
|
status.NPomodoros,
|
2018-01-30 03:18:19 +01:00
|
|
|
wheel,
|
2018-02-04 04:13:46 +01:00
|
|
|
status.Remaining,
|
2018-01-26 16:07:38 +01:00
|
|
|
)
|
|
|
|
case BREAKING:
|
2018-01-27 17:22:41 +01:00
|
|
|
|
2021-08-31 00:31:35 +02:00
|
|
|
par.Text = fmt.Sprintf(
|
|
|
|
`It is time to take a break!
|
2018-01-27 17:22:41 +01:00
|
|
|
|
2021-08-31 00:31:35 +02:00
|
|
|
|
|
|
|
Once you are ready, press [Enter]
|
|
|
|
to begin the next Pomodoro
|
|
|
|
|
|
|
|
%s %s pause duration
|
|
|
|
|
|
|
|
|
|
|
|
[q] - quit [p] - pause
|
|
|
|
`,
|
|
|
|
wheel,
|
|
|
|
status.Pauseduration,
|
|
|
|
)
|
2018-01-26 16:07:38 +01:00
|
|
|
case PAUSED:
|
2021-07-29 00:52:20 +02:00
|
|
|
par.Text = `Pomo is suspended.
|
2018-01-30 03:18:19 +01:00
|
|
|
|
|
|
|
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:
|
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
|
2018-02-04 04:13:46 +01:00
|
|
|
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
|
|
|
|
2021-07-29 00:52:20 +02:00
|
|
|
x1 := (termWidth - 50) / 2
|
|
|
|
x2 := x1 + 50
|
2021-08-31 00:31:35 +02:00
|
|
|
|
2021-07-29 00:52:20 +02:00
|
|
|
y1 := (termHeight - 8) / 2
|
|
|
|
y2 := y1 + 8
|
2018-01-26 16:07:38 +01:00
|
|
|
|
2021-08-31 00:31:35 +02:00
|
|
|
switch runner.state {
|
|
|
|
case BREAKING:
|
|
|
|
y1 = (termHeight - 12) / 2
|
|
|
|
y2 = y1 + 12
|
|
|
|
}
|
|
|
|
|
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 {
|
2021-08-31 00:31:35 +02:00
|
|
|
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()
|
2021-08-31 00:31:35 +02:00
|
|
|
resize()
|
2021-07-29 00:52:20 +02:00
|
|
|
render()
|
|
|
|
case "p":
|
|
|
|
runner.Pause()
|
|
|
|
render()
|
|
|
|
}
|
|
|
|
case <-ticker.C:
|
2021-08-31 00:31:35 +02:00
|
|
|
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
|
|
|
|
|
|
|
}
|