From d7c3bfb614928a1a810b26c841bb5434ee6be2b5 Mon Sep 17 00:00:00 2001 From: laureanray Date: Tue, 19 Jul 2022 17:19:33 +0800 Subject: [PATCH] improved text output (truncate) --- cmd/search.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/cmd/search.go b/cmd/search.go index 3556b5b..630d0ef 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -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 @@ -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{