From 497f5c30e12b9acacde9b2dfea9e2e107dcec7e6 Mon Sep 17 00:00:00 2001 From: Kevin Schoon Date: Sun, 21 Jan 2018 00:51:27 +0800 Subject: [PATCH] add pretty list output --- main.go | 19 +++++++++---------- store.go | 7 ------- types.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ util.go | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 17 deletions(-) create mode 100644 util.go diff --git a/main.go b/main.go index 893b101..39a7a3d 100644 --- a/main.go +++ b/main.go @@ -2,19 +2,11 @@ package main import ( "encoding/json" - "fmt" "github.com/jawher/mow.cli" "os" "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) { return func(cmd *cli.Cmd) { cmd.Spec = "[OPTIONS] MESSAGE" @@ -55,13 +47,20 @@ func initialize(path *string) func(*cli.Cmd) { func list(path *string) func(*cli.Cmd) { return func(cmd *cli.Cmd) { + cmd.Spec = "[OPTIONS]" + var asJSON = cmd.BoolOpt("json", false, "output task history as JSON") cmd.Action = func() { db, err := NewStore(*path) maybe(err) defer db.Close() tasks, err := db.ReadTasks() maybe(err) - maybe(json.NewEncoder(os.Stdout).Encode(tasks)) + if *asJSON { + 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.Spec = "[OPTIONS]" 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("init", "initialize the sqlite database", initialize(path)) diff --git a/store.go b/store.go index a761735..ac36440 100644 --- a/store.go +++ b/store.go @@ -4,7 +4,6 @@ import ( "database/sql" _ "github.com/mattn/go-sqlite3" "os" - "os/user" "strings" "time" ) @@ -12,12 +11,6 @@ import ( // 2018-01-16 19:05:21.752851759+08: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 { db *sql.DB } diff --git a/types.go b/types.go index 23b7df9..e879c94 100644 --- a/types.go +++ b/types.go @@ -1,6 +1,10 @@ package main import ( + "encoding/json" + "fmt" + "github.com/fatih/color" + "io/ioutil" "os/exec" "time" ) @@ -18,6 +22,47 @@ type Message struct { 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 type Task struct { ID int `json:"id"` diff --git a/util.go b/util.go new file mode 100644 index 0000000..d710648 --- /dev/null +++ b/util.go @@ -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) + } +}