Skip to content

Commit

Permalink
add label list command
Browse files Browse the repository at this point in the history
  • Loading branch information
kobtea committed Feb 28, 2017
1 parent 658af7c commit 59a22da
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cmd/todoist/cmd/label.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"
)

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

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

func init() {
RootCmd.AddCommand(labelCmd)
labelCmd.AddCommand(labelListCmd)
}
14 changes: 14 additions & 0 deletions cmd/util/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,17 @@ func ProjectTableString(projects []todoist.Project) string {
}
return TableString(rows)
}

func LabelTableString(labels []todoist.Label) string {
sort.Slice(labels, func(i, j int) bool {
return labels[i].ItemOrder < labels[j].ItemOrder
})
var rows [][]todoist.ColorStringer
for _, l := range labels {
rows = append(rows, []todoist.ColorStringer{
todoist.NewNoColorString(l.ID.String()),
l,
})
}
return TableString(rows)
}
23 changes: 23 additions & 0 deletions todoist/label.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"github.com/fatih/color"
"net/http"
"net/url"
)
Expand All @@ -19,6 +20,28 @@ func (l Label) String() string {
return "@" + l.Name
}

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

type LabelClient struct {
*Client
cache *labelCache
Expand Down

0 comments on commit 59a22da

Please sign in to comment.