Skip to content

Commit

Permalink
Fix print in interactive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
arimatakao committed Aug 17, 2024
1 parent f50ced0 commit a870860
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion app/meta.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package app

const (
VERSION = "v1.11.0"
VERSION = "v1.11.1"

API_VERSION = "v5.10.2"

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
35 changes: 21 additions & 14 deletions internal/mdx/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,6 @@ func (p dlParam) DownloadAllChapters(mangaId string) {
os.Exit(0)
}

fmt.Print(p.chapters)

printShortMangaInfo(mangaInfo)
if p.isMerge {
p.downloadMergeChapters()
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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; {
Expand Down Expand Up @@ -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...)
}
Expand All @@ -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)
Expand All @@ -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{}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions internal/mdx/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package mdx

import (
"github.com/arimatakao/mdx/mangadexapi"
"github.com/nathan-fiscaletti/consolesize-go"
"github.com/pterm/pterm"
)

Expand All @@ -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"))
Expand Down

0 comments on commit a870860

Please sign in to comment.