aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-07-02 16:46:14 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.xyz>2022-07-02 16:46:14 +0530
commitfb7324295cf3f32c530135a0456f0afd14324d29 (patch)
tree1118db78fdf54f63fc8596bc41afee9b26ff4935
parent720045d9ea2e612ca0f0b7333ee62a73b2955a15 (diff)
incrementing/decrementing score now
-rw-r--r--cmd/score.go40
-rw-r--r--cmd/version.go2
-rw-r--r--mal/episodes.go36
-rw-r--r--ui/score.go20
-rw-r--r--util/number_parser.go41
5 files changed, 89 insertions, 50 deletions
diff --git a/cmd/score.go b/cmd/score.go
index f97de87..27106e5 100644
--- a/cmd/score.go
+++ b/cmd/score.go
@@ -22,8 +22,11 @@ import (
"fmt"
"os"
"strings"
+ "strconv"
+ "errors"
"github.com/MikunoNaka/macli/ui"
"github.com/MikunoNaka/macli/mal"
+ "github.com/MikunoNaka/macli/util"
"github.com/spf13/cobra"
)
@@ -41,7 +44,7 @@ var scoreCmd = &cobra.Command{
mal.Init()
searchInput := strings.Join(args, " ")
- scoreInput, err := cmd.Flags().GetInt("set-value")
+ scoreInput, err := cmd.Flags().GetString("set-value")
if err != nil {
fmt.Println("Error while reading \x1b[33m--set-value\x1b[0m flag.", err.Error())
os.Exit(1)
@@ -62,7 +65,18 @@ var scoreCmd = &cobra.Command{
},
}
-func setAnimeScore(scoreInput int, searchInput string) {
+func validateScore(input string, currentScore int) (int, error) {
+ parsedScore := util.ParseNumeric(input, currentScore)
+ if parsedScore > 10 {
+ return currentScore, errors.New("\x1b[31mScore out of range (" + strconv.Itoa(parsedScore) + " > 10)\x1b[0m")
+ }
+ if parsedScore < 0 {
+ return currentScore, errors.New("\x1b[31mScore out of range (" + strconv.Itoa(parsedScore) + " < 0)\x1b[0m")
+ }
+ return parsedScore, nil
+}
+
+func setAnimeScore(scoreInput, searchInput string) {
if searchInput == "" {
var promptText string
if queryOnlyMode {
@@ -82,15 +96,20 @@ func setAnimeScore(scoreInput int, searchInput string) {
os.Exit(0)
}
- if scoreInput < 0 {
+ if scoreInput == "" {
ui.ScoreInput(anime.Id, selectedAnime.MyListStatus.Score, anime.Title, false)
} else {
- resp := mal.SetAnimeScore(anime.Id, scoreInput)
+ score, err := validateScore(scoreInput, selectedAnime.MyListStatus.Score)
+ if err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+ resp := mal.SetAnimeScore(anime.Id, score)
fmt.Println(ui.CreateScoreUpdateConfirmationMessage(anime.Title, currentScore, resp.Score))
}
}
-func setMangaScore(scoreInput int, searchInput string) {
+func setMangaScore(scoreInput, searchInput string) {
if searchInput == "" {
var promptText string
if queryOnlyMode {
@@ -110,15 +129,20 @@ func setMangaScore(scoreInput int, searchInput string) {
os.Exit(0)
}
- if scoreInput < 0 {
+ if scoreInput == "" {
ui.ScoreInput(manga.Id, selectedManga.MyListStatus.Score, manga.Title, true)
} else {
- resp := mal.SetMangaScore(manga.Id, scoreInput)
+ score, err := validateScore(scoreInput, selectedManga.MyListStatus.Score)
+ if err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+ resp := mal.SetMangaScore(manga.Id, score)
fmt.Println(ui.CreateScoreUpdateConfirmationMessage(manga.Title, currentScore, resp.Score))
}
}
func init() {
rootCmd.AddCommand(scoreCmd)
- scoreCmd.Flags().IntP("set-value", "s", -1, "Score to be set")
+ scoreCmd.Flags().StringP("set-value", "s", "", "Score to be set")
}
diff --git a/cmd/version.go b/cmd/version.go
index 0901980..174b7e3 100644
--- a/cmd/version.go
+++ b/cmd/version.go
@@ -24,7 +24,7 @@ import (
"github.com/spf13/cobra"
)
-var version string = "v1.5.1"
+var version string = "v1.5.3"
var versionCmd = &cobra.Command {
Use: "version",
diff --git a/mal/episodes.go b/mal/episodes.go
index 06bb7a0..22dbae4 100644
--- a/mal/episodes.go
+++ b/mal/episodes.go
@@ -21,28 +21,13 @@ package mal
import (
"fmt"
"os"
- "strconv"
a "github.com/MikunoNaka/MAL2Go/v2/user/anime"
m "github.com/MikunoNaka/MAL2Go/v2/user/manga"
+ "github.com/MikunoNaka/macli/util"
)
func SetEpisodes(animeId, prevValue int, ep string) a.UpdateResponse {
- epInt, err := strconv.Atoi(ep)
- if err != nil {
- fmt.Println("Error while parsing episode input", err)
- os.Exit(1)
- }
-
- var epValue int
- switch ep[0:1] {
- case "+", "-":
- // works both for increment and decrement
- epValue = prevValue + epInt
- default:
- epValue = epInt
- }
-
- res, err := userAnimeClient.SetWatchedEpisodes(animeId, epValue)
+ res, err := userAnimeClient.SetWatchedEpisodes(animeId, util.ParseNumeric(ep, prevValue))
if err != nil {
fmt.Println("MyAnimeList returned error while updating episodes:", err)
os.Exit(1)
@@ -51,22 +36,7 @@ func SetEpisodes(animeId, prevValue int, ep string) a.UpdateResponse {
}
func SetChapters(mangaId, prevValue int, ch string) m.UpdateResponse {
- chInt, err := strconv.Atoi(ch)
- if err != nil {
- fmt.Println("Error while parsing chapter input", err)
- os.Exit(1)
- }
-
- var chValue int
- switch ch[0:1] {
- case "+", "-":
- // works both for increment and decrement
- chValue = prevValue + chInt
- default:
- chValue = chInt
- }
-
- res, err := userMangaClient.SetChaptersRead(mangaId, chValue)
+ res, err := userMangaClient.SetChaptersRead(mangaId, util.ParseNumeric(ch, prevValue))
if err != nil {
fmt.Println("MyAnimeList returned error while updating chapters:", err)
os.Exit(1)
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
}
diff --git a/util/number_parser.go b/util/number_parser.go
new file mode 100644
index 0000000..de46160
--- /dev/null
+++ b/util/number_parser.go
@@ -0,0 +1,41 @@
+/*
+macli - Unofficial CLI-Based MyAnimeList Client
+Copyright © 2022 Vidhu Kant Sharma <vidhukant@vidhukant.xyz>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+package util
+
+import (
+ "fmt"
+ "os"
+ "strconv"
+)
+
+func ParseNumeric(input string, prevValue int) int {
+ inputInt, err := strconv.Atoi(input)
+ if err != nil {
+ fmt.Println("Error while parsing input", err)
+ os.Exit(1)
+ }
+
+ switch input[0:1] {
+ case "+", "-":
+ // works both for increment and decrement
+ return prevValue + inputInt
+ default:
+ return inputInt
+ }
+}