add pretty list output
This commit is contained in:
parent
060b8ab728
commit
497f5c30e1
17
main.go
17
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)
|
||||
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))
|
||||
|
|
7
store.go
7
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
|
||||
}
|
||||
|
|
45
types.go
45
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"`
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue