From 2689541cbbba044153c33fb1b36d691514916139 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Mon, 4 Jul 2022 22:18:49 +0530 Subject: added --id flag to search and episodes and chapters command --- cmd/chapters.go | 22 +++++++++++++++------- cmd/episodes.go | 24 +++++++++++++++--------- cmd/status.go | 3 +-- ui/episodes.go | 18 ++++++++++++------ 4 files changed, 43 insertions(+), 24 deletions(-) diff --git a/cmd/chapters.go b/cmd/chapters.go index 87d887b..220953e 100644 --- a/cmd/chapters.go +++ b/cmd/chapters.go @@ -24,6 +24,7 @@ import ( "strings" "github.com/MikunoNaka/macli/ui" "github.com/MikunoNaka/macli/mal" + m "github.com/MikunoNaka/MAL2Go/v3/manga" "github.com/spf13/cobra" ) @@ -41,8 +42,13 @@ var chaptersCmd = &cobra.Command{ " - \x1b[33m`macli chapters -s -2 `\x1b[0m to decrement the chapters by 2\n", Run: func(cmd *cobra.Command, args []string) { mal.Init() + var selectedManga m.Manga + if entryId > 0 { + selectedManga = mal.GetMangaData(entryId, []string{"my_list_status", "num_chapters"}) + } + searchInput := strings.Join(args, " ") - if searchInput == "" { + if searchInput == "" && entryId < 1 { var promptText string if queryOnlyMode { promptText = "Search Manga to Get Amount of Chapters Read For: " @@ -64,20 +70,21 @@ var chaptersCmd = &cobra.Command{ } } - manga := ui.MangaSearch("Select Manga:", searchInput) - selectedManga := mal.GetMangaData(manga.Id, []string{"my_list_status", "num_chapters"}) - prevChRead := selectedManga.MyListStatus.ChaptersRead + if entryId < 1 { + manga := ui.MangaSearch("Select Manga:", searchInput) + selectedManga = mal.GetMangaData(manga.Id, []string{"my_list_status", "num_chapters"}) + } if queryOnlyMode { - fmt.Printf("\x1b[35m%s\x1b[0m Chapters :: \x1b[1;36m%d\x1b[0m / \x1b[1;33m%d\x1b[0m Read\n", manga.Title, prevChRead, selectedManga.NumChapters) + fmt.Printf("%s / \x1b[1;33m%d\n", ui.CreateChapterUpdateConfirmationMessage(selectedManga.Title, selectedManga.MyListStatus.ChaptersRead, -1), selectedManga.NumChapters) os.Exit(0) } if chInput == "" { ui.ChapterInput(selectedManga) } else { - resp := mal.SetChapters(manga.Id, prevChRead, chInput) - fmt.Println(ui.CreateChapterUpdateConfirmationMessage(manga.Title, prevChRead, resp.ChaptersRead)) + resp := mal.SetChapters(selectedManga.Id, selectedManga.MyListStatus.ChaptersRead, chInput) + fmt.Println(ui.CreateChapterUpdateConfirmationMessage(selectedManga.Title, resp.ChaptersRead, selectedManga.MyListStatus.ChaptersRead)) } }, } @@ -90,4 +97,5 @@ func init() { chaptersCmd.Flags().IntVarP(&mal.SearchOffset, "search-offset", "o", 0, "Offset for the search results") chaptersCmd.Flags().BoolVarP(&mal.SearchNSFW, "search-nsfw", "", false, "Include NSFW-rated items in search results") chaptersCmd.Flags().BoolVarP(&queryOnlyMode, "query", "q", false, "Query only (don't update data)") + chaptersCmd.Flags().IntVarP(&entryId, "id", "i", -1, "Manually specify the ID of anime/manga (overrides search)") } diff --git a/cmd/episodes.go b/cmd/episodes.go index 0746712..13dd4ab 100644 --- a/cmd/episodes.go +++ b/cmd/episodes.go @@ -24,6 +24,7 @@ import ( "strings" "github.com/MikunoNaka/macli/ui" "github.com/MikunoNaka/macli/mal" + a "github.com/MikunoNaka/MAL2Go/v3/anime" "github.com/spf13/cobra" ) @@ -41,8 +42,13 @@ var episodesCmd = &cobra.Command{ " - \x1b[33m`macli episodes -s -2 `\x1b[0m to decrement the episodes by 2\n", Run: func(cmd *cobra.Command, args []string) { mal.Init() + var selectedAnime a.Anime + if entryId > 0 { + selectedAnime = mal.GetAnimeData(entryId, []string{"my_list_status", "num_episodes"}) + } + searchInput := strings.Join(args, " ") - if searchInput == "" { + if searchInput == "" && entryId < 1 { var promptText string if queryOnlyMode { promptText = "Search Anime to Get Amount of Episodes Watched for: " @@ -64,23 +70,22 @@ var episodesCmd = &cobra.Command{ } } - anime := ui.AnimeSearch("Select Anime:", searchInput) - selectedAnime := mal.GetAnimeData(anime.Id, []string{"my_list_status", "num_episodes"}) - prevEpWatched := selectedAnime.MyListStatus.EpWatched + if entryId < 1 { + anime := ui.AnimeSearch("Select Anime:", searchInput) + selectedAnime = mal.GetAnimeData(anime.Id, []string{"my_list_status", "num_episodes"}) + } if queryOnlyMode { - fmt.Printf("\x1b[35m%s\x1b[0m Episodes :: \x1b[1;36m%d\x1b[0m / \x1b[1;33m%d\x1b[0m Watched\n", anime.Title, prevEpWatched, selectedAnime.NumEpisodes) + fmt.Printf("%s / \x1b[1;33m%d\n", ui.CreateEpisodeUpdateConfirmationMessage(selectedAnime.Title, selectedAnime.MyListStatus.EpWatched, -1), selectedAnime.NumEpisodes) os.Exit(0) } if epInput == "" { ui.EpisodeInput(selectedAnime) } else { - resp := mal.SetEpisodes(anime.Id, prevEpWatched, epInput) - fmt.Println(ui.CreateEpisodeUpdateConfirmationMessage(anime.Title, prevEpWatched, resp.EpWatched)) + resp := mal.SetEpisodes(selectedAnime.Id, selectedAnime.MyListStatus.EpWatched, epInput) + fmt.Println(ui.CreateEpisodeUpdateConfirmationMessage(selectedAnime.Title, resp.EpWatched, selectedAnime.MyListStatus.EpWatched)) } - - }, } @@ -92,4 +97,5 @@ func init() { episodesCmd.Flags().IntVarP(&mal.SearchLength, "search-length", "n", 10, "Amount of search results to load") episodesCmd.Flags().BoolVarP(&mal.SearchNSFW, "search-nsfw", "", false, "Include NSFW-rated items in search results") episodesCmd.Flags().IntVarP(&mal.SearchOffset, "search-offset", "o", 0, "Offset for the search results") + episodesCmd.Flags().IntVarP(&entryId, "id", "i", -1, "Manually specify the ID of anime/manga (overrides search)") } diff --git a/cmd/status.go b/cmd/status.go index ba5d047..1e80c7b 100644 --- a/cmd/status.go +++ b/cmd/status.go @@ -128,8 +128,7 @@ func setMangaStatus(statusInput, searchInput string) { ui.MangaStatusMenu(selectedManga) } else { resp := mal.SetMangaStatus(selectedManga.Id, statusInput) - fmt.Println(resp.Status) - fmt.Println(ui.CreateStatusUpdateConfirmationMessage(selectedManga.Title, resp.Status, selectedManga.MyListStatus.Status)) + fmt.Println(ui.CreateStatusUpdateConfirmationMessage(selectedManga.Title, selectedManga.MyListStatus.Status, resp.Status)) } } diff --git a/ui/episodes.go b/ui/episodes.go index 21c0078..04ff53e 100644 --- a/ui/episodes.go +++ b/ui/episodes.go @@ -30,12 +30,18 @@ import ( ) // very short name I know -func CreateEpisodeUpdateConfirmationMessage(title string, prevEpNum, epNum int) string { - return fmt.Sprintf("\x1b[35m%s\x1b[0m Episodes Watched :: \x1b[1;33m%d\x1b[0m -> \x1b[1;36m%d\x1b[0m", title, prevEpNum, epNum) +func CreateEpisodeUpdateConfirmationMessage(title string, epNum, prevEpNum int) string { + if prevEpNum >= 0 { + return fmt.Sprintf("\x1b[35m%s\x1b[0m Episodes Watched :: \x1b[1;33m%d\x1b[0m -> \x1b[1;36m%d\x1b[0m", title, prevEpNum, epNum) + } + return fmt.Sprintf("\x1b[35m%s\x1b[0m Episodes Watched :: \x1b[1;36m%d\x1b[0m", title, epNum) } -func CreateChapterUpdateConfirmationMessage(title string, prevChNum, chNum int) string { - return fmt.Sprintf("\x1b[35m%s\x1b[0m Chapters Read :: \x1b[1;33m%d\x1b[0m -> \x1b[1;36m%d\x1b[0m", title, prevChNum, chNum) +func CreateChapterUpdateConfirmationMessage(title string, chNum, prevChNum int) string { + if prevChNum >= 0 { + return fmt.Sprintf("\x1b[35m%s\x1b[0m Chapters Read :: \x1b[1;33m%d\x1b[0m -> \x1b[1;36m%d\x1b[0m", title, prevChNum, chNum) + } + return fmt.Sprintf("\x1b[35m%s\x1b[0m Chapters Read :: \x1b[1;36m%d\x1b[0m", title, chNum) } func EpisodeInput(anime a.Anime) { @@ -68,7 +74,7 @@ func EpisodeInput(anime a.Anime) { } resp := mal.SetEpisodes(anime.Id, epWatchedNum, res) - fmt.Println(CreateEpisodeUpdateConfirmationMessage(anime.Title, epWatchedNum, resp.EpWatched)) + fmt.Println(CreateEpisodeUpdateConfirmationMessage(anime.Title, resp.EpWatched, epWatchedNum)) } func ChapterInput(manga m.Manga) { @@ -101,5 +107,5 @@ func ChapterInput(manga m.Manga) { } resp := mal.SetChapters(manga.Id, chReadNum, res) - fmt.Println(CreateChapterUpdateConfirmationMessage(manga.Title, chReadNum, resp.ChaptersRead)) + fmt.Println(CreateEpisodeUpdateConfirmationMessage(manga.Title, resp.ChaptersRead, chReadNum)) } -- cgit v1.2.3