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) {
fmt.Fprintf(
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.CurrentPomodoro,
msg.Pomodoros,
@ -31,6 +32,7 @@ func run(task Task, prompter Prompter, db *Store) {
writer.Start()
ticker := time.NewTicker(RefreshInterval)
timer := time.NewTimer(task.duration)
wheel := &Wheel{}
var p int
for p < task.pomodoros {
pomodoro := &Pomodoro{}
@ -44,6 +46,7 @@ func run(task Task, prompter Prompter, db *Store) {
Start: pomodoro.Start,
Duration: task.duration,
Pomodoros: task.pomodoros,
Wheel: wheel,
CurrentPomodoro: p,
})
goto loop

View File

@ -20,6 +20,30 @@ type Message struct {
Duration time.Duration
Pomodoros 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