diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-07-02 17:04:29 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-07-02 17:04:29 +0530 |
commit | 7e512b17f64b85390e189754081fb9a7994cd083 (patch) | |
tree | f3643990e456701ddb96d73a5c446eb2bfcf7141 /ui | |
parent | fb7324295cf3f32c530135a0456f0afd14324d29 (diff) |
added score option to action menu
Diffstat (limited to 'ui')
-rw-r--r-- | ui/actions.go | 8 | ||||
-rw-r--r-- | ui/score.go | 55 |
2 files changed, 46 insertions, 17 deletions
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)) } |