Files
nib-v1/cmd/cards.go
T

79 lines
1.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cmd
import (
"fmt"
"strings"
"github.com/lerko/nib/internal/db"
"github.com/lerko/nib/internal/display"
"github.com/spf13/cobra"
)
var (
cardsTag string
cardsType string
)
var cardsCmd = &cobra.Command{
Use: "cards",
Short: "list crystallized cards by usage",
RunE: runCards,
}
func init() {
cardsCmd.Flags().StringVar(&cardsTag, "tag", "", "filter by tag")
cardsCmd.Flags().StringVar(&cardsType, "type", "", "filter by card type")
rootCmd.AddCommand(cardsCmd)
}
func runCards(_ *cobra.Command, _ []string) error {
store, err := openStore()
if err != nil {
return err
}
defer store.Close()
p := db.DefaultListParams()
p.CardsOnly = true
p.Sort = "use_count"
p.Order = "desc"
if cardsTag != "" {
p.Tag = &cardsTag
}
if cardsType != "" {
if !db.ValidCardType(cardsType) {
return fmt.Errorf("invalid_type — %q is not a valid card type", cardsType)
}
ct := db.CardType(cardsType)
p.CardTypeFilter = &ct
}
entities, err := store.List(p)
if err != nil {
return err
}
for _, e := range entities {
glyph := display.DisplayGlyph(e.Glyph, e.CardType)
shortID := display.FormatID(e.ID)
var tagStr string
for _, tag := range e.Tags {
tagStr += " #" + tag
}
label := e.Body
if e.Title != nil {
label = *e.Title
}
fmt.Printf("%s %-40s %-16s %3d× %s\n",
glyph, label,
strings.TrimSpace(tagStr),
e.UseCount, shortID)
}
return nil
}