diff options
-rw-r--r-- | cmd/episodes.go | 68 | ||||
-rw-r--r-- | mal/episodes.go | 33 | ||||
-rw-r--r-- | ui/episodes.go | 12 |
3 files changed, 101 insertions, 12 deletions
diff --git a/cmd/episodes.go b/cmd/episodes.go new file mode 100644 index 0000000..5c8b6e0 --- /dev/null +++ b/cmd/episodes.go @@ -0,0 +1,68 @@ +/* +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 cmd + +import ( + "fmt" + "strings" + "github.com/MikunoNaka/macli/ui" + "github.com/MikunoNaka/macli/mal" + + "github.com/spf13/cobra" +) + +// statusCmd represents the status command +var episodesCmd = &cobra.Command{ + Use: "episodes", + Short: "Set an anime/manga's status", + Long: "Set an anime's status\n" + + "\n" + + "Example Usage:\n" + + " - \x1b[33m`macli status <anime-name>`\x1b[0m For interactive prompt (anime-name can be omitted)\n" + + " - \x1b[33m`macli status -s \x1b[34mwatching|plan_to_watch|dropped|on_hold|completed\x1b[33m <anime-name>`\x1b[0m to specify status from command\n", + Run: func(cmd *cobra.Command, args []string) { + searchInput := strings.Join(args, " ") + if searchInput == "" { + searchInput = ui.TextInput("Search Anime To Set Episodes For: ", "Search can't be blank.") + } + + epInput, err := cmd.Flags().GetString("set-value") + if err != nil { + fmt.Println("Error while reading --set-value flag.", err.Error()) + } + + anime := ui.AnimeSearch("Select Anime:", searchInput) + animeData := mal.GetAnimeData(anime.Id, []string{"my_list_status"}) + prevEpWatched := animeData.MyListStatus.EpWatched + + if epInput == "" { + ui.EpisodeInput(anime) + } else { + resp := mal.SetEpisodes(anime.Id, prevEpWatched, epInput) + fmt.Println(ui.CreateEpisodeUpdateConfirmationMessage(anime.Title, prevEpWatched, resp.EpWatched)) + } + + + }, +} + +func init() { + rootCmd.AddCommand(episodesCmd) + episodesCmd.Flags().StringP("set-value", "s", "", "status to be set") +} diff --git a/mal/episodes.go b/mal/episodes.go index 255f8b0..c0ac116 100644 --- a/mal/episodes.go +++ b/mal/episodes.go @@ -22,26 +22,36 @@ import ( "fmt" "os" "strconv" + a "github.com/MikunoNaka/MAL2Go/v2/user/anime" + m "github.com/MikunoNaka/MAL2Go/v2/user/manga" ) -func SetEpisodes(animeId int, ep string) { - epValue, err := strconv.Atoi(ep) +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) } - sign := ep[0:1] - if sign == "+" || sign == "-" { - fmt.Printf("Cannot increment/decrement watched episodes by %d\n. Currently that doesn't wokr", epValue) - os.Exit(2) + var epValue int + switch ep[0:1] { + case "+", "-": + // works both for increment and decrement + epValue = prevValue + epInt + default: + epValue = epInt } - userAnimeClient.SetWatchedEpisodes(animeId, epValue) + res, err := userAnimeClient.SetWatchedEpisodes(animeId, epValue) + if err != nil { + fmt.Println("MyAnimeList returned error while updating episodes:", err) + os.Exit(1) + } + return res } -func SetChapters(mangaId int, ch string) { +func SetChapters(mangaId, prevValue int, ch string) m.UpdateResponse { chValue, err := strconv.Atoi(ch) if err != nil { fmt.Println("Error while parsing chapter input", err) @@ -54,5 +64,10 @@ func SetChapters(mangaId int, ch string) { os.Exit(2) } - userMangaClient.SetChaptersRead(mangaId, chValue) + res, err := userMangaClient.SetChaptersRead(mangaId, chValue) + if err != nil { + fmt.Println("MyAnimeList returned error while updating chapters:", err) + os.Exit(1) + } + return res } diff --git a/ui/episodes.go b/ui/episodes.go index 277d52f..748482b 100644 --- a/ui/episodes.go +++ b/ui/episodes.go @@ -29,6 +29,12 @@ import ( p "github.com/manifoldco/promptui" ) +// very short name I know +func CreateEpisodeUpdateConfirmationMessage(title string, prevEpNum, epNum int) string { + return fmt.Sprintf("Set Episodes Watched for \x1b[35m%s\x1b[0m from \x1b[1;36m%d\x1b[0m to \x1b[1;33m%d\x1b[0m.", title, prevEpNum, epNum) +} + + func EpisodeInput(anime a.Anime) { // fetch number of total episodes, number of watched episodes from the API animeData := mal.GetAnimeData(anime.Id, []string{"num_episodes", "my_list_status"}) @@ -60,8 +66,8 @@ func EpisodeInput(anime a.Anime) { os.Exit(1) } - // TODO: read resp and show confirmation message - mal.SetEpisodes(anime.Id, res) + resp := mal.SetEpisodes(anime.Id, epWatchedNum, res) + fmt.Println(CreateEpisodeUpdateConfirmationMessage(anime.Title, epWatchedNum, resp.EpWatched)) } func ChapterInput(manga m.Manga) { @@ -96,5 +102,5 @@ func ChapterInput(manga m.Manga) { } // TODO: read resp and show confirmation message - mal.SetChapters(manga.Id, res) + mal.SetChapters(manga.Id, chReadNum, res) } |