caret

[ACTIVE] a command line tool for browsing Lobsters in your terminal
git clone git://git.figbert.com/caret.git
Log | Files | Refs | README | LICENSE

header.go (1431B)


      1 package comments
      2 
      3 import (
      4 	"fmt"
      5 	"strings"
      6 
      7 	"git.figbert.com/caret/api"
      8 	"git.figbert.com/caret/ui"
      9 	"git.figbert.com/caret/utils"
     10 
     11 	gloss "github.com/charmbracelet/lipgloss"
     12 
     13 	"github.com/dustin/go-humanize"
     14 )
     15 
     16 func header(post *api.Post) string {
     17 	blockWidth := 72
     18 	textWidth := blockWidth - 2
     19 
     20 	box := gloss.NewStyle().
     21 		BorderStyle(gloss.RoundedBorder()).
     22 		PaddingLeft(1).
     23 		PaddingRight(1).
     24 		MarginLeft(1).
     25 		MarginTop(1).
     26 		Width(blockWidth)
     27 
     28 	url := post.URL
     29 	urlRunes := []rune(url)
     30 	if len(urlRunes) > textWidth {
     31 		url = string(append(urlRunes[:textWidth-1], '…'))
     32 	}
     33 
     34 	posted := utils.TimeFromAPI(post.CreatedAt)
     35 
     36 	user := fmt.Sprintf("by %s %s", ui.LobstersRed().Render(post.User), ui.Gray().Render(humanize.Time(posted)))
     37 	id := fmt.Sprintf("ID %s", gloss.NewStyle().Foreground(gloss.Color("2")).Render(post.ShortID))
     38 	cmnts := fmt.Sprintf("%s comments", gloss.NewStyle().Foreground(gloss.Color("5")).Render(fmt.Sprintf("%d", post.CommentCount)))
     39 	karma := fmt.Sprintf("%s karma", ui.Orange().Render(fmt.Sprintf("%d", post.Score)))
     40 
     41 	topSpacer := strings.Repeat(" ", textWidth-gloss.Width(user)-gloss.Width(id))
     42 	bottomSpacer := strings.Repeat(" ", textWidth-gloss.Width(cmnts)-gloss.Width(karma))
     43 
     44 	return box.Render(
     45 		fmt.Sprintf(
     46 			"%s\n%s\n\n%s%s%s\n%s%s%s",
     47 			post.Headline,
     48 			gloss.NewStyle().Foreground(gloss.Color("4")).Render(url),
     49 			user, topSpacer, id,
     50 			cmnts, bottomSpacer, karma,
     51 		),
     52 	)
     53 }