From 7e512b17f64b85390e189754081fb9a7994cd083 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Sat, 2 Jul 2022 17:04:29 +0530 Subject: added score option to action menu --- cmd/score.go | 4 ++-- cmd/version.go | 2 +- ui/actions.go | 8 ++------ ui/score.go | 55 ++++++++++++++++++++++++++++++++++++++++++++----------- 4 files changed, 49 insertions(+), 20 deletions(-) diff --git a/cmd/score.go b/cmd/score.go index 27106e5..33c02c3 100644 --- a/cmd/score.go +++ b/cmd/score.go @@ -97,7 +97,7 @@ func setAnimeScore(scoreInput, searchInput string) { } if scoreInput == "" { - ui.ScoreInput(anime.Id, selectedAnime.MyListStatus.Score, anime.Title, false) + ui.AnimeScoreInput(selectedAnime) } else { score, err := validateScore(scoreInput, selectedAnime.MyListStatus.Score) if err != nil { @@ -130,7 +130,7 @@ func setMangaScore(scoreInput, searchInput string) { } if scoreInput == "" { - ui.ScoreInput(manga.Id, selectedManga.MyListStatus.Score, manga.Title, true) + ui.MangaScoreInput(selectedManga) } else { score, err := validateScore(scoreInput, selectedManga.MyListStatus.Score) if err != nil { diff --git a/cmd/version.go b/cmd/version.go index 174b7e3..c89866b 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -24,7 +24,7 @@ import ( "github.com/spf13/cobra" ) -var version string = "v1.5.3" +var version string = "v1.5.4" var versionCmd = &cobra.Command { Use: "version", diff --git a/ui/actions.go b/ui/actions.go index 2600b21..b6bbf16 100644 --- a/ui/actions.go +++ b/ui/actions.go @@ -47,9 +47,7 @@ func AnimeActionMenu(animeIsAdded bool) func(a.Anime) { options := []AnimeAction { {"Set Status", "Set status for an anime (watching, dropped, etc)", AnimeStatusMenu}, {"Set Episodes", "Set number of episodes watched", EpisodeInput}, - // these only temporarily run AnimeStatusMenu - // because that functionality doesnt exist yet - // {"Set Score", "Set score", AnimeStatusMenu}, + {"Set Score", "Set score", AnimeScoreInput}, // {"Set Re-watching", "Set if re-watching", AnimeStatusMenu}, // {"Set Times Re-watched", "Set number of times re-watched", AnimeStatusMenu}, } @@ -104,9 +102,7 @@ func MangaActionMenu(mangaIsAdded bool) func(m.Manga) { options := []MangaAction { {"Set Status", "Set status for a manga (reading, dropped, etc)", MangaStatusMenu}, {"Set Chapters", "Set number of chapters read", ChapterInput}, - // these only temporarily run MangaStatusMenu - // because that functionality doesnt exist yet - // {"Set Score", "Set score", MangaStatusMenu}, + {"Set Score", "Set score", MangaScoreInput}, // {"Set Re-reading", "Set if re-reading", MangaStatusMenu}, // {"Set Times Re-read", "Set number of times re-read", MangaStatusMenu}, } diff --git a/ui/score.go b/ui/score.go index fd83d0b..8555756 100644 --- a/ui/score.go +++ b/ui/score.go @@ -25,7 +25,8 @@ import ( "os" "github.com/MikunoNaka/macli/mal" "github.com/MikunoNaka/macli/util" - // m "github.com/MikunoNaka/MAL2Go/v2/manga" + m "github.com/MikunoNaka/MAL2Go/v2/manga" + a "github.com/MikunoNaka/MAL2Go/v2/anime" p "github.com/manifoldco/promptui" ) @@ -68,7 +69,8 @@ func CreateScoreUpdateConfirmationMessage(title string, prevScore, score int) st return fmt.Sprintf("\x1b[35m%s\x1b[0m Score :: %s -> %s", title, FormatScore(prevScore), FormatScore(score)) } -func ScoreInput(id, currentScore int, title string, isManga bool) { +func AnimeScoreInput(anime a.Anime) { + currentScore := anime.MyListStatus.Score validate := func(input string) error { i, err := strconv.ParseFloat(input, 64) if err != nil || i < -10 || i > 10 { @@ -102,15 +104,46 @@ func ScoreInput(id, currentScore int, title string, isManga bool) { os.Exit(1) } - var newScore int - parsedScore := util.ParseNumeric(res, currentScore) - if isManga { - resp := mal.SetMangaScore(id, parsedScore) - newScore = resp.Score - } else { - resp := mal.SetAnimeScore(id, parsedScore) - newScore = resp.Score + resp := mal.SetAnimeScore(anime.Id, util.ParseNumeric(res, currentScore)) + fmt.Println(CreateScoreUpdateConfirmationMessage(anime.Title, currentScore, resp.Score)) +} + +func MangaScoreInput(manga m.Manga) { + currentScore := manga.MyListStatus.Score + + validate := func(input string) error { + i, err := strconv.ParseFloat(input, 64) + if err != nil || i < -10 || i > 10 { + return errors.New("Input must be a number within 0-10.") + } + newScore := util.ParseNumeric(input, currentScore) + if newScore < 0 { + return errors.New("Score out of range (" + strconv.Itoa(newScore) + " < 0)") + } + if newScore > 10 { + return errors.New("Score out of range (" + strconv.Itoa(newScore) + " > 10)") + } + return nil + } + + template := &p.PromptTemplates { + Valid: "\x1b[0m{{ . | magenta }}", + Invalid: "\x1b[0m{{ . | magenta }}\x1b[31m", + Success: "{{ . | cyan }}", + } + + prompt := p.Prompt { + Label: fmt.Sprintf("Set Score (Current: %d): ", currentScore), + Templates: template, + Validate: validate, + } + + res, err := prompt.Run() + if err != nil { + fmt.Println("Error Running score input Prompt.", err.Error()) + os.Exit(1) } - fmt.Println(CreateScoreUpdateConfirmationMessage(title, currentScore, newScore)) + resp := mal.SetMangaScore(manga.Id, util.ParseNumeric(res, currentScore)) + fmt.Println(CreateScoreUpdateConfirmationMessage(manga.Title, currentScore, resp.Score)) } -- cgit v1.2.3