diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-07-02 16:46:14 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-07-02 16:46:14 +0530 |
commit | fb7324295cf3f32c530135a0456f0afd14324d29 (patch) | |
tree | 1118db78fdf54f63fc8596bc41afee9b26ff4935 /ui/score.go | |
parent | 720045d9ea2e612ca0f0b7333ee62a73b2955a15 (diff) |
incrementing/decrementing score now
Diffstat (limited to 'ui/score.go')
-rw-r--r-- | ui/score.go | 20 |
1 files changed, 12 insertions, 8 deletions
diff --git a/ui/score.go b/ui/score.go index 4832f6a..fd83d0b 100644 --- a/ui/score.go +++ b/ui/score.go @@ -24,6 +24,7 @@ import ( "fmt" "os" "github.com/MikunoNaka/macli/mal" + "github.com/MikunoNaka/macli/util" // m "github.com/MikunoNaka/MAL2Go/v2/manga" p "github.com/manifoldco/promptui" ) @@ -70,9 +71,16 @@ func CreateScoreUpdateConfirmationMessage(title string, prevScore, score int) st func ScoreInput(id, currentScore int, title string, isManga bool) { validate := func(input string) error { i, err := strconv.ParseFloat(input, 64) - if err != nil || i < 0 || i > 10 { + 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 } @@ -94,17 +102,13 @@ func ScoreInput(id, currentScore int, title string, isManga bool) { os.Exit(1) } - score, err := strconv.Atoi(res) - if err != nil { - fmt.Println("Error while parsing score input:", err) - } - var newScore int + parsedScore := util.ParseNumeric(res, currentScore) if isManga { - resp := mal.SetMangaScore(id, score) + resp := mal.SetMangaScore(id, parsedScore) newScore = resp.Score } else { - resp := mal.SetAnimeScore(id, score) + resp := mal.SetAnimeScore(id, parsedScore) newScore = resp.Score } |