item.go (1331B)
1 package main 2 3 import ( 4 "fmt" 5 "net/url" 6 "strings" 7 8 "git.figbert.com/caret/api" 9 "git.figbert.com/caret/ui" 10 "git.figbert.com/caret/utils" 11 12 "github.com/dustin/go-humanize" 13 ) 14 15 type item struct { 16 post api.Post 17 } 18 19 func (i item) FilterValue() string { return "" } 20 func (i item) Title() string { 21 parsedURL, err := url.Parse(i.post.URL) 22 if err != nil { 23 return i.post.Headline 24 } 25 26 inline := " ☶ " 27 if u := parsedURL.Hostname(); u != "" { 28 inline = fmt.Sprintf(" (%s)", u) 29 } 30 31 return fmt.Sprintf("%s%s", i.post.Headline, ui.Gray().Render(inline)) 32 } 33 func (itm item) Description() string { 34 created := utils.TimeFromAPI(itm.post.CreatedAt) 35 relationship := "via" 36 if itm.post.UserIsAuthor { 37 relationship = "authored by" 38 } 39 40 intro := ui.Gray().Render( 41 fmt.Sprintf( 42 "%d upvotes %s %s | %s | ", 43 itm.post.Score, relationship, 44 itm.post.User, humanize.Time(created), 45 ), 46 ) 47 48 var tags strings.Builder 49 for i, t := range itm.post.Tags { 50 switch t { 51 case "video", "pdf", "slides", "show", "meta", "ask": 52 tags.WriteString(ui.Purple().Render(t)) 53 default: 54 tags.WriteString(ui.Orange().Render(t)) 55 } 56 if i != len(itm.post.Tags)-1 { 57 tags.WriteString(ui.Gray().Render(", ")) 58 } 59 } 60 61 comments := ui.Gray().Render(fmt.Sprintf(" | %d comments", itm.post.CommentCount)) 62 63 return intro + tags.String() + comments 64 }