Skip to content

Commit d7c3bfb

Browse files
committed
improved text output (truncate)
1 parent c3fdf96 commit d7c3bfb

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

cmd/search.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@ package cmd
33
import (
44
"fmt"
55
"log"
6+
"strings"
67

78
"github.com/laureanray/clibgen/pkg/api"
89
"github.com/manifoldco/promptui"
910
"github.com/spf13/cobra"
1011
)
1112

13+
func truncateText(s string, max int) string {
14+
if max > len(s) {
15+
return s
16+
}
17+
return s[:strings.LastIndex(s[:max], " ")] + " ..."
18+
}
19+
1220
var (
1321
selectedSite string
1422
numberOfResults = 10
@@ -45,7 +53,9 @@ var (
4553
var titles []string
4654

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

5161
prompt := promptui.Select{

0 commit comments

Comments
 (0)