dynamically update display
This commit is contained in:
parent
6dab576f27
commit
24672d396e
27
main.go
27
main.go
|
@ -15,33 +15,12 @@ func maybe(err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func startTask(task Task, prompter Prompter, db *Store) {
|
|
||||||
taskID, err := db.CreateTask(task)
|
|
||||||
maybe(err)
|
|
||||||
for i := 0; i < task.count; i++ {
|
|
||||||
// Create a record for
|
|
||||||
// this particular stent of work
|
|
||||||
record := &Record{}
|
|
||||||
// Prompt the client
|
|
||||||
maybe(prompter.Prompt("Begin Working!"))
|
|
||||||
record.Start = time.Now()
|
|
||||||
// Wait the specified interval
|
|
||||||
time.Sleep(task.duration)
|
|
||||||
maybe(prompter.Prompt("Take a Break!"))
|
|
||||||
// Record how long the user waited
|
|
||||||
// until closing the notification
|
|
||||||
record.End = time.Now()
|
|
||||||
maybe(db.CreateRecord(taskID, *record))
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func start(path *string) func(*cli.Cmd) {
|
func start(path *string) func(*cli.Cmd) {
|
||||||
return func(cmd *cli.Cmd) {
|
return func(cmd *cli.Cmd) {
|
||||||
cmd.Spec = "[OPTIONS] MESSAGE"
|
cmd.Spec = "[OPTIONS] MESSAGE"
|
||||||
var (
|
var (
|
||||||
duration = cmd.StringOpt("d duration", "25m", "duration of each stent")
|
duration = cmd.StringOpt("d duration", "25m", "duration of each stent")
|
||||||
count = cmd.IntOpt("c count", 4, "number of working stents")
|
stents = cmd.IntOpt("s stents", 4, "number of working stents")
|
||||||
message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task")
|
message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task")
|
||||||
tags = cmd.StringsOpt("t tag", []string{}, "tags associated with this task")
|
tags = cmd.StringsOpt("t tag", []string{}, "tags associated with this task")
|
||||||
)
|
)
|
||||||
|
@ -54,10 +33,10 @@ func start(path *string) func(*cli.Cmd) {
|
||||||
task := Task{
|
task := Task{
|
||||||
Message: *message,
|
Message: *message,
|
||||||
Tags: *tags,
|
Tags: *tags,
|
||||||
count: *count,
|
stents: *stents,
|
||||||
duration: parsed,
|
duration: parsed,
|
||||||
}
|
}
|
||||||
startTask(task, &I3{}, db)
|
run(task, &I3{}, db)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,59 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"github.com/gosuri/uilive"
|
||||||
|
"io"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Task Starting..
|
||||||
|
//
|
||||||
|
// 20min remaning [stent 1/4]
|
||||||
|
// ..
|
||||||
|
// 15min remaining [stent 2/4]
|
||||||
|
// ..
|
||||||
|
// Task Completed!
|
||||||
|
func display(writer io.Writer, msg Message) {
|
||||||
|
fmt.Fprintf(
|
||||||
|
writer,
|
||||||
|
"%s remaining [ stent %d/%d ]\n",
|
||||||
|
(msg.Duration - time.Since(msg.Start)).Truncate(time.Second),
|
||||||
|
msg.CurrentStent,
|
||||||
|
msg.Stents,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func run(task Task, prompter Prompter, db *Store) {
|
||||||
|
taskID, err := db.CreateTask(task)
|
||||||
|
maybe(err)
|
||||||
|
writer := uilive.New()
|
||||||
|
writer.Start()
|
||||||
|
ticker := time.NewTicker(RefreshInterval)
|
||||||
|
timer := time.NewTimer(task.duration)
|
||||||
|
var currentStent int
|
||||||
|
for currentStent < task.stents {
|
||||||
|
record := &Record{}
|
||||||
|
maybe(prompter.Prompt("Begin working!"))
|
||||||
|
record.Start = time.Now()
|
||||||
|
timer.Reset(task.duration)
|
||||||
|
loop:
|
||||||
|
select {
|
||||||
|
case <-ticker.C:
|
||||||
|
display(writer, Message{
|
||||||
|
Start: record.Start,
|
||||||
|
Duration: task.duration,
|
||||||
|
Stents: task.stents,
|
||||||
|
CurrentStent: currentStent,
|
||||||
|
})
|
||||||
|
goto loop
|
||||||
|
case <-timer.C:
|
||||||
|
maybe(prompter.Prompt("Take a break!"))
|
||||||
|
record.End = time.Now()
|
||||||
|
maybe(db.CreateRecord(taskID, *record))
|
||||||
|
currentStent++
|
||||||
|
}
|
||||||
|
maybe(db.CreateRecord(taskID, *record))
|
||||||
|
}
|
||||||
|
writer.Stop()
|
||||||
|
}
|
18
types.go
18
types.go
|
@ -5,14 +5,28 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// RefreshInterval is the frequency at which
|
||||||
|
// 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
|
||||||
|
Stents int
|
||||||
|
CurrentStent int
|
||||||
|
}
|
||||||
|
|
||||||
// Task describes some activity
|
// Task describes some activity
|
||||||
type Task struct {
|
type Task struct {
|
||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
Message string `json:"message"`
|
Message string `json:"message"`
|
||||||
Records []*Record `json:"records"`
|
Records []*Record `json:"records"`
|
||||||
// Free-form tags associated with this task
|
// Free-form tags associated with this task
|
||||||
Tags []string `json:"tags"`
|
Tags []string `json:"tags"`
|
||||||
count int
|
// Number of iterations to perform the task
|
||||||
|
stents int
|
||||||
duration time.Duration
|
duration time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue