Skip to content

Commit

Permalink
refactor utils / search
Browse files Browse the repository at this point in the history
  • Loading branch information
laureanray committed Jul 13, 2023
1 parent 68b9f06 commit db2a91b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 42 deletions.
79 changes: 37 additions & 42 deletions cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,15 @@ package cmd

import (
"fmt"
"strings"

"github.com/fatih/color"
"github.com/laureanray/clibgen/internal/book"
"github.com/laureanray/clibgen/internal/libgen"
"github.com/laureanray/clibgen/internal/mirror"
"github.com/laureanray/clibgen/internal/utils"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
)

func truncateText(s string, max int) string {
if max > len(s) {
return s
}
return s[:strings.LastIndex(s[:max], " ")] + " ..."
}

func getExtension(s string) string {
cyan := color.New(color.FgHiCyan).SprintFunc()
magenta := color.New(color.FgHiMagenta).SprintFunc()
Expand All @@ -36,7 +28,8 @@ func getExtension(s string) string {

var (
selectedSite string
selectedFilter string
selectedFilter string
outputDirectory string
numberOfResults = 10

searchCmd = &cobra.Command{
Expand All @@ -50,55 +43,53 @@ var (
return
}

var m mirror.Mirror
var m mirror.Mirror

if selectedSite == "legacy" {
m = mirror.NewLegacyMirror(libgen.IS)
m = mirror.NewLegacyMirror(libgen.IS)
} else if selectedSite == "new" {
m = mirror.NewCurrentMirror(libgen.LC)
} else{
// TODO: Improve this.
fmt.Print("Not an option");
return
}

var books []book.Book

switch (selectedFilter) {
case libgen.AUTHOR:
books, _ = m.SearchByAuthor(args[0])
default:
books, _ = m.SearchByTitle(args[0])
}

if len(books) == 0 {
return
}
m = mirror.NewCurrentMirror(libgen.LC)
} else {
// TODO: Improve this.
fmt.Print("Not an option")
return
}

var books []book.Book

switch selectedFilter {
case libgen.AUTHOR:
books, _ = m.SearchByAuthor(args[0])
default:
books, _ = m.SearchByTitle(args[0])
}

if len(books) == 0 {
return
}

var titles []string

for _, book := range books {
parsedTitle := truncateText(book.Title, 42)
parsedAuthor := truncateText(book.Author, 24)
parsedTitle := utils.TruncateText(book.Title, 42)
parsedAuthor := utils.TruncateText(book.Author, 24)
parsedExt := getExtension(fmt.Sprintf("%-4s", book.Extension))
titles = append(titles, fmt.Sprintf("%s %-6s | %-45s %s", parsedExt, book.FileSize, parsedTitle, parsedAuthor))
}

prompt := promptui.Select{
Label: "Select Title",
Items: titles,
}

resultInt, _, err := prompt.Run()

if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}

println(resultInt)

m.DownloadSelection(books[resultInt])
m.DownloadSelection(books[resultInt], outputDirectory)
},
}
)
Expand All @@ -112,15 +103,19 @@ func init() {
"new"
`)

searchCmd.
PersistentFlags().
StringVarP(&selectedFilter, "filter", "f", "title", `select which filter to use
searchCmd.
PersistentFlags().
StringVarP(&selectedFilter, "filter", "f", "title", `select which filter to use
options:
"title"
"author"
"isbn"
`)

searchCmd.
PersistentFlags().
StringVarP(&outputDirectory, "output", "o", "./", `Output directory`)

searchCmd.
PersistentFlags().
IntVarP(&numberOfResults, "number of results", "n", 10, `number of result(s) to be displayed maximum: 25`)
Expand Down
10 changes: 10 additions & 0 deletions internal/utils/string.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package utils

import "strings"

func TruncateText(s string, max int) string {
if max > len(s) {
return s
}
return s[:strings.LastIndex(s[:max], " ")] + " ..."
}

0 comments on commit db2a91b

Please sign in to comment.