Skip to content

Commit

Permalink
Allow filtering by active decks in clsr list decks
Browse files Browse the repository at this point in the history
  • Loading branch information
adamkpickering committed Feb 22, 2023
1 parent 69237b0 commit b33786d
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion cmd/list_deck.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,13 @@ import (
"github.com/spf13/cobra"
)

var listDeckFlags = struct {
All bool
}{}

func init() {
listCmd.AddCommand(listDeckCmd)
listDeckCmd.Flags().BoolVarP(&listDeckFlags.All, "all", "a", false, "list all decks, not just active ones")
}

var listDeckCmd = &cobra.Command{
Expand All @@ -48,11 +53,23 @@ var listDeckCmd = &cobra.Command{
scheduler := scheduler.NewTwoReviewScheduler(config.DefaultConfig)

// get all decks
decks, err := utils.GetDecks(deckSource)
allDecks, err := utils.GetDecks(deckSource)
if err != nil {
return fmt.Errorf("failed to get decks: %w", err)
}

// filter decks if necessary
decks := make([]*models.Deck, 0, len(allDecks))
if listDeckFlags.All {
decks = allDecks
} else {
for _, deck := range allDecks {
if deck.Active {
decks = append(decks, deck)
}
}
}

// display decks in table
table := simpletable.New()
table.SetStyle(simpletable.StyleCompactClassic)
Expand All @@ -65,6 +82,9 @@ var listDeckCmd = &cobra.Command{
{Text: "Total Cards"},
},
}
if cmd.Flags().Changed("all") {
table.Header.Cells = append(table.Header.Cells, &simpletable.Cell{Text: "Active"})
}
for _, deck := range decks {
dueCount, err := countCardsDue(deck, scheduler)
if err != nil {
Expand All @@ -78,6 +98,9 @@ var listDeckCmd = &cobra.Command{
{Text: fmt.Sprintf("%d", inactiveCount)},
{Text: fmt.Sprintf("%d", len(deck.Cards))},
}
if cmd.Flags().Changed("all") {
row = append(row, &simpletable.Cell{Text: fmt.Sprintf("%t", deck.Active)})
}
table.Body.Cells = append(table.Body.Cells, row)
}
fmt.Println(table.String())
Expand Down

0 comments on commit b33786d

Please sign in to comment.