Skip to content

Commit

Permalink
Refactored code structure
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Apr 3, 2021
1 parent 9235494 commit b22aedc
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
32 changes: 10 additions & 22 deletions mast/cmd.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package mast

import (
"context"
"strings"

"github.com/mattn/go-mastodon"
)

type CmdReturnCode int
Expand All @@ -24,7 +21,6 @@ func CmdAvailable() ([]string) {

"t",
"toot",
"tootpublic",

"tp",
"tootprivate",
Expand Down Expand Up @@ -98,28 +94,28 @@ func CmdAutocompleter(input string, knownUsers []string) ([]string) {
return entries
}

func CmdProcessor(mastodonClient *mastodon.Client, input string) (CmdReturnCode) {
func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) {
split := strings.SplitN(input, " ", 2)
cmd := split[0]
args := split[1]

switch cmd {
case "quit", "exit", "bye":
return CodeQuit
case "t", "toot", "tootpublic":
return CmdToot(mastodonClient, args, VisibilityPublic)
case "t", "toot":
return CmdToot(timeline, args, VisibilityPublic)
case "tp", "tootprivate":
return CmdToot(mastodonClient, args, VisibilityPrivate)
return CmdToot(timeline, args, VisibilityPrivate)
case "tu", "tootunlisted":
return CmdToot(mastodonClient, args, VisibilityUnlisted)
return CmdToot(timeline, args, VisibilityUnlisted)
case "td", "tootdirect":
return CmdToot(mastodonClient, args, VisibilityUnlisted)
return CmdToot(timeline, args, VisibilityUnlisted)
case "quit", "exit", "bye":
return CodeQuit
}

return CodeOk
}

func CmdToot(mastodonClient *mastodon.Client, content string, visibility string) (CmdReturnCode) {
func CmdToot(timeline *Timeline, content string, visibility string) (CmdReturnCode) {
var status string = ""
var spoiler string = ""
var sensitive bool = false
Expand All @@ -136,18 +132,10 @@ func CmdToot(mastodonClient *mastodon.Client, content string, visibility string)

status = splitSpoiler[0]

newToot := mastodon.Toot{
Status: status,
Visibility: visibility,
Sensitive: sensitive,
SpoilerText: spoiler,
}

_, err := mastodonClient.PostStatus(context.Background(), &newToot)
_, err := timeline.Toot(&status, -1, nil, &visibility, sensitive, &spoiler)
if err != nil {
return CodeNotOk
}

return CodeOk
}

12 changes: 12 additions & 0 deletions mast/timeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func NewTimeline(mastodonClient *mastodon.Client) Timeline {
client: mastodonClient,

LastRenderedIndex: -1,
Type: TimelineHome,
TootIndexStatusIDMappings: make(map[string]int),
}

Expand Down Expand Up @@ -87,3 +88,14 @@ func (timeline *Timeline) Load(timelineType TimelineType) (error) {

return nil
}

func (timeline *Timeline) Toot(status *string, inReplyTo int, filesToUpload *[]string, visibility *string, sensitive bool, spoiler *string) (*mastodon.Status, error) {
newToot := mastodon.Toot{
Status: *status,
Visibility: *visibility,
Sensitive: sensitive,
SpoilerText: *spoiler,
}

return timeline.client.PostStatus(context.Background(), &newToot)
}
4 changes: 2 additions & 2 deletions tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TUI(tuiCore TUICore) {
if key == tcell.KeyEnter {
cmd := tuiCore.CmdLine.GetText()
tuiCore.CmdLine.SetText("...")
retCode := mast.CmdProcessor(tuiCore.Client, cmd)
retCode := mast.CmdProcessor(&tuiCore.Timeline, cmd)
tuiCore.CmdLine.SetText("")

switch retCode {
Expand Down Expand Up @@ -111,7 +111,7 @@ func TUI(tuiCore TUICore) {
func (tuiCore *TUICore) UpdateTimeline(scrollToEnd bool) bool {
_, _, w, _ := tuiCore.Stream.Box.GetInnerRect()

err := tuiCore.Timeline.Load(mast.TimelineHome)
err := tuiCore.Timeline.Load(tuiCore.Timeline.Type)
if err != nil {
// TODO: Display errors somewhere
return false
Expand Down

0 comments on commit b22aedc

Please sign in to comment.