From a870860921a61c50ef33fcd9435e5cce18e5b4e8 Mon Sep 17 00:00:00 2001 From: arimatakao <104305796+arimatakao@users.noreply.github.com> Date: Sat, 17 Aug 2024 22:17:43 +0300 Subject: [PATCH] Fix print in interactive mode --- README.md | 1 + app/meta.go | 2 +- go.mod | 1 + go.sum | 2 ++ internal/mdx/download.go | 35 +++++++++++++++++++++-------------- internal/mdx/print.go | 14 ++++++++++++++ 6 files changed, 40 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index fa43071..017fbfb 100644 --- a/README.md +++ b/README.md @@ -302,6 +302,7 @@ This project uses the following third-party libraries: - PTerm (https://github.com/pterm/pterm) - Licensed under the MIT - gopdf (https://github.com/signintech/gopdf) - Licensed under the MIT - go-epub (https://github.com/go-shiori/go-epub) - Licensed under the MIT +- consolesize-go (https://github.com/nathan-fiscaletti/consolesize-go) - Licensed under the MIT ## Contributors diff --git a/app/meta.go b/app/meta.go index eeb2702..0c9c32e 100644 --- a/app/meta.go +++ b/app/meta.go @@ -1,7 +1,7 @@ package app const ( - VERSION = "v1.11.0" + VERSION = "v1.11.1" API_VERSION = "v5.10.2" diff --git a/go.mod b/go.mod index da70728..4c4e495 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22.4 require ( github.com/go-resty/resty/v2 v2.13.1 github.com/go-shiori/go-epub v1.2.1 + github.com/nathan-fiscaletti/consolesize-go v0.0.0-20220204101620-317176b6684d github.com/pterm/pterm v0.12.79 github.com/signintech/gopdf v0.26.0 github.com/spf13/cobra v1.8.0 diff --git a/go.sum b/go.sum index 8e04caa..3fcc6e6 100644 --- a/go.sum +++ b/go.sum @@ -49,6 +49,8 @@ github.com/lithammer/fuzzysearch v1.1.8/go.mod h1:IdqeyBClc3FFqSzYq/MXESsS4S0FsZ github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/nathan-fiscaletti/consolesize-go v0.0.0-20220204101620-317176b6684d h1:NqRhLdNVlozULwM1B3VaHhcXYSgrOAv8V5BE65om+1Q= +github.com/nathan-fiscaletti/consolesize-go v0.0.0-20220204101620-317176b6684d/go.mod h1:cxIIfNMTwff8f/ZvRouvWYF6wOoO7nj99neWSx2q/Es= github.com/phpdave11/gofpdi v1.0.14-0.20211212211723-1f10f9844311 h1:zyWXQ6vu27ETMpYsEMAsisQ+GqJ4e1TPvSNfdOPF0no= github.com/phpdave11/gofpdi v1.0.14-0.20211212211723-1f10f9844311/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI= github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I= diff --git a/internal/mdx/download.go b/internal/mdx/download.go index 391ef88..f71ac51 100644 --- a/internal/mdx/download.go +++ b/internal/mdx/download.go @@ -305,8 +305,6 @@ func (p dlParam) DownloadAllChapters(mangaId string) { os.Exit(0) } - fmt.Print(p.chapters) - printShortMangaInfo(mangaInfo) if p.isMerge { p.downloadMergeChapters() @@ -319,12 +317,15 @@ const OPTION_MANGA_TEMPLATE = "%d | %s | %s" // numnber | author const OPTION_CHAPTER_TEMPLATE = "%d | vol. %s | ch. %s | %s" // number | volume | chapter | chapter title const OPTION_SAVING_TEMPLATE = "%d | %s" -func toMangaInfoOptions(m []mangadexapi.MangaInfo) ([]string, map[string]string) { +func toMangaInfoOptions(m []mangadexapi.MangaInfo, maxOptionSize int) ([]string, map[string]string) { printOptions := []string{} associationNums := make(map[string]string) for i, manga := range m { - printOptions = append(printOptions, fmt.Sprintf(OPTION_MANGA_TEMPLATE, - i+1, manga.Authors(), manga.Title("en"))) + option := fmt.Sprintf(OPTION_MANGA_TEMPLATE, i+1, manga.Authors(), manga.Title("en")) + if len(option)+2 >= maxOptionSize { + option = option[:maxOptionSize-2] + } + printOptions = append(printOptions, option) associationNums[strconv.Itoa(i+1)] = manga.ID } return printOptions, associationNums @@ -334,12 +335,16 @@ func getMangaNumOption(option string) string { return strings.Split(option, " | ")[0] } -func toChaptersOptions(c []mangadexapi.Chapter) ([]string, map[string]string) { +func toChaptersOptions(c []mangadexapi.Chapter, maxOptionSize int) ([]string, map[string]string) { options := []string{} associationNums := make(map[string]string) for i, chapter := range c { - options = append(options, fmt.Sprintf(OPTION_CHAPTER_TEMPLATE, - i+1, chapter.Volume(), chapter.Number(), chapter.Title())) + option := fmt.Sprintf(OPTION_CHAPTER_TEMPLATE, + i+1, chapter.Volume(), chapter.Number(), chapter.Title()) + if len(option)+6 >= maxOptionSize { + option = option[:maxOptionSize-6] + } + options = append(options, option) associationNums[strconv.Itoa(i+1)] = chapter.ID } return options, associationNums @@ -397,6 +402,8 @@ func getSavingOption(option string) (string, bool) { } func (p dlParam) RunInteractiveDownload() { + cols, rows := getTerminalSize() + foundManga := []string{} associationMangaIdNums := make(map[string]string) for isSearching := true; isSearching; { @@ -428,7 +435,7 @@ func (p dlParam) RunInteractiveDownload() { } isSearching = false - printOptions, associationNums := toMangaInfoOptions(searchResult) + printOptions, associationNums := toMangaInfoOptions(searchResult, cols) maps.Copy(associationMangaIdNums, associationNums) foundManga = append(foundManga, printOptions...) } @@ -437,7 +444,7 @@ func (p dlParam) RunInteractiveDownload() { for isSelected := false; !isSelected; { clearOutput() mangaOption, _ := pterm.DefaultInteractiveSelect.WithOptions(foundManga). - WithMaxHeight(8).Show("Select manga from list") + WithMaxHeight(rows - 2).Show("Select manga from list") mangaId := associationMangaIdNums[getMangaNumOption(mangaOption)] respMangaInfo, err := client.GetMangaInfo(mangaId) @@ -458,7 +465,7 @@ func (p dlParam) RunInteractiveDownload() { clearOutput() translatedLanguage, _ := pterm.DefaultInteractiveSelect. WithOptions(mangaInfo.TranslatedLanguages()).WithFilter(false). - WithMaxHeight(8).Show("Select language") + WithMaxHeight(rows - 2).Show("Select language") p.language = translatedLanguage foundChapters := []mangadexapi.Chapter{} @@ -486,10 +493,10 @@ func (p dlParam) RunInteractiveDownload() { selectedChapterNums := []string{} for isSelected := false; !isSelected; { clearOutput() - printChapterOptions, associationIdNums := toChaptersOptions(foundChapters) + printChapterOptions, associationIdNums := toChaptersOptions(foundChapters, cols) selectedChapters, _ := pterm.DefaultInteractiveMultiselect. WithOptions(printChapterOptions). - WithMaxHeight(16).Show("Select chapters from list") + WithMaxHeight(rows - 3).Show("Select chapters from list") if len(selectedChapters) == 0 { isContinue, _ := pterm.DefaultInteractiveConfirm. @@ -533,7 +540,7 @@ func (p dlParam) RunInteractiveDownload() { savingOption, _ := pterm.DefaultInteractiveSelect. WithOptions(toSavingOptions()). - WithMaxHeight(8). + WithMaxHeight(rows - 2). Show("Select saving options") outputExt, isMerge := getSavingOption(savingOption) diff --git a/internal/mdx/print.go b/internal/mdx/print.go index 9fad4ee..48e9b95 100644 --- a/internal/mdx/print.go +++ b/internal/mdx/print.go @@ -2,6 +2,7 @@ package mdx import ( "github.com/arimatakao/mdx/mangadexapi" + "github.com/nathan-fiscaletti/consolesize-go" "github.com/pterm/pterm" ) @@ -18,6 +19,19 @@ func clearOutput() { dp.Print("\033[H\033[J") } +func getTerminalSize() (int, int) { + cols, rows := consolesize.GetConsoleSize() + + if cols < 14 { + cols = 14 + } + if rows < 5 { + rows = 5 + } + + return cols, rows +} + func printMangaInfo(i mangadexapi.MangaInfo) { dp.Println(field.Sprint("Link: "), dp.Sprintf("https://mangadex.org/title/%s", i.ID)) dp.Println(field.Sprint("Title: "), i.Title("en"))