diff options
| author | Grant Ammons <gammons@gmail.com> | 2016-04-24 19:58:53 -0400 |
|---|---|---|
| committer | Grant Ammons <gammons@gmail.com> | 2016-04-24 19:58:53 -0400 |
| commit | dde4f5330c2126af0e3ec17e4098653c6f551426 (patch) | |
| tree | 6e3fa6b61f2e5b2d19463c7467808e44e87b290b | |
| parent | 51770b80db0fe299c69bef5060662178f67eb714 (diff) | |
Bring in the app and router
| -rw-r--r-- | todo.go | 36 | ||||
| -rw-r--r-- | todolist/app.go | 16 | ||||
| -rw-r--r-- | todolist/file_store.go | 3 | ||||
| -rw-r--r-- | todolist/formatter.go | 67 | ||||
| -rw-r--r-- | todolist/store.go | 11 |
5 files changed, 107 insertions, 26 deletions
@@ -3,35 +3,29 @@ package main import ( "fmt" "os" - "strconv" - "text/tabwriter" + "strings" "github.com/gammons/todolist/todolist" ) func main() { - store := todolist.NewFileStore() - store.Load() - - doOutput(store.Data) + if len(os.Args) <= 1 { + usage() + os.Exit(0) + } + routeInput(os.Args[1]) } -func doOutput(todos []todolist.Todo) { - w := new(tabwriter.Writer) - w.Init(os.Stdout, 0, 8, 0, '\t', 0) - - for _, item := range todos { - str := strconv.Itoa(item.Id) + "\t" + completedText(item.Completed) + "\t" + item.Subject - fmt.Fprintln(w, str) - //fmt.Fprintln(w, - } - w.Flush() +func usage() { + fmt.Println("usage") } -func completedText(completed bool) string { - if completed { - return "[x]" - } else { - return "[ ]" +func routeInput(command string) { + app := todolist.NewApp() + input := strings.Join(os.Args[1:], " ") + switch { + case command == "l" || command == "list": + app.ListTodos(input) } + } diff --git a/todolist/app.go b/todolist/app.go new file mode 100644 index 0000000..3235f99 --- /dev/null +++ b/todolist/app.go @@ -0,0 +1,16 @@ +package todolist + +type App struct { + TodoStore Store +} + +func NewApp() *App { + app := &App{TodoStore: NewFileStore()} + app.TodoStore.Load() + return app +} + +func (a *App) ListTodos(input string) { + formatter := NewFormatter(a.TodoStore.Todos()) + formatter.Print() +} diff --git a/todolist/file_store.go b/todolist/file_store.go index 3f71ba2..ac3dff5 100644 --- a/todolist/file_store.go +++ b/todolist/file_store.go @@ -30,5 +30,8 @@ func (f *FileStore) Load() { fmt.Println("Error reading json data", jerr) os.Exit(1) } +} +func (f *FileStore) Todos() []Todo { + return f.Data } diff --git a/todolist/formatter.go b/todolist/formatter.go new file mode 100644 index 0000000..b2b9816 --- /dev/null +++ b/todolist/formatter.go @@ -0,0 +1,67 @@ +package todolist + +import ( + "fmt" + "os" + "regexp" + "strconv" + "strings" + "text/tabwriter" + + "github.com/fatih/color" +) + +type Formatter struct { + Todos []Todo + Writer *tabwriter.Writer +} + +func NewFormatter(todos []Todo) *Formatter { + w := new(tabwriter.Writer) + w.Init(os.Stdout, 0, 8, 0, '\t', 0) + formatter := &Formatter{Todos: todos, Writer: w} + return formatter +} + +func (f *Formatter) Print() { + for _, todo := range f.Todos { + yellow := color.New(color.FgYellow).SprintFunc() + + fmt.Fprintf(f.Writer, " \t%s\t%s\t%s\t\n", + yellow(strconv.Itoa(todo.Id)), + f.formatCompleted(todo.Completed), + f.formatSubject(todo.Subject)) + } + f.Writer.Flush() +} + +func (f *Formatter) formatSubject(subject string) string { + red := color.New(color.FgRed).SprintFunc() + magenta := color.New(color.FgMagenta).SprintFunc() + + splitted := strings.Split(subject, " ") + projectRegex, _ := regexp.Compile(`\+\w+`) + contextRegex, _ := regexp.Compile(`\@\w+`) + + coloredWords := []string{} + + for _, word := range splitted { + if projectRegex.MatchString(word) { + coloredWords = append(coloredWords, magenta(word)) + } else if contextRegex.MatchString(word) { + coloredWords = append(coloredWords, red(word)) + } else { + coloredWords = append(coloredWords, word) + } + } + return strings.Join(coloredWords, " ") + +} + +func (f *Formatter) formatCompleted(completed bool) string { + if completed { + return "[x]" + } else { + return "[ ]" + } +} diff --git a/todolist/store.go b/todolist/store.go index 006583a..8b892bf 100644 --- a/todolist/store.go +++ b/todolist/store.go @@ -2,10 +2,11 @@ package todolist type Store interface { Load() - Save() + Todos() []Todo + //Save() - Find(id int) Todo - Add(t *Todo) - Remove(t *Todo) - NextId() int + //Find(id int) Todo + //Add(t *Todo) + //Remove(t *Todo) + //NextId() int } |
