[record,stent,count] -> pomodoro

This commit is contained in:
Kevin Schoon 2018-01-20 20:17:58 +08:00
parent 24672d396e
commit 8c071b0c94
4 changed files with 54 additions and 55 deletions

16
main.go
View File

@ -19,10 +19,10 @@ func start(path *string) func(*cli.Cmd) {
return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS] MESSAGE"
var (
duration = cmd.StringOpt("d duration", "25m", "duration of each stent")
stents = cmd.IntOpt("s stents", 4, "number of working stents")
message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task")
tags = cmd.StringsOpt("t tag", []string{}, "tags associated with this task")
duration = cmd.StringOpt("d duration", "25m", "duration of each stent")
pomodoros = cmd.IntOpt("p pomodoros", 4, "number of pomodoros")
message = cmd.StringArg("MESSAGE", "", "descriptive name of the given task")
tags = cmd.StringsOpt("t tag", []string{}, "tags associated with this task")
)
cmd.Action = func() {
parsed, err := time.ParseDuration(*duration)
@ -31,10 +31,10 @@ func start(path *string) func(*cli.Cmd) {
maybe(err)
defer db.Close()
task := Task{
Message: *message,
Tags: *tags,
stents: *stents,
duration: parsed,
Message: *message,
Tags: *tags,
pomodoros: *pomodoros,
duration: parsed,
}
run(task, &I3{}, db)
}

View File

@ -50,12 +50,12 @@ func (s Store) CreateTask(task Task) (int, error) {
return taskID, tx.Commit()
}
func (s Store) CreateRecord(taskID int, record Record) error {
func (s Store) CreatePomodoro(taskID int, pomodoro Pomodoro) error {
_, err := s.db.Exec(
`INSERT INTO record (task_id, start, end) VALUES ($1, $2, $3)`,
`INSERT INTO pomodoro (task_id, start, end) VALUES ($1, $2, $3)`,
taskID,
record.Start,
record.End,
pomodoro.Start,
pomodoro.End,
)
return err
}
@ -68,47 +68,47 @@ func (s Store) ReadTasks() ([]*Task, error) {
tasks := []*Task{}
for rows.Next() {
var tags string
task := &Task{Records: []*Record{}}
task := &Task{Pomodoros: []*Pomodoro{}}
err = rows.Scan(&task.ID, &task.Message, &tags)
if err != nil {
return nil, err
}
task.Tags = strings.Split(tags, ",")
records, err := s.ReadRecords(task.ID)
pomodoros, err := s.ReadPomodoros(task.ID)
if err != nil {
return nil, err
}
for _, record := range records {
task.Records = append(task.Records, record)
for _, pomodoro := range pomodoros {
task.Pomodoros = append(task.Pomodoros, pomodoro)
}
tasks = append(tasks, task)
}
return tasks, nil
}
func (s Store) ReadRecords(taskID int) ([]*Record, error) {
rows, err := s.db.Query(`SELECT start,end FROM record WHERE task_id = $1`, &taskID)
func (s Store) ReadPomodoros(taskID int) ([]*Pomodoro, error) {
rows, err := s.db.Query(`SELECT start,end FROM pomodoro WHERE task_id = $1`, &taskID)
if err != nil {
return nil, err
}
records := []*Record{}
pomodoros := []*Pomodoro{}
for rows.Next() {
var (
startStr string
endStr string
)
record := &Record{}
pomodoro := &Pomodoro{}
err = rows.Scan(&startStr, &endStr)
if err != nil {
return nil, err
}
start, _ := time.Parse(datetimeFmt, startStr)
end, _ := time.Parse(datetimeFmt, endStr)
record.Start = start
record.End = end
records = append(records, record)
pomodoro.Start = start
pomodoro.End = end
pomodoros = append(pomodoros, pomodoro)
}
return records, nil
return pomodoros, nil
}
func (s Store) DeleteTask(taskID int) error {
@ -137,7 +137,7 @@ func initDB(db *Store) error {
message TEXT,
tags TEXT
);
CREATE TABLE record (
CREATE TABLE pomodoro (
task_id INTEGER,
start DATETTIME,
end DATETTIME

33
task.go
View File

@ -9,18 +9,18 @@ import (
// Task Starting..
//
// 20min remaning [stent 1/4]
// 20min remaning [pomodoro 1/4]
// ..
// 15min remaining [stent 2/4]
// 15min remaining [pomodoro 2/4]
// ..
// Task Completed!
func display(writer io.Writer, msg Message) {
fmt.Fprintf(
writer,
"%s remaining [ stent %d/%d ]\n",
"%s remaining [ pomodoro %d/%d ]\n",
(msg.Duration - time.Since(msg.Start)).Truncate(time.Second),
msg.CurrentStent,
msg.Stents,
msg.CurrentPomodoro,
msg.Pomodoros,
)
}
@ -31,29 +31,28 @@ func run(task Task, prompter Prompter, db *Store) {
writer.Start()
ticker := time.NewTicker(RefreshInterval)
timer := time.NewTimer(task.duration)
var currentStent int
for currentStent < task.stents {
record := &Record{}
var p int
for p < task.pomodoros {
pomodoro := &Pomodoro{}
maybe(prompter.Prompt("Begin working!"))
record.Start = time.Now()
pomodoro.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,
Start: pomodoro.Start,
Duration: task.duration,
Pomodoros: task.pomodoros,
CurrentPomodoro: p,
})
goto loop
case <-timer.C:
maybe(prompter.Prompt("Take a break!"))
record.End = time.Now()
maybe(db.CreateRecord(taskID, *record))
currentStent++
pomodoro.End = time.Now()
maybe(db.CreatePomodoro(taskID, *pomodoro))
p++
}
maybe(db.CreateRecord(taskID, *record))
}
writer.Stop()
}

View File

@ -12,27 +12,27 @@ 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
Start time.Time
Duration time.Duration
Pomodoros int
CurrentPomodoro int
}
// Task describes some activity
type Task struct {
ID int `json:"id"`
Message string `json:"message"`
Records []*Record `json:"records"`
ID int `json:"id"`
Message string `json:"message"`
Pomodoros []*Pomodoro `json:"pomodoros"`
// Free-form tags associated with this task
Tags []string `json:"tags"`
// Number of iterations to perform the task
stents int
duration time.Duration
// Number of pomodoros for this task
pomodoros int
duration time.Duration
}
// Record is a stetch of work performed on a
// specific task.
type Record struct {
// Pomodoro is a unit of time to spend working
// on a single task.
type Pomodoro struct {
Start time.Time `json:"start"`
End time.Time `json:"end"`
}