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

68
task.go
View File

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

View File

@ -37,35 +37,22 @@ const (
// the display is updated.
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
type Wheel struct {
state int
}
type Wheel int
func (w *Wheel) String() string {
switch w.state {
switch int(*w) {
case 0:
w.state++
*w++
return "|"
case 1:
w.state++
*w++
return "/"
case 2:
w.state++
*w++
return "-"
case 3:
w.state = 0
*w = 0
return "\\"
}
return ""

57
ui.go
View File

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