This repository has been archived by the owner on Apr 11, 2023. It is now read-only.
/
main.go
76 lines (67 loc) · 1.88 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package printer
import (
"fmt"
"strings"
markdown "github.com/MichaelMure/go-term-markdown"
"github.com/azimut/redditviewer/format"
"github.com/azimut/redditviewer/human"
"github.com/fatih/color"
"github.com/tidwall/gjson"
)
func Print_Posts(r gjson.Result, op string, width int) {
for _, c := range r.Array() {
childrens(c, op, width)
}
}
func childrens(r gjson.Result, op string, width int) {
print_post(r, op, width)
for _, v := range r.Get("replies.data.children.#.data").Array() {
childrens(v, op, width)
}
}
func print_post(r gjson.Result, op string, width int) {
// Comment
depth := int(r.Get("depth").Int())
msg := r.Get("body").String()
msg = strings.Replace(msg, ">", ">", -1) // KLUDGE: ">" not handled by markdown
comment := markdown.Render(msg, width, human.Max(3*depth, 1))
fmt.Print(string(comment))
// Check if author is op
author := r.Get("author").String()
yellow := color.New(color.FgYellow).SprintFunc()
if author == op {
author = yellow(author)
}
// Footer
unix_human := human.Unix_Time(r.Get("created_utc").Int())
score := r.Get("score").Int()
reply, _ :=
format.Format_Post(
fmt.Sprintf("%s(%d) - %s\n",
author,
score,
unix_human),
depth)
fmt.Println(reply)
fmt.Println()
}
func Print_Header(r gjson.Result, width int) {
title := r.Get("title")
url := r.Get("url")
fmt.Printf("\ntitle: %s\nurl: %s\n", title, url)
selftext := r.Get("selftext").String()
selftext = strings.Replace(selftext, ">", ">", -1) // KLUDGE: ">" not handled by markdown
if len(strings.TrimSpace(selftext)) > 0 {
resp := markdown.Render(selftext, width, 3)
fmt.Printf("\n%s\n", string(resp))
}
upvotes := r.Get("ups").Int()
author := r.Get("author")
unix_human := human.Unix_Time(r.Get("created_utc").Int())
comments := r.Get("num_comments").Int()
fmt.Printf("%s(%d) - %s - %d Comment(s)\n\n\n",
author,
upvotes,
unix_human,
comments)
}