comments.go (1393B)
1 package comments 2 3 import ( 4 "fmt" 5 "os/exec" 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/charmbracelet/bubbletea" 13 "github.com/charmbracelet/glamour" 14 gloss "github.com/charmbracelet/lipgloss" 15 16 "github.com/dustin/go-humanize" 17 ) 18 19 func Load(post api.Post) tea.Cmd { 20 return func() tea.Msg { 21 renderer, err := glamour.NewTermRenderer(glamour.WithStyles(ui.GlamourConfig)) 22 if err != nil { 23 return utils.Error{} 24 } 25 26 var page strings.Builder 27 page.WriteString(header(&post)) 28 29 for _, comment := range post.Comments { 30 cmnt, err := renderer.Render(comment.CommentPlain) 31 if err != nil { 32 return utils.Error{} 33 } 34 cmnt = strings.TrimSpace(cmnt) 35 cmnt = gloss.NewStyle().MarginLeft(2).Render(cmnt) 36 37 headline := fmt.Sprintf( 38 " %s %s", 39 ui.Bold().Render(comment.User), 40 ui.Gray().Render(humanize.Time( 41 utils.TimeFromAPI(comment.CreatedAt), 42 )), 43 ) 44 45 composite := fmt.Sprintf("%s\n%s", headline, cmnt) 46 margin := gloss.NewStyle().MarginLeft(2 * comment.Depth) 47 page.WriteString("\n\n" + margin.Render(composite)) 48 } 49 50 command := exec.Command("less", 51 "--RAW-CONTROL-CHARS", 52 "--ignore-case", 53 "--tilde", 54 "--use-color", 55 "-P?e"+ui.Prompt().Render("End"), 56 "-DSy", 57 "-DP-") 58 command.Stdin = strings.NewReader(page.String()) 59 60 return utils.RunCommand(command) 61 } 62 }