Make String() methods more useful for terminal output

This commit is contained in:
Alessio 2021-07-24 10:55:44 -07:00
parent 6568259d63
commit d386ba2cb4
2 changed files with 70 additions and 5 deletions

View File

@ -3,6 +3,8 @@ package scraper
import (
"time"
"fmt"
"offline_twitter/terminal_utils"
)
const DEFAULT_MAX_REPLIES_EAGER_LOAD = 50
@ -29,11 +31,42 @@ type Tweet struct {
QuotedTweet TweetID
}
func (t Tweet) String() string {
return fmt.Sprintf(
`ID %s, User %s: %q (%s). Likes: %d, Retweets: %d, QTs: %d, Replies: %d.
Urls: %v Images: %v Mentions: %v Hashtags: %v`,
t.ID, t.UserID, t.Text, t.PostedAt, t.NumLikes, t.NumRetweets, t.NumQuoteTweets, t.NumReplies, t.Urls, t.Images, t.Mentions, t.Hashtags)
var author string
if t.User != nil {
author = fmt.Sprintf("%s\n@%s", t.User.DisplayName, t.User.Handle)
} else {
author = "@???"
}
ret := fmt.Sprintf(
`%s
%s
%s
Replies: %d RT: %d QT: %d Likes: %d
`,
author,
terminal_utils.FormatDate(t.PostedAt),
terminal_utils.WrapText(t.Text, 60),
t.NumReplies,
t.NumRetweets,
t.NumQuoteTweets,
t.NumLikes,
)
if len(t.Images) > 0 {
ret += fmt.Sprintf(terminal_utils.COLOR_GREEN + "images: %d\n" + terminal_utils.COLOR_RESET, len(t.Images))
}
if len(t.Urls) > 0 {
ret += "urls: [\n"
for _, url := range(t.Urls) {
ret += " " + url + "\n"
}
ret += "]"
}
return ret
}
// Turn an APITweet, as returned from the scraper, into a properly structured Tweet object

View File

@ -4,6 +4,8 @@ import (
"time"
"fmt"
"strings"
"offline_twitter/terminal_utils"
)
type UserID string
@ -36,7 +38,37 @@ type User struct {
}
func (u User) String() string {
return fmt.Sprintf("%s (@%s)[%s]: %q", u.DisplayName, u.Handle, u.ID, u.Bio)
var verified string
if u.IsVerified {
verified = "[\u2713]"
}
ret := fmt.Sprintf(
`%s%s
@%s
%s
Following: %d Followers: %d
Joined %s
%s
%s
`,
u.DisplayName,
verified,
u.Handle,
terminal_utils.WrapText(u.Bio, 60),
u.FollowingCount,
u.FollowersCount,
terminal_utils.FormatDate(u.JoinDate),
u.Location,
u.Website,
)
if u.PinnedTweet != nil {
ret += "\n" + terminal_utils.WrapText(u.PinnedTweet.Text, 60)
} else {
println("Pinned tweet id:", u.PinnedTweetID)
}
return ret
}
// Turn an APIUser, as returned from the scraper, into a properly structured User object