diff --git a/cmd/todoist/cmd/filter.go b/cmd/todoist/cmd/filter.go new file mode 100644 index 0000000..2cfcd0d --- /dev/null +++ b/cmd/todoist/cmd/filter.go @@ -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) +} diff --git a/cmd/util/format.go b/cmd/util/format.go index 4ef8f07..dd2963c 100644 --- a/cmd/util/format.go +++ b/cmd/util/format.go @@ -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 { diff --git a/todoist/filter.go b/todoist/filter.go index 8be30dd..324977a 100644 --- a/todoist/filter.go +++ b/todoist/filter.go @@ -4,8 +4,10 @@ import ( "context" "errors" "fmt" + "github.com/fatih/color" "net/http" "net/url" + "strings" ) type Filter struct { @@ -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 @@ -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 }