Skip to content

Commit

Permalink
Implemented whois command and user profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Apr 4, 2021
1 parent 85ee4f7 commit bcd2ce5
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 14 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -16,3 +16,4 @@

.DS_Store
/gomphotherium
error.log
6 changes: 6 additions & 0 deletions README.md
Expand Up @@ -96,6 +96,9 @@ Leave **Command** mode (while in **Command** mode)
`hashtag`*` tag [local]`* \
Switch between timelines

`whois`*` user`* \
Switch to user profile and timeline

`t`*` content ...`* \
`toot`*` content ...`* \
Publish a new public toot
Expand Down Expand Up @@ -193,6 +196,9 @@ the *local* instance
`hashtag lol` \
Switch to the hashtag timeline and search for the hashtag *#lol* globally

`whois mrus@merveilles.town` \
Switch to the profile of *mrus@merveilles.town* and list his toots

`t Hello World!` \
Publish a new public toot that says *Hello World!*

Expand Down
16 changes: 14 additions & 2 deletions mast/cmd.go
Expand Up @@ -16,6 +16,7 @@ const (
CodeOk CmdReturnCode = 0
CodeNotOk = 1
CodeCommandNotFound = 2
CodeUserNotFound = 3

CodeQuit = -1
CodeHelp = -2
Expand All @@ -32,6 +33,8 @@ func CmdAvailable() ([]string) {
"notifications",
"hashtag",

"whois",

"t",
"toot",

Expand Down Expand Up @@ -70,8 +73,6 @@ func CmdAvailable() ([]string) {
"open",
"share",

"whois",

// "search",

"help",
Expand Down Expand Up @@ -152,6 +153,17 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) {
timeline.Switch(TimelineHashtag, &timelineOptions)
return CodeOk
case "whois":
accounts, err := timeline.SearchUser(args, 1)
if err != nil || len(accounts) < 1 {
// TODO: pass info back to caller
return CodeUserNotFound
}

timelineOptions := TimelineOptions{
User: *accounts[0],
}

timeline.Switch(TimelineUser, &timelineOptions)
return CodeOk
case "t", "toot":
return CmdToot(timeline, args, -1, VisibilityPublic)
Expand Down
24 changes: 22 additions & 2 deletions mast/timeline.go
Expand Up @@ -13,12 +13,13 @@ const (
TimelinePublic = 2
TimelineNotifications = 3
TimelineHashtag = 4
TimelineEnd = 5
TimelineUser = 5
)

type TimelineOptions struct {
IsLocal bool
Hashtag string
User mastodon.Account
}

type Timeline struct {
Expand Down Expand Up @@ -48,7 +49,9 @@ func NewTimeline(mastodonClient *mastodon.Client) Timeline {
}

func (timeline *Timeline) Switch(timelineType TimelineType, options *TimelineOptions) {
if timeline.timelineType != timelineType {
if timeline.timelineType != timelineType ||
timelineType == TimelineHashtag ||
timelineType == TimelineUser {
timeline.timelineType = timelineType
if options != nil {
timeline.timelineOptions = *options
Expand All @@ -63,6 +66,10 @@ func (timeline *Timeline) GetCurrentType() (TimelineType) {
return timeline.timelineType
}

func (timeline *Timeline) GetCurrentOptions() (TimelineOptions) {
return timeline.timelineOptions
}

func (timeline *Timeline) Load() (error) {
var statuses []*mastodon.Status
var err error
Expand Down Expand Up @@ -102,6 +109,13 @@ func (timeline *Timeline) Load() (error) {
timeline.timelineOptions.IsLocal,
nil,
)
case TimelineUser:
statuses, err =
timeline.client.GetAccountStatuses(
context.Background(),
timeline.timelineOptions.User.ID,
nil,
)
}

if err != nil {
Expand Down Expand Up @@ -185,3 +199,9 @@ func (timeline *Timeline) Fav(
}
return timeline.client.Unfavourite(context.Background(), id)
}

func (timeline *Timeline) SearchUser(
query string,
limit int64) ([]*mastodon.Account, error) {
return timeline.client.AccountsSearch(context.Background(), query, limit)
}
93 changes: 93 additions & 0 deletions tui/profile.go
@@ -0,0 +1,93 @@
package tui

import (
"fmt"
"math"
// "time"
// "context"

"github.com/mattn/go-runewidth"
"github.com/grokify/html-strip-tags-go"
"html"

// "image/color"
// "github.com/eliukblau/pixterm/pkg/ansimage"

"github.com/mattn/go-mastodon"
// "github.com/mrusme/gomphotherium/mast"
)

func RenderProfile(
profile *mastodon.Account,
width int,
showImages bool) (string, error) {
var output string = ""
var err error = nil

account := profile.Acct
if account == "" {
account = profile.Username
}

bot := ""
if profile.Bot == true {
bot = "\xF0\x9F\xA4\x96"
}

output = fmt.Sprintf("%s[blue]%s[-] [grey]%s[-] [red]%s[-]\n%s\n\n",
output,
profile.DisplayName,
account,
bot,
runewidth.Truncate(
html.UnescapeString(strip.StripTags(profile.Note)),
width,
"...",
),
)

halfwidth := int(math.Floor(float64(width)))

fieldsNumber := len(profile.Fields)
if fieldsNumber > 4 {
fieldsNumber = 4
}

for i := 0; i < fieldsNumber; i++ {
field := profile.Fields[i]
output = fmt.Sprintf("%s[grey]%s:[-] [magenta]%s[-]\n",
output,
runewidth.Truncate(
field.Name,
halfwidth,
"...",
),
runewidth.Truncate(
html.UnescapeString(strip.StripTags(field.Value)),
halfwidth,
"...",
),
)
}

for i := fieldsNumber; i < 4; i++ {
output = fmt.Sprintf("%s\n", output)
}

output = fmt.Sprintf("%s[blue]%d[-] [grey]toots[-] ",
output,
profile.StatusesCount,
)

output = fmt.Sprintf("%s[blue]%d[-] [grey]followers[-] ",
output,
profile.FollowersCount,
)

output = fmt.Sprintf("%s[blue]%d[-] [grey]following[-] ",
output,
profile.FollowingCount,
)

return output, err
}
61 changes: 51 additions & 10 deletions tui/tui.go
Expand Up @@ -27,6 +27,7 @@ type TUICore struct {
Client *mastodon.Client
App *tview.Application
CmdLine *tview.InputField
Profile *tview.TextView
Stream *tview.TextView
Grid *tview.Grid

Expand Down Expand Up @@ -77,6 +78,19 @@ func TUI(tuiCore TUICore) {

switch retCode {
case mast.CodeOk:
if tuiCore.Timeline.GetCurrentType() == mast.TimelineUser &&
tuiCore.RenderedTimelineType != mast.TimelineUser {
tuiCore.Grid.
RemoveItem(tuiCore.Stream).
AddItem(tuiCore.Profile, 0, 0, 1, 1, 0, 0, false).
AddItem(tuiCore.Stream, 1, 0, 1, 1, 0, 0, false)
} else if tuiCore.RenderedTimelineType == mast.TimelineUser &&
tuiCore.Timeline.GetCurrentType() != mast.TimelineUser {
tuiCore.Grid.
RemoveItem(tuiCore.Profile).
RemoveItem(tuiCore.Stream).
AddItem(tuiCore.Stream, 0, 0, 2, 1, 0, 0, false)
}
tuiCore.UpdateTimeline(true)
case mast.CodeHelp:
tuiCore.ShowHelp()
Expand All @@ -86,17 +100,23 @@ func TUI(tuiCore TUICore) {
}
})

tuiCore.Profile = tview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
SetWrap(true).
SetScrollable(false)

tuiCore.Stream = tview.NewTextView().
SetDynamicColors(true).
SetRegions(true).
SetWrap(true)

tuiCore.Grid = tview.NewGrid().
SetRows(0, 1).
SetRows(8, 0, 1).
SetColumns(0).
SetBorders(true).
AddItem(tuiCore.Stream, 0, 0, 1, 1, 0, 0, false).
AddItem(tuiCore.CmdLine, 1, 0, 1, 1, 0, 0, true)
AddItem(tuiCore.Stream, 0, 0, 2, 1, 0, 0, false).
AddItem(tuiCore.CmdLine, 2, 0, 1, 1, 0, 0, false)

tuiCore.App.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
Expand Down Expand Up @@ -186,6 +206,15 @@ func (tuiCore *TUICore) UpdateTimeline(scrollToEnd bool) bool {
return false
}

currentTimelineType := tuiCore.Timeline.GetCurrentType()
if tuiCore.RenderedTimelineType != currentTimelineType ||
currentTimelineType == mast.TimelineHashtag ||
currentTimelineType == mast.TimelineUser {
tuiCore.Stream.Clear()
tuiCore.RenderedTimelineType = currentTimelineType
tuiCore.Timeline.LastRenderedIndex = -1
}

output, err := RenderTimeline(
&tuiCore.Timeline,
w,
Expand All @@ -197,19 +226,31 @@ func (tuiCore *TUICore) UpdateTimeline(scrollToEnd bool) bool {
return false
}

currentTimelineType := tuiCore.Timeline.GetCurrentType()
if tuiCore.RenderedTimelineType != currentTimelineType ||
currentTimelineType == mast.TimelineHashtag {
tuiCore.Stream.Clear()
tuiCore.RenderedTimelineType = currentTimelineType
}

fmt.Fprint(tuiCore.Stream, tview.TranslateANSI(output))

if scrollToEnd == true {
tuiCore.Stream.ScrollToEnd()
}

if currentTimelineType == mast.TimelineUser {
tuiCore.Profile.Clear()

options := tuiCore.Timeline.GetCurrentOptions()

profileOutput, err := RenderProfile(
&options.User,
w,
tuiCore.Options.ShowImages,
)

if err != nil {
// TODO: Display errors somewhere
return false
}

fmt.Fprint(tuiCore.Profile, tview.TranslateANSI(profileOutput))
}

return true
}

Expand Down

0 comments on commit bcd2ce5

Please sign in to comment.