Skip to content

Commit

Permalink
Enhanced processor, implemented tooting
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Apr 3, 2021
1 parent 7607c1d commit 9235494
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 10 deletions.
6 changes: 4 additions & 2 deletions cli/tuiCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ var tuiCmd = &cobra.Command{
Short: "Launch TUI",
Long: "Launch TUI.",
Run: func(cmd *cobra.Command, args []string) {
tuiCore := tui.TUICore{}
tui.TUI(tuiCore, MastodonClient)
tuiCore := tui.TUICore{
Client: MastodonClient,
}
tui.TUI(tuiCore)
},
}

Expand Down
64 changes: 60 additions & 4 deletions mast/cmd.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package mast

import (
"context"
"strings"

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

type CmdReturnCode int
const (
CodeOk CmdReturnCode = 0
CodeNotOk = 1
CodeQuit = -1
)

Expand All @@ -20,6 +24,16 @@ func CmdAvailable() ([]string) {

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

"tp",
"tootprivate",

"tu",
"tootunlisted",

"td",
"tootdirect",

"rt",
"retoot",
Expand Down Expand Up @@ -84,14 +98,56 @@ func CmdAutocompleter(input string, knownUsers []string) ([]string) {
return entries
}

func CmdProcessor(input string) (CmdReturnCode) {
split := strings.Split(input, " ")
switch split[0] {
func CmdProcessor(mastodonClient *mastodon.Client, 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":
case "t", "toot", "tootpublic":
return CmdToot(mastodonClient, args, VisibilityPublic)
case "tp", "tootprivate":
return CmdToot(mastodonClient, args, VisibilityPrivate)
case "tu", "tootunlisted":
return CmdToot(mastodonClient, args, VisibilityUnlisted)
case "td", "tootdirect":
return CmdToot(mastodonClient, args, VisibilityUnlisted)
}

return CodeOk
}

func CmdToot(mastodonClient *mastodon.Client, content string, visibility string) (CmdReturnCode) {
var status string = ""
var spoiler string = ""
var sensitive bool = false

splitSensitive := strings.SplitN(content, "~~!", 2)
if len(splitSensitive) == 2 {
sensitive = true
}

splitSpoiler := strings.SplitN(splitSensitive[0], "~~:", 2)
if len(splitSpoiler) == 2 {
spoiler = splitSpoiler[1]
}

status = splitSpoiler[0]

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

_, err := mastodonClient.PostStatus(context.Background(), &newToot)
if err != nil {
return CodeNotOk
}

return CodeOk
}

7 changes: 7 additions & 0 deletions mast/toot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import (
"github.com/mattn/go-mastodon"
)

const (
VisibilityPublic string = "public"
VisibilityPrivate = "private"
VisibilityUnlisted = "unlisted"
VisibilityDirect = "direct"
)

type Toot struct {
client *mastodon.Client

Expand Down
13 changes: 9 additions & 4 deletions tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
)

type TUICore struct {
Client *mastodon.Client
App *tview.Application
CmdLine *tview.InputField
Stream *tview.TextView
Expand All @@ -29,8 +30,8 @@ type TUICore struct {
Timeline mast.Timeline
}

func TUI(tuiCore TUICore, mastodonClient *mastodon.Client) {
tuiCore.Timeline = mast.NewTimeline(mastodonClient)
func TUI(tuiCore TUICore) {
tuiCore.Timeline = mast.NewTimeline(tuiCore.Client)
tuiCore.App = tview.NewApplication()

tuiCore.CmdLine = tview.NewInputField().
Expand All @@ -42,10 +43,14 @@ func TUI(tuiCore TUICore, mastodonClient *mastodon.Client) {
SetDoneFunc(func(key tcell.Key) {
if key == tcell.KeyEnter {
cmd := tuiCore.CmdLine.GetText()
tuiCore.CmdLine.SetText("...")
retCode := mast.CmdProcessor(tuiCore.Client, cmd)
tuiCore.CmdLine.SetText("")
retCode := mast.CmdProcessor(cmd)

if retCode == mast.CodeQuit {
switch retCode {
case mast.CodeOk:
tuiCore.UpdateTimeline(true)
case mast.CodeQuit:
tuiCore.App.Stop();
}
}
Expand Down

0 comments on commit 9235494

Please sign in to comment.