improve ui

This commit is contained in:
Kevin Schoon 2018-01-27 23:58:56 +08:00
parent af18268be6
commit 2f2b55cde1
3 changed files with 89 additions and 61 deletions

40
task.go
View File

@ -6,8 +6,11 @@ import (
type TaskRunner struct { type TaskRunner struct {
count int count int
taskID int
taskMessage string
nPomodoros int
origDuration time.Duration
state State state State
task *Task
store *Store store *Store
started time.Time started time.Time
pause chan bool pause chan bool
@ -21,9 +24,11 @@ func NewTaskRunner(task *Task, store *Store) (*TaskRunner, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
task.ID = taskID
tr := &TaskRunner{ tr := &TaskRunner{
task: task, taskID: taskID,
taskMessage: task.Message,
nPomodoros: task.NPomodoros,
origDuration: task.Duration,
store: store, store: store,
state: State(0), state: State(0),
pause: make(chan bool), pause: make(chan bool),
@ -43,7 +48,7 @@ func (t *TaskRunner) TimeRemaining() time.Duration {
} }
func (t *TaskRunner) run() error { func (t *TaskRunner) run() error {
for t.count < t.task.NPomodoros { for t.count < t.nPomodoros {
// Create a new pomodoro where we // Create a new pomodoro where we
// track the start / end time of // track the start / end time of
// of this session. // of this session.
@ -59,42 +64,45 @@ func (t *TaskRunner) run() error {
loop: loop:
select { select {
case <-timer.C: case <-timer.C:
// Timer ended so now we set the
// state as BREAKING
t.state = BREAKING t.state = BREAKING
t.count++
case <-t.toggle: case <-t.toggle:
// Catch any toggles when we // Catch any toggles when we
// are not expecting them // are not expecting them
goto loop goto loop
case <-t.pause: case <-t.pause:
timer.Stop() timer.Stop()
// Record the remaining time of this pomodoro // Record the remaining time of the current pomodoro
remaining := t.TimeRemaining() remaining := t.TimeRemaining()
// Change state to PAUSED // Change state to PAUSED
t.state = PAUSED t.state = PAUSED
// Wait for the user to press [p] // Wait for the user to press [p]
<-t.pause <-t.pause
// Resume the timer at previous // Resume the timer with previous
// remaining time // remaining time
timer.Reset(remaining) timer.Reset(remaining)
// Change duration
t.started = time.Now() t.started = time.Now()
t.duration = remaining t.duration = remaining
// Restore state to RUNNING // Restore state to RUNNING
t.state = RUNNING t.state = RUNNING
goto loop goto loop
} }
if t.count+1 == t.task.NPomodoros {
break
}
// User concludes the break
<-t.toggle
pomodoro.End = time.Now() pomodoro.End = time.Now()
err := t.store.CreatePomodoro(t.task.ID, *pomodoro) err := t.store.CreatePomodoro(t.taskID, *pomodoro)
if err != nil { if err != nil {
return err return err
} }
t.duration = t.task.Duration // All pomodoros completed
t.count++ if t.count == t.nPomodoros {
break
}
// Reset the duration incase it
// was paused.
t.duration = t.origDuration
// User concludes the break
<-t.toggle
} }
t.state = COMPLETE t.state = COMPLETE
return nil return nil

View File

@ -37,35 +37,22 @@ const (
// the display is updated. // the display is updated.
const RefreshInterval = 800 * time.Millisecond const RefreshInterval = 800 * time.Millisecond
// Message is used internally for updating
// the display.
type Message struct {
Start time.Time
Duration time.Duration
Pomodoros int
CurrentPomodoro int
State State
Wheel *Wheel
}
// Wheel keeps track of an ASCII spinner // Wheel keeps track of an ASCII spinner
type Wheel struct { type Wheel int
state int
}
func (w *Wheel) String() string { func (w *Wheel) String() string {
switch w.state { switch int(*w) {
case 0: case 0:
w.state++ *w++
return "|" return "|"
case 1: case 1:
w.state++ *w++
return "/" return "/"
case 2: case 2:
w.state++ *w++
return "-" return "-"
case 3: case 3:
w.state = 0 *w = 0
return "\\" return "\\"
} }
return "" return ""

57
ui.go
View File

@ -5,26 +5,26 @@ import (
"github.com/gizak/termui" "github.com/gizak/termui"
) )
func getText(runner *TaskRunner) *termui.Par { func status(runner *TaskRunner) termui.GridBufferer {
par := termui.NewPar("") var text string
switch runner.state { switch runner.state {
case RUNNING: case RUNNING:
par.Text = fmt.Sprintf( text = fmt.Sprintf(
"%s %s remaining [ pomodoro %d/%d ]", "%s %s remaining [ pomodoro %d/%d ]",
"X", "X",
runner.TimeRemaining(), runner.TimeRemaining(),
runner.count, runner.count,
runner.task.NPomodoros, runner.nPomodoros,
) )
case BREAKING: case BREAKING:
par.Text = "Time to take a break.\nPress [enter] to begin the next Pomodoro!" text = "Time to take a break.\nPress [enter] to begin the next Pomodoro!"
case PAUSED: case PAUSED:
par.Text = "Press p to resume" text = "Press p to resume"
case COMPLETE: case COMPLETE:
par.Text = "Press q to quit" text = "Press q to quit"
} }
par.Height = 8 par := termui.NewPar(text)
par.Width = 55 par.Height = 10
par.BorderLabel = fmt.Sprintf("Pomo - %s", runner.state) par.BorderLabel = fmt.Sprintf("Pomo - %s", runner.state)
par.BorderLabelFg = termui.ColorWhite par.BorderLabelFg = termui.ColorWhite
par.BorderFg = termui.ColorRed par.BorderFg = termui.ColorRed
@ -34,6 +34,33 @@ func getText(runner *TaskRunner) *termui.Par {
return par return par
} }
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
}
func startUI(runner *TaskRunner) { func startUI(runner *TaskRunner) {
err := termui.Init() err := termui.Init()
if err != nil { if err != nil {
@ -42,18 +69,24 @@ func startUI(runner *TaskRunner) {
defer termui.Close() defer termui.Close()
termui.Render(centered(status(runner)))
termui.Handle("/timer/1s", func(termui.Event) { termui.Handle("/timer/1s", func(termui.Event) {
termui.Render(getText(runner)) termui.Render(centered(status(runner)))
})
termui.Handle("/sys/wnd/resize", func(termui.Event) {
termui.Render(centered(status(runner)))
}) })
termui.Handle("/sys/kbd/<enter>", func(termui.Event) { termui.Handle("/sys/kbd/<enter>", func(termui.Event) {
runner.Toggle() runner.Toggle()
termui.Render(getText(runner)) termui.Render(centered(status(runner)))
}) })
termui.Handle("/sys/kbd/p", func(termui.Event) { termui.Handle("/sys/kbd/p", func(termui.Event) {
runner.Pause() runner.Pause()
termui.Render(getText(runner)) termui.Render(centered(status(runner)))
}) })
termui.Handle("/sys/kbd/q", func(termui.Event) { termui.Handle("/sys/kbd/q", func(termui.Event) {