Skip to content

Commit

Permalink
improved text output (truncate)
Browse files Browse the repository at this point in the history
  • Loading branch information
laureanray committed Jul 19, 2022
1 parent c3fdf96 commit d7c3bfb
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion cmd/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,20 @@ package cmd
import (
"fmt"
"log"
"strings"

"github.com/laureanray/clibgen/pkg/api"
"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], " ")] + " ..."
}

var (
selectedSite string
numberOfResults = 10
Expand Down Expand Up @@ -45,7 +53,9 @@ var (
var titles []string

for _, book := range books {
titles = append(titles, fmt.Sprintf("[%s] [%s] %s (%s)", book.FileSize, book.Extension, book.Title, book.Author))
parsedTitle := truncateText(book.Title, 42)
parsedAuthor := truncateText(book.Author, 24)
titles = append(titles, fmt.Sprintf("[%5s %4s] %-45s %s", book.FileSize, book.Extension, parsedTitle, parsedAuthor))
}

prompt := promptui.Select{
Expand Down

0 comments on commit d7c3bfb

Please sign in to comment.