forked from wtfutil/wtf
/
widget.go
129 lines (98 loc) · 3.16 KB
/
widget.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
package twitter
import (
"fmt"
"html"
"regexp"
"github.com/dustin/go-humanize"
"github.com/rivo/tview"
"github.com/doctorfree/wtf/utils"
"github.com/doctorfree/wtf/view"
)
type Widget struct {
view.MultiSourceWidget
view.TextWidget
client *Client
idx int
settings *Settings
}
func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{
MultiSourceWidget: view.NewMultiSourceWidget(settings.Common, "screenName", "screenNames"),
TextWidget: view.NewTextWidget(tviewApp, redrawChan, pages, settings.Common),
idx: 0,
settings: settings,
}
widget.initializeKeyboardControls()
widget.SetDisplayFunction(widget.Refresh)
widget.client = NewClient(settings)
widget.View.SetBorderPadding(1, 1, 1, 1)
widget.View.SetWrap(true)
widget.View.SetWordWrap(true)
return &widget
}
/* -------------------- Exported Functions -------------------- */
// Refresh is called on the interval and refreshes the data
func (widget *Widget) Refresh() {
widget.Redraw(widget.content)
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) content() (string, string, bool) {
widget.client.screenName = widget.CurrentSource()
tweets := widget.client.Tweets()
title := fmt.Sprintf("Twitter - [green]@%s[white]", widget.CurrentSource())
if len(tweets) == 0 {
str := fmt.Sprintf("\n\n\n%s", utils.CenterText("[lightblue]No Tweets[white]", 50))
return title, str, true
}
_, _, width, _ := widget.View.GetRect()
str := widget.settings.PaginationMarker(len(widget.Sources), widget.Idx, width-2) + "\n"
for _, tweet := range tweets {
str += widget.format(tweet)
}
return title, str, true
}
// If the tweet's Username is the same as the account we're watching, no
// need to display the username
func (widget *Widget) displayName(tweet Tweet) string {
if widget.CurrentSource() == tweet.User.ScreenName {
return ""
}
return tweet.User.ScreenName
}
func (widget *Widget) formatText(text string) string {
result := text
// Convert HTML entities
result = html.UnescapeString(result)
// RT indicator
rtRegExp := regexp.MustCompile(`^RT`)
result = rtRegExp.ReplaceAllString(result, "[olive]${0}[white::-]")
// @name mentions
atRegExp := regexp.MustCompile(`@[0-9A-Za-z_]*`)
result = atRegExp.ReplaceAllString(result, "[lightblue]${0}[white]")
// HTTP(S) links
linkRegExp := regexp.MustCompile(`http[s:\/.0-9A-Za-z]*`)
result = linkRegExp.ReplaceAllString(result, "[lightblue::u]${0}[white::-]")
// Hash tags
hashRegExp := regexp.MustCompile(`#[0-9A-Za-z_]*`)
result = hashRegExp.ReplaceAllString(result, "[yellow]${0}[white]")
return result
}
func (widget *Widget) format(tweet Tweet) string {
body := widget.formatText(tweet.Text)
name := widget.displayName(tweet)
var attribution string
if name == "" {
attribution = humanize.Time(tweet.Created())
} else {
attribution = fmt.Sprintf(
"%s, %s",
name,
humanize.Time(tweet.Created()),
)
}
return fmt.Sprintf("%s\n[grey]%s[white]\n\n", body, attribution)
}
func (widget *Widget) currentSourceURI() string {
src := "https://twitter.com/" + widget.CurrentSource()
return src
}