Skip to content

Commit

Permalink
Sel command expects title and if no title is given, selected entry in…
Browse files Browse the repository at this point in the history
…fo is printed
  • Loading branch information
aQaTL committed Mar 18, 2018
1 parent 62d6d3c commit 178ce2e
Showing 1 changed file with 40 additions and 34 deletions.
74 changes: 40 additions & 34 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,27 +94,23 @@ func main() {
Action: setEntryStatus,
},
cli.Command{
Name: "cmpl",
Category: "Update",
Usage: "Set entry status to completed",
Name: "cmpl",
Category: "Update",
Usage: "Set entry status to completed",
UsageText: "mal cmpl",
Action: setEntryStatusCompleted,
Action: setEntryStatusCompleted,
},
cli.Command{
Name: "sel",
Aliases: []string{"select"},
Category: "Config",
Usage: "Select an entry",
UsageText: "mal sel [entry ID]",
UsageText: "mal sel [entry title]",
Action: selectEntry,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "t",
Usage: "Select entry by title instead of by ID",
},
cli.BoolFlag{
Name: "s",
Usage: "Show selected entry",
Name: "id",
Usage: "Select entry by id instead of by title",
},
},
},
Expand Down Expand Up @@ -430,55 +426,51 @@ func setEntryStatusCompleted(ctx *cli.Context) error {
}

func selectEntry(ctx *cli.Context) error {
if ctx.Bool("s") {
return showSelectedEntry(ctx)
}
if ctx.Bool("t") {
switch {
case ctx.Bool("id"):
return selectById(ctx)
default:
return selectByTitle(ctx)
}
}

func selectById(ctx *cli.Context) error {
id, err := strconv.Atoi(ctx.Args().First())
if err != nil {
return fmt.Errorf("invalid id (use with -t to select by title)")
}

cfg := LoadConfig()
cfg.SelectedID = id
cfg.Save()

_, list, err := loadMAL(ctx)
if err != nil {
return err
}

fmt.Println("Selected entry:")
printEntryDetails(list.GetByID(id))

return nil
}

func showSelectedEntry(ctx *cli.Context) error {
cfg := LoadConfig()
_, list, err := loadMAL(ctx)
if err != nil {
return err

entry := list.GetByID(id)
if entry == nil {
return fmt.Errorf("entry %d not found", id)
}

cfg.SelectedID = id
cfg.Save()

fmt.Println("Selected entry:")
selEntry := list.GetByID(cfg.SelectedID)
printEntryDetails(selEntry)
printEntryDetails(entry)

return nil
}

func selectByTitle(ctx *cli.Context) error {
title := strings.ToLower(strings.Join(ctx.Args(), " "))
if title == "" {
return showSelectedEntry(ctx)
}

_, list, err := loadMAL(ctx)
if err != nil {
return err
}

title := strings.ToLower(strings.Join(ctx.Args(), " "))

found := make(mal.AnimeList, 0)
for _, entry := range list {
if strings.Contains(strings.ToLower(entry.Title), title) ||
Expand Down Expand Up @@ -522,6 +514,20 @@ func selectByTitle(ctx *cli.Context) error {
return nil
}

func showSelectedEntry(ctx *cli.Context) error {
cfg := LoadConfig()
_, list, err := loadMAL(ctx)
if err != nil {
return err
}

fmt.Println("Selected entry:")
selEntry := list.GetByID(cfg.SelectedID)
printEntryDetails(selEntry)

return nil
}

func openWebsite(ctx *cli.Context) error {
_, list, err := loadMAL(ctx)
if err != nil {
Expand Down

0 comments on commit 178ce2e

Please sign in to comment.