add cool ascii spinner

This commit is contained in:
Kevin Schoon 2018-01-21 01:49:07 +08:00
parent 782bd5956a
commit b5042fdbeb
2 changed files with 28 additions and 1 deletions

View File

@ -17,7 +17,8 @@ import (
func display(writer io.Writer, msg Message) { func display(writer io.Writer, msg Message) {
fmt.Fprintf( fmt.Fprintf(
writer, writer,
"%s remaining [ pomodoro %d/%d ]\n", "%s %s remaining [ pomodoro %d/%d ]\n",
msg.Wheel,
(msg.Duration - time.Since(msg.Start)).Truncate(time.Second), (msg.Duration - time.Since(msg.Start)).Truncate(time.Second),
msg.CurrentPomodoro, msg.CurrentPomodoro,
msg.Pomodoros, msg.Pomodoros,
@ -31,6 +32,7 @@ func run(task Task, prompter Prompter, db *Store) {
writer.Start() writer.Start()
ticker := time.NewTicker(RefreshInterval) ticker := time.NewTicker(RefreshInterval)
timer := time.NewTimer(task.duration) timer := time.NewTimer(task.duration)
wheel := &Wheel{}
var p int var p int
for p < task.pomodoros { for p < task.pomodoros {
pomodoro := &Pomodoro{} pomodoro := &Pomodoro{}
@ -44,6 +46,7 @@ func run(task Task, prompter Prompter, db *Store) {
Start: pomodoro.Start, Start: pomodoro.Start,
Duration: task.duration, Duration: task.duration,
Pomodoros: task.pomodoros, Pomodoros: task.pomodoros,
Wheel: wheel,
CurrentPomodoro: p, CurrentPomodoro: p,
}) })
goto loop goto loop

View File

@ -20,6 +20,30 @@ type Message struct {
Duration time.Duration Duration time.Duration
Pomodoros int Pomodoros int
CurrentPomodoro int CurrentPomodoro int
Wheel *Wheel
}
// Wheel keeps track of an ASCII spinner
type Wheel struct {
state int
}
func (w *Wheel) String() string {
switch w.state {
case 0:
w.state++
return "|"
case 1:
w.state++
return "/"
case 2:
w.state++
return "-"
case 3:
w.state = 0
return "\\"
}
return ""
} }
// Config represents user preferences // Config represents user preferences