From f20ab17ed8f241b08e1ee8c5543eae1908847ff1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Sat, 3 Apr 2021 18:36:48 -0500 Subject: [PATCH] Implemented timeline switching --- mast/cmd.go | 12 ++++++++++++ mast/timeline.go | 21 +++++++++++++++++---- tui/tui.go | 14 ++++++++++++-- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/mast/cmd.go b/mast/cmd.go index 8bb818d..3431005 100644 --- a/mast/cmd.go +++ b/mast/cmd.go @@ -100,6 +100,18 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) { args := split[1] switch cmd { + case "home": + timeline.Switch(TimelineHome) + return CodeOk + case "local": + timeline.Switch(TimelineLocal) + return CodeOk + case "public": + timeline.Switch(TimelinePublic) + return CodeOk + case "notifications": + timeline.Switch(TimelineNotifications) + return CodeOk case "t", "toot": return CmdToot(timeline, args, VisibilityPublic) case "tp", "tootprivate": diff --git a/mast/timeline.go b/mast/timeline.go index 3340d16..2796d00 100644 --- a/mast/timeline.go +++ b/mast/timeline.go @@ -17,10 +17,10 @@ const ( type Timeline struct { client *mastodon.Client + timelineType TimelineType LastRenderedIndex int - Type TimelineType Account mastodon.Account Toots []Toot TootIndexStatusIDMappings map[string]int @@ -30,16 +30,29 @@ type Timeline struct { func NewTimeline(mastodonClient *mastodon.Client) Timeline { timeline := Timeline{ client: mastodonClient, + timelineType: TimelineHome, LastRenderedIndex: -1, - Type: TimelineHome, TootIndexStatusIDMappings: make(map[string]int), } return timeline } -func (timeline *Timeline) Load(timelineType TimelineType) (error) { +func (timeline *Timeline) Switch(timelineType TimelineType) { + if timeline.timelineType != timelineType { + timeline.timelineType = timelineType + timeline.Toots = []Toot{} + timeline.TootIndexStatusIDMappings = make(map[string]int) + timeline.LastRenderedIndex = -1 + } +} + +func (timeline *Timeline) GetCurrentType() (TimelineType) { + return timeline.timelineType +} + +func (timeline *Timeline) Load() (error) { var statuses []*mastodon.Status var err error @@ -50,7 +63,7 @@ func (timeline *Timeline) Load(timelineType TimelineType) (error) { timeline.Account = *account - switch timelineType { + switch timeline.timelineType { case TimelineHome: statuses, err = timeline.client.GetTimelineHome(context.Background(), nil) case TimelineLocal: diff --git a/tui/tui.go b/tui/tui.go index 77b2ba9..6444449 100644 --- a/tui/tui.go +++ b/tui/tui.go @@ -28,12 +28,16 @@ type TUICore struct { Mode ModeType Timeline mast.Timeline + RenderedTimelineType mast.TimelineType } func TUI(tuiCore TUICore) { - tuiCore.Timeline = mast.NewTimeline(tuiCore.Client) tuiCore.App = tview.NewApplication() + tuiCore.Timeline = mast.NewTimeline(tuiCore.Client) + tuiCore.RenderedTimelineType = mast.TimelineHome + tuiCore.Timeline.Switch(mast.TimelineHome) + tuiCore.CmdLine = tview.NewInputField(). SetLabelColor(tcell.ColorDefault). SetFieldBackgroundColor(tcell.ColorDefault). @@ -111,7 +115,7 @@ func TUI(tuiCore TUICore) { func (tuiCore *TUICore) UpdateTimeline(scrollToEnd bool) bool { _, _, w, _ := tuiCore.Stream.Box.GetInnerRect() - err := tuiCore.Timeline.Load(tuiCore.Timeline.Type) + err := tuiCore.Timeline.Load() if err != nil { // TODO: Display errors somewhere return false @@ -124,6 +128,12 @@ func (tuiCore *TUICore) UpdateTimeline(scrollToEnd bool) bool { return false } + currentTimelineType := tuiCore.Timeline.GetCurrentType() + if tuiCore.RenderedTimelineType != currentTimelineType { + tuiCore.Stream.Clear() + tuiCore.RenderedTimelineType = currentTimelineType + } + fmt.Fprint(tuiCore.Stream, tview.TranslateANSI(output)) if scrollToEnd == true {