Skip to content

Commit

Permalink
add filter list command
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Feb 28, 2017
1 parent b803eb9 commit 5d2fb9a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cmd/todoist/cmd/filter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import (
"fmt"

"github.com/kobtea/go-todoist/cmd/util"
"github.com/spf13/cobra"
)

// filterCmd represents the filter command
var filterCmd = &cobra.Command{
Use: "filter",
Short: "subcommand for filter",
}

var filterListCmd = &cobra.Command{
Use: "list",
Short: "list filters",
RunE: func(cmd *cobra.Command, args []string) error {
client, err := util.NewClient()
if err != nil {
return err
}
filters := client.Filter.GetAll()
fmt.Println(util.FilterTableString(filters))
return nil
},
}

func init() {
RootCmd.AddCommand(filterCmd)
filterCmd.AddCommand(filterListCmd)
}
15 changes: 15 additions & 0 deletions cmd/util/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ func TableString(rows [][]todoist.ColorStringer) string {
return res
}

func FilterTableString(filters []todoist.Filter) string {
sort.Slice(filters, func(i, j int) bool {
return filters[i].ItemOrder < filters[j].ItemOrder
})
var rows [][]todoist.ColorStringer
for _, f := range filters {
rows = append(rows, []todoist.ColorStringer{
todoist.NewNoColorString(f.ID.String()),
f,
todoist.NewNoColorString(f.Query),
})
}
return TableString(rows)
}

func ItemTableString(items []todoist.Item, relations todoist.ItemRelations) string {
var rows [][]todoist.ColorStringer
for _, i := range items {
Expand Down
38 changes: 38 additions & 0 deletions todoist/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"errors"
"fmt"
"github.com/fatih/color"
"net/http"
"net/url"
"strings"
)

type Filter struct {
Expand All @@ -16,6 +18,32 @@ type Filter struct {
ItemOrder int `json:"item_order"`
}

func (f Filter) String() string {
return f.Name
}

func (f Filter) ColorString() string {
var attr color.Attribute
switch f.Color {
case 2, 4, 10:
attr = color.FgHiRed
case 0, 11:
attr = color.FgHiGreen
case 1:
attr = color.FgHiYellow
case 5, 8:
attr = color.FgHiBlue
case 3:
attr = color.FgHiMagenta
case 6, 9:
attr = color.FgHiCyan
case 7, 12:
default:
attr = color.FgHiBlack
}
return color.New(attr).Sprint(f.String())
}

type FilterClient struct {
*Client
cache *filterCache
Expand Down Expand Up @@ -95,6 +123,16 @@ func (c *FilterClient) Resolve(id ID) *Filter {
return c.cache.resolve(id)
}

func (c FilterClient) FindByName(substr string) []Filter {
var res []Filter
for _, f := range c.GetAll() {
if strings.Contains(f.Name, substr) {
res = append(res, f)
}
}
return res
}

type filterCache struct {
cache *[]Filter
}
Expand Down

0 comments on commit 5d2fb9a

Please sign in to comment.