From 028632de277704fe4576e732d4997daa70f25f60 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Wed, 15 Jun 2022 00:08:39 +0530 Subject: added color coding and error handling + other visual appeal --- ui/actions.go | 25 +++++++++++++++++-------- ui/episodes.go | 58 ++++++++++++++++++++-------------------------------------- ui/input.go | 6 ++++-- ui/search.go | 19 ++++++++++++------- ui/status.go | 31 ++++++++++++++++++++++++------- 5 files changed, 77 insertions(+), 62 deletions(-) (limited to 'ui') diff --git a/ui/actions.go b/ui/actions.go index 77afae7..7db5a96 100644 --- a/ui/actions.go +++ b/ui/actions.go @@ -20,35 +20,44 @@ package ui import ( "strings" - "log" + "fmt" + "os" p "github.com/manifoldco/promptui" - // mal "github.com/MikunoNaka/macli/mal" + a "github.com/MikunoNaka/MAL2Go/anime" ) type Action struct { Label string Description string - Method func(int) + Method func(a.Anime) } // only search animes probably only now -func ActionMenu() func(animeId int) { +func ActionMenu(animeIsAdded bool) func(a.Anime) { // TODO: load promptLength from config promptLength := 5 options := []Action { {"Set Status", "Set status for an anime (watching, dropped, etc)", StatusMenu}, - {"Set Episodes", "Set number of episodes watched", StatusMenu}, + {"Set Episodes", "Set number of episodes watched", EpisodeInput}, {"Set Score", "Set score", StatusMenu}, {"Set Rewatching", "Set if rewatching", StatusMenu}, {"Set Times Rewatched", "Set number of times rewatched", StatusMenu}, } + // if anime not in list + if animeIsAdded { + options = append( + options, + Action{"Delete Anime", "Delete Anime From Your MyAnimeList List.", StatusMenu}, + ) + } + template := &p.SelectTemplates { Label: "{{ .Label }}", Active: "{{ .Label | magenta }} {{ .Description | faint }}", Inactive: "{{ .Label }}", - Selected: "{{ .Label }}", + Selected: "{{ .Label | magenta }}", Details: ` ------------------- {{ .Description }} @@ -72,8 +81,8 @@ func ActionMenu() func(animeId int) { res, _, err := prompt.Run() if err != nil { - log.Println(err) - return nil + fmt.Println("Error running actions menu.", err.Error()) + os.Exit(1) } return options[res].Method diff --git a/ui/episodes.go b/ui/episodes.go index a2413bd..53c1cb9 100644 --- a/ui/episodes.go +++ b/ui/episodes.go @@ -20,64 +20,46 @@ package ui import ( "strconv" - "log" + "fmt" + "os" + "errors" "github.com/MikunoNaka/macli/mal" + a "github.com/MikunoNaka/MAL2Go/anime" + // m "github.com/MikunoNaka/MAL2Go/manga" p "github.com/manifoldco/promptui" ) -func EpisodeInput(animeId int) { +func EpisodeInput(anime a.Anime) { validate := func(input string) error { - _, err := strconv.ParseFloat(input, 64) - return err + if _, err := strconv.ParseFloat(input, 64); err != nil { + return errors.New("Input must be a number.") + } + return nil } template := &p.PromptTemplates { - Prompt: "{{ . }} ", - Valid: "{{ . }} ", - Invalid: "{{ . | red }} ", - Success: "{{ . }} ", + Valid: "\x1b[0m{{ . | magenta }}", + Invalid: "\x1b[0m{{ . | magenta }}\x1b[31m ", + Success: "{{ . | cyan }}", } prompt := p.Prompt { - // Label: "Set Episode Number: (Increment/Decrement With +1, -2, etc)", Label: "Set Episode Number: ", Templates: template, Validate: validate, } - res, err := prompt.Run() - if err != nil { - log.Println("Error Running EpisodeInput Prompt.", err) - return - } - - mal.SetEpisodes(animeId, res) -} - -func ChapterInput(mangaId int) { - validate := func(input string) error { - _, err := strconv.ParseFloat(input, 64) - return err - } - - template := &p.PromptTemplates { - Prompt: "{{ . }} ", - Valid: "{{ . }} ", - Invalid: "{{ . | red }} ", - Success: "{{ . }} ", - } - - prompt := p.Prompt { - Label: "Set Chapter Number: (Increment/Decrement With +1, -2, etc)", - Templates: template, - Validate: validate, + // print current episode number if any + epNum := anime.MyListStatus.EpWatched + if epNum != 0 { + fmt.Printf("\x1b[33mYou currently have watched %d episodes.\n\x1b[0m", epNum) } res, err := prompt.Run() if err != nil { - log.Println("Error Running ChapterInput Prompt.", err) - return + fmt.Println("Error Running episode input Prompt.", err.Error()) + os.Exit(1) } - mal.SetChapters(mangaId, res) + mal.SetEpisodes(anime.Id, res) } diff --git a/ui/input.go b/ui/input.go index bd7306b..0aa2f06 100644 --- a/ui/input.go +++ b/ui/input.go @@ -20,7 +20,8 @@ package ui import ( "errors" - "log" + "fmt" + "os" p "github.com/manifoldco/promptui" ) @@ -39,7 +40,8 @@ func TextInput(label, errMessage string) string { res, err := prompt.Run() if err != nil { - log.Fatal("Failed to run TextInput Prompt.") + fmt.Println("Failed to run TextInput Prompt.", err.Error()) + os.Exit(1) } return res diff --git a/ui/search.go b/ui/search.go index ee25604..c02ae22 100644 --- a/ui/search.go +++ b/ui/search.go @@ -20,23 +20,26 @@ package ui import ( "strings" - "log" + "fmt" + "os" p "github.com/manifoldco/promptui" mal "github.com/MikunoNaka/macli/mal" + a "github.com/MikunoNaka/MAL2Go/anime" ) // only search animes probably only now -func SearchAndGetID(label, searchString string) int { +func AnimeSearch(label, searchString string) a.Anime { // TODO: load promptLength from config promptLength := 5 - animes := mal.SearchAnime(searchString) + extraFields := []string{"my_list_status"} + animes := mal.SearchAnime(searchString, extraFields) template := &p.SelectTemplates { Label: "{{ . }}", Active: "{{ .Title | magenta }}", Inactive: "{{ .Title }}", - Selected: "{{ .Title }}", + Selected: "{{ .Title | blue }}", Details: ` --------- {{ .Title }} ---------- More Details To Be Added Later @@ -58,11 +61,13 @@ More Details To Be Added Later Size: promptLength, } + var anime a.Anime animeIndex, _, err := prompt.Run() if err != nil { - log.Println(err) - return 0 + fmt.Println("Error running search menu.", err.Error()) + os.Exit(1) } - return animes[animeIndex].Id + anime = animes[animeIndex] + return anime } diff --git a/ui/status.go b/ui/status.go index 762bfd8..661abe1 100644 --- a/ui/status.go +++ b/ui/status.go @@ -20,8 +20,10 @@ package ui import ( "strings" - "log" + "fmt" + "os" "github.com/MikunoNaka/macli/mal" + a "github.com/MikunoNaka/MAL2Go/anime" p "github.com/manifoldco/promptui" ) @@ -31,7 +33,7 @@ type StatusOption struct { } // only search animes probably only now -func StatusMenu(animeId int) { +func StatusMenu(anime a.Anime) { options := []StatusOption { {"Watching", "watching"}, {"Completed", "completed"}, @@ -40,11 +42,21 @@ func StatusMenu(animeId int) { {"Plan to Watch", "plan_to_watch"}, } + // highlight current status (if any) + animeStatus := anime.MyListStatus.Status + if animeStatus != "" { + for i := range options { + if options[i].Status == animeStatus { + options[i].Label = options[i].Label + " \x1b[35m\U00002714\x1b[0m" + } + } + } + template := &p.SelectTemplates { Label: "{{ .Label }}", Active: "{{ .Label | magenta }}", Inactive: "{{ .Label }}", - Selected: "{{ .Label }}", + Selected: "{{ .Label | cyan }}", } // returns true if input == anime title @@ -54,8 +66,13 @@ func StatusMenu(animeId int) { return strings.Contains(status, input) } + promptLabel := "Set Status: " + if anime.MyListStatus.Status != "" { + promptLabel = promptLabel + "(current - " + anime.MyListStatus.Status + ")" + } + prompt := p.Select { - Label: "Set Status:", + Label: promptLabel, Items: options, Templates: template, Searcher: searcher, @@ -64,9 +81,9 @@ func StatusMenu(animeId int) { res, _, err := prompt.Run() if err != nil { - log.Println(err) - return + fmt.Println("Error running status prompt.", err.Error()) + os.Exit(1) } - mal.SetStatus(animeId, options[res].Status) + mal.SetStatus(anime.Id, options[res].Status) } -- cgit v1.2.3