add pretty list output

This commit is contained in:
Kevin Schoon 2018-01-21 00:51:27 +08:00
parent 060b8ab728
commit 497f5c30e1
4 changed files with 92 additions and 17 deletions

17
main.go
View File

@ -2,19 +2,11 @@ package main
import ( import (
"encoding/json" "encoding/json"
"fmt"
"github.com/jawher/mow.cli" "github.com/jawher/mow.cli"
"os" "os"
"time" "time"
) )
func maybe(err error) {
if err != nil {
fmt.Printf("Error:\n%s\n", err)
os.Exit(1)
}
}
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"
@ -55,13 +47,20 @@ func initialize(path *string) func(*cli.Cmd) {
func list(path *string) func(*cli.Cmd) { func list(path *string) func(*cli.Cmd) {
return func(cmd *cli.Cmd) { return func(cmd *cli.Cmd) {
cmd.Spec = "[OPTIONS]"
var asJSON = cmd.BoolOpt("json", false, "output task history as JSON")
cmd.Action = func() { cmd.Action = func() {
db, err := NewStore(*path) db, err := NewStore(*path)
maybe(err) maybe(err)
defer db.Close() defer db.Close()
tasks, err := db.ReadTasks() tasks, err := db.ReadTasks()
maybe(err) maybe(err)
if *asJSON {
maybe(json.NewEncoder(os.Stdout).Encode(tasks)) maybe(json.NewEncoder(os.Stdout).Encode(tasks))
return
}
config, _ := NewConfig(*path + "/config.json")
summerizeTasks(config, tasks)
} }
} }
} }
@ -83,7 +82,7 @@ func main() {
app := cli.App("pomo", "Pomodoro CLI") app := cli.App("pomo", "Pomodoro CLI")
app.Spec = "[OPTIONS]" app.Spec = "[OPTIONS]"
var ( var (
path = app.StringOpt("p path", defaultDBPath(), "path to the pomo state directory") path = app.StringOpt("p path", defaultConfigPath(), "path to the pomo config directory")
) )
app.Command("start s", "start a new task", start(path)) app.Command("start s", "start a new task", start(path))
app.Command("init", "initialize the sqlite database", initialize(path)) app.Command("init", "initialize the sqlite database", initialize(path))

View File

@ -4,7 +4,6 @@ import (
"database/sql" "database/sql"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"os" "os"
"os/user"
"strings" "strings"
"time" "time"
) )
@ -12,12 +11,6 @@ import (
// 2018-01-16 19:05:21.752851759+08:00 // 2018-01-16 19:05:21.752851759+08:00
const datetimeFmt = "2006-01-02 15:04:05.999999999-07:00" const datetimeFmt = "2006-01-02 15:04:05.999999999-07:00"
func defaultDBPath() string {
u, err := user.Current()
maybe(err)
return u.HomeDir + "/.pomo"
}
type Store struct { type Store struct {
db *sql.DB db *sql.DB
} }

View File

@ -1,6 +1,10 @@
package main package main
import ( import (
"encoding/json"
"fmt"
"github.com/fatih/color"
"io/ioutil"
"os/exec" "os/exec"
"time" "time"
) )
@ -18,6 +22,47 @@ type Message struct {
CurrentPomodoro int CurrentPomodoro int
} }
// Config represents user preferences
type Config struct {
Colors map[string]*color.Color
}
var colorMap = map[string]*color.Color{
"red": color.New(color.FgRed),
"blue": color.New(color.FgBlue),
"green": color.New(color.FgGreen),
"white": color.New(color.FgWhite),
}
func (c *Config) UnmarshalJSON(raw []byte) error {
config := &struct {
Colors map[string]string `json:"colors"`
}{}
err := json.Unmarshal(raw, config)
if err != nil {
return err
}
for key, name := range config.Colors {
if color, ok := colorMap[name]; ok {
c.Colors[key] = color
} else {
return fmt.Errorf("bad color choice: %s", name)
}
}
return nil
}
func NewConfig(path string) (*Config, error) {
raw, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
config := &Config{
Colors: map[string]*color.Color{},
}
return config, json.Unmarshal(raw, config)
}
// Task describes some activity // Task describes some activity
type Task struct { type Task struct {
ID int `json:"id"` ID int `json:"id"`

38
util.go Normal file
View File

@ -0,0 +1,38 @@
package main
import (
"fmt"
//"github.com/fatih/color"
"os"
"os/user"
)
func maybe(err error) {
if err != nil {
fmt.Printf("Error:\n%s\n", err)
os.Exit(1)
}
}
func defaultConfigPath() string {
u, err := user.Current()
maybe(err)
return u.HomeDir + "/.pomo"
}
func summerizeTasks(config *Config, tasks []*Task) {
for _, task := range tasks {
var tags string
if len(task.Tags) > 0 {
for i, tag := range task.Tags {
if color, ok := config.Colors[tag]; ok {
if i > 0 {
tags += " "
}
tags += color.SprintfFunc()("%s", tag)
}
}
}
fmt.Printf("%d [%s]: %s\n", task.ID, tags, task.Message)
}
}