diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-06-15 12:59:22 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.xyz> | 2022-06-15 12:59:22 +0530 |
commit | ab3db8a4ca89293ce0928177e8845d622f13755f (patch) | |
tree | e5331856b39b43cfea422f806aa046b0925e92e4 /ui | |
parent | 5eb7a3cd41826fccdc432d049dd85a31b4b56073 (diff) |
added pretty template for TextInput
Diffstat (limited to 'ui')
-rw-r--r-- | ui/actions.go | 83 | ||||
-rw-r--r-- | ui/episodes.go | 37 | ||||
-rw-r--r-- | ui/input.go | 37 | ||||
-rw-r--r-- | ui/search.go | 49 | ||||
-rw-r--r-- | ui/status.go | 65 |
5 files changed, 249 insertions, 22 deletions
diff --git a/ui/actions.go b/ui/actions.go index 7db5a96..cc9e391 100644 --- a/ui/actions.go +++ b/ui/actions.go @@ -24,32 +24,97 @@ import ( "os" p "github.com/manifoldco/promptui" a "github.com/MikunoNaka/MAL2Go/anime" + m "github.com/MikunoNaka/MAL2Go/manga" ) -type Action struct { +type AnimeAction struct { Label string Description string Method func(a.Anime) } -// only search animes probably only now -func ActionMenu(animeIsAdded bool) func(a.Anime) { +type MangaAction struct { + Label string + Description string + Method func(m.Manga) +} + +func AnimeActionMenu(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}, + options := []AnimeAction { + {"Set Status", "Set status for an anime (watching, dropped, etc)", AnimeStatusMenu}, {"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}, + // these only temporarily run AnimeStatusMenu + // because that functionality doesnt exist yet + {"Set Score", "Set score", AnimeStatusMenu}, + {"Set Re-watching", "Set if re-watching", AnimeStatusMenu}, + {"Set Times Re-watched", "Set number of times re-watched", AnimeStatusMenu}, } // if anime not in list if animeIsAdded { options = append( options, - Action{"Delete Anime", "Delete Anime From Your MyAnimeList List.", StatusMenu}, + AnimeAction{"Delete Anime", "Delete Anime From Your MyAnimeList List.", AnimeStatusMenu}, + ) + } + + template := &p.SelectTemplates { + Label: "{{ .Label }}", + Active: "{{ .Label | magenta }} {{ .Description | faint }}", + Inactive: "{{ .Label }}", + Selected: "{{ .Label | magenta }}", + Details: ` +------------------- +{{ .Description }} +`, + } + + // returns true if input == anime title + searcher := func(input string, index int) bool { + action := strings.Replace(strings.ToLower(options[index].Label), " ", "", -1) + input = strings.Replace(strings.ToLower(input), " ", "", -1) + return strings.Contains(action, input) + } + + prompt := p.Select { + Label: "Select Action: ", + Items: options, + Templates: template, + Searcher: searcher, + Size: promptLength, + } + + res, _, err := prompt.Run() + if err != nil { + fmt.Println("Error running actions menu.", err.Error()) + os.Exit(1) + } + + return options[res].Method +} + +func MangaActionMenu(mangaIsAdded bool) func(m.Manga) { + // TODO: load promptLength from config + promptLength := 5 + + options := []MangaAction { + {"Set Status", "Set status for a manga (reading, dropped, etc)", MangaStatusMenu}, + {"Set Chapters", "Set number of chapters read", ChapterInput}, + // these only temporarily run MangaStatusMenu + // because that functionality doesnt exist yet + {"Set Score", "Set score", MangaStatusMenu}, + {"Set Re-reading", "Set if re-reading", MangaStatusMenu}, + {"Set Times Re-read", "Set number of times re-read", MangaStatusMenu}, + } + + // if manga not in list + if mangaIsAdded { + options = append( + options, + MangaAction{"Delete Manga", "Delete Manga From Your MyAnimeList List.", MangaStatusMenu}, ) } diff --git a/ui/episodes.go b/ui/episodes.go index 53c1cb9..d4c3791 100644 --- a/ui/episodes.go +++ b/ui/episodes.go @@ -25,7 +25,7 @@ import ( "errors" "github.com/MikunoNaka/macli/mal" a "github.com/MikunoNaka/MAL2Go/anime" - // m "github.com/MikunoNaka/MAL2Go/manga" + m "github.com/MikunoNaka/MAL2Go/manga" p "github.com/manifoldco/promptui" ) @@ -63,3 +63,38 @@ func EpisodeInput(anime a.Anime) { mal.SetEpisodes(anime.Id, res) } + +func ChapterInput(manga m.Manga) { + validate := func(input string) error { + if _, err := strconv.ParseFloat(input, 64); err != nil { + return errors.New("Input must be a number.") + } + return nil + } + + template := &p.PromptTemplates { + Valid: "\x1b[0m{{ . | magenta }}", + Invalid: "\x1b[0m{{ . | magenta }}\x1b[31m ", + Success: "{{ . | cyan }}", + } + + prompt := p.Prompt { + Label: "Set Chapter Number: ", + Templates: template, + Validate: validate, + } + + // print current chapter number if any + chNum := manga.MyListStatus.ChaptersRead + if chNum != 0 { + fmt.Printf("\x1b[33mYou currently have read %d chapters.\n\x1b[0m", chNum) + } + + res, err := prompt.Run() + if err != nil { + fmt.Println("Error Running chapter input Prompt.", err.Error()) + os.Exit(1) + } + + mal.SetChapters(manga.Id, res) +} diff --git a/ui/input.go b/ui/input.go index 0aa2f06..a334943 100644 --- a/ui/input.go +++ b/ui/input.go @@ -33,14 +33,49 @@ func TextInput(label, errMessage string) string { return nil } + template := &p.PromptTemplates { + Valid: "\x1b[0m{{ . | magenta }}", + Invalid: "\x1b[0m{{ . | magenta }}\x1b[31m", + Success: "{{ . | cyan }}", + } + + prompt := p.Prompt { + Label: label, + Validate: validate, + Templates: template,} + res, err := prompt.Run() + if err != nil { + fmt.Println("Failed to run input prompt.", err.Error()) + os.Exit(1) + } + + return res +} + +func PasswordInput(label, errMessage string) string { + validate := func(input string) error { + if input == "" { + return errors.New(errMessage) + } + return nil + } + + template := &p.PromptTemplates { + Valid: "{{ . | cyan }}", + Invalid: "{{ . | cyan }}", + Success: "{{ . | blue }}", + } + prompt := p.Prompt { Label: label, + Templates: template, Validate: validate, + Mask: '*', } res, err := prompt.Run() if err != nil { - fmt.Println("Failed to run TextInput Prompt.", err.Error()) + fmt.Println("Failed to run input prompt.", err.Error()) os.Exit(1) } diff --git a/ui/search.go b/ui/search.go index c02ae22..911add5 100644 --- a/ui/search.go +++ b/ui/search.go @@ -25,15 +25,14 @@ import ( p "github.com/manifoldco/promptui" mal "github.com/MikunoNaka/macli/mal" a "github.com/MikunoNaka/MAL2Go/anime" + m "github.com/MikunoNaka/MAL2Go/manga" ) // only search animes probably only now func AnimeSearch(label, searchString string) a.Anime { // TODO: load promptLength from config promptLength := 5 - - extraFields := []string{"my_list_status"} - animes := mal.SearchAnime(searchString, extraFields) + animes := mal.SearchAnime(searchString) template := &p.SelectTemplates { Label: "{{ . }}", @@ -61,13 +60,51 @@ More Details To Be Added Later Size: promptLength, } - var anime a.Anime animeIndex, _, err := prompt.Run() if err != nil { fmt.Println("Error running search menu.", err.Error()) os.Exit(1) } - anime = animes[animeIndex] - return anime + return animes[animeIndex] +} + +func MangaSearch(label, searchString string) m.Manga { + // TODO: load promptLength from config + promptLength := 5 + mangas := mal.SearchManga(searchString) + + template := &p.SelectTemplates { + Label: "{{ . }}", + Active: "{{ .Title | magenta }}", + Inactive: "{{ .Title }}", + Selected: "{{ .Title | blue }}", + Details: ` +--------- {{ .Title }} ---------- +More Details To Be Added Later +`, + } + + // returns true if input == anime title + searcher := func(input string, index int) bool { + title := strings.Replace(strings.ToLower(mangas[index].Title), " ", "", -1) + input = strings.Replace(strings.ToLower(input), " ", "", -1) + return strings.Contains(title, input) + } + + prompt := p.Select { + Label: label, + Items: mangas, + Templates: template, + Searcher: searcher, + Size: promptLength, + } + + mangaIndex, _, err := prompt.Run() + if err != nil { + fmt.Println("Error running search menu.", err.Error()) + os.Exit(1) + } + + return mangas[mangaIndex] } diff --git a/ui/status.go b/ui/status.go index 661abe1..11fe5d8 100644 --- a/ui/status.go +++ b/ui/status.go @@ -24,6 +24,7 @@ import ( "os" "github.com/MikunoNaka/macli/mal" a "github.com/MikunoNaka/MAL2Go/anime" + m "github.com/MikunoNaka/MAL2Go/manga" p "github.com/manifoldco/promptui" ) @@ -32,8 +33,7 @@ type StatusOption struct { Status string } -// only search animes probably only now -func StatusMenu(anime a.Anime) { +func AnimeStatusMenu(anime a.Anime) { options := []StatusOption { {"Watching", "watching"}, {"Completed", "completed"}, @@ -67,8 +67,63 @@ func StatusMenu(anime a.Anime) { } promptLabel := "Set Status: " - if anime.MyListStatus.Status != "" { - promptLabel = promptLabel + "(current - " + anime.MyListStatus.Status + ")" + if animeStatus != "" { + promptLabel = promptLabel + "(current - " + animeStatus + ")" + } + + prompt := p.Select { + Label: promptLabel, + Items: options, + Templates: template, + Searcher: searcher, + Size: 5, + } + + res, _, err := prompt.Run() + if err != nil { + fmt.Println("Error running status prompt.", err.Error()) + os.Exit(1) + } + + mal.SetAnimeStatus(anime.Id, options[res].Status) +} + +func MangaStatusMenu(manga m.Manga) { + options := []StatusOption { + {"Reading", "reading"}, + {"Completed", "completed"}, + {"On Hold", "on_hold"}, + {"Dropped", "dropped"}, + {"Plan to Read", "plan_to_read"}, + } + + // highlight current status (if any) + mangaStatus := manga.MyListStatus.Status + if mangaStatus != "" { + for i := range options { + if options[i].Status == mangaStatus { + options[i].Label = options[i].Label + " \x1b[35m\U00002714\x1b[0m" + } + } + } + + template := &p.SelectTemplates { + Label: "{{ .Label }}", + Active: "{{ .Label | magenta }}", + Inactive: "{{ .Label }}", + Selected: "{{ .Label | cyan }}", + } + + // returns true if input == anime title + searcher := func(input string, index int) bool { + status := strings.Replace(strings.ToLower(options[index].Label), " ", "", -1) + input = strings.Replace(strings.ToLower(input), " ", "", -1) + return strings.Contains(status, input) + } + + promptLabel := "Set Status: " + if mangaStatus != "" { + promptLabel = promptLabel + "(current - " + mangaStatus + ")" } prompt := p.Select { @@ -85,5 +140,5 @@ func StatusMenu(anime a.Anime) { os.Exit(1) } - mal.SetStatus(anime.Id, options[res].Status) + mal.SetMangaStatus(manga.Id, options[res].Status) } |