-
Notifications
You must be signed in to change notification settings - Fork 9
/
output.go
144 lines (131 loc) · 2.97 KB
/
output.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// -*- tab-width: 4; -*-
package main
import (
"fmt"
"regexp"
"strings"
"time"
"github.com/goware/urlx"
)
func red(s string) string {
return fmt.Sprintf("\033[31m%s\033[0m", s)
}
func green(s string) string {
return fmt.Sprintf("\033[32m%s\033[0m", s)
}
func yellow(s string) string {
return fmt.Sprintf("\033[33m%s\033[0m", s)
}
func boldgreen(s string) string {
return fmt.Sprintf("\033[32;1m%s\033[0m", s)
}
func blue(s string) string {
return fmt.Sprintf("\033[34m%s\033[0m", s)
}
func PrintFollowee(nick, url string) {
fmt.Printf("> %s @ %s",
yellow(nick),
url,
)
}
func PrintFolloweeRaw(nick, url string) {
fmt.Printf("%s: %s\n", nick, url)
}
func PrintTweet(tweet Tweet, now time.Time) {
text := ShortenMentions(tweet.Text)
nick := green(tweet.Tweeter.Nick)
if NormalizeURL(tweet.Tweeter.URL) == NormalizeURL(conf.Twturl) {
nick = boldgreen(tweet.Tweeter.Nick)
}
fmt.Printf("> %s (%s)\n%s\n",
nick,
PrettyDuration(now.Sub(tweet.Created)),
text)
}
func PrintTweetRaw(tweet Tweet) {
fmt.Printf("%s\t%s\t%s",
tweet.Tweeter.URL,
tweet.Created.Format(time.RFC3339),
tweet.Text)
}
// Turns "@<nick URL>" into "@nick" if we're following URL (or it's us!). If
// we're following as another nick then "@nick(followednick)".
func ShortenMentions(text string) string {
re := regexp.MustCompile(`@<([^ ]+) *([^>]+)>`)
return re.ReplaceAllStringFunc(text, func(match string) string {
parts := re.FindStringSubmatch(match)
nick, url := parts[1], parts[2]
if fnick := conf.urlToNick(url); fnick != "" {
return FormatMention(nick, url, fnick)
}
// Not shortening if we're not following
return match
})
}
func NormalizeURL(url string) string {
if url == "" {
return ""
}
u, err := urlx.Parse(url)
if err != nil {
return ""
}
if u.Scheme == "https" {
u.Scheme = "http"
u.Host = strings.TrimSuffix(u.Host, ":443")
}
u.User = nil
u.Path = strings.TrimSuffix(u.Path, "/")
norm, err := urlx.Normalize(u)
if err != nil {
return ""
}
return norm
}
// Takes followednick to be able to indicated when somebody (URL) was mentioned
// using a nick other than the one we follow the person as.
func FormatMention(nick, url, followednick string) string {
str := "@" + nick
if followednick != nick {
str += fmt.Sprintf("(%s)", followednick)
}
if NormalizeURL(url) == NormalizeURL(conf.Twturl) {
return red(str)
}
return blue(str)
}
func PrettyDuration(duration time.Duration) string {
s := int(duration.Seconds())
d := s / 86400
s %= 86400
if d >= 365 {
return fmt.Sprintf("%dy %dw ago", d/365, d%365/7)
}
if d >= 14 {
return fmt.Sprintf("%dw ago", d/7)
}
h := s / 3600
s %= 3600
if d > 0 {
str := fmt.Sprintf("%dd", d)
if h > 0 && d <= 6 {
str += fmt.Sprintf(" %dh", h)
}
return str + " ago"
}
m := s / 60
s %= 60
if h > 0 || m > 0 {
str := ""
hh := ""
if h > 0 {
str += fmt.Sprintf("%dh", h)
hh = " "
}
if m > 0 {
str += fmt.Sprintf("%s%dm", hh, m)
}
return str + " ago"
}
return fmt.Sprintf("%ds ago", s)
}