Skip to content

Commit

Permalink
Implemented timeline switching
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Apr 3, 2021
1 parent b22aedc commit f20ab17
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
12 changes: 12 additions & 0 deletions mast/cmd.go
Expand Up @@ -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":
Expand Down
21 changes: 17 additions & 4 deletions mast/timeline.go
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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:
Expand Down
14 changes: 12 additions & 2 deletions tui/tui.go
Expand Up @@ -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).
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit f20ab17

Please sign in to comment.