Skip to content

Commit

Permalink
Implemented CLI cmd command
Browse files Browse the repository at this point in the history
  • Loading branch information
mrusme committed Apr 9, 2021
1 parent 2e519d6 commit a9464b0
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 36 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,14 @@ Goodbye!

### CLI

TODO: Document CLI usage. Please use `gomphotherium -h` for now.
The CLI interface allows for exectuion of commands right from the terminal
prompt. Not all commands are supported.


#### Usage Examples

`gomphotherium cmd home` \
Render the home timeline

`gomphotherium cmd t Hello World!` \
Publish a new public toot that says *Hello World!*
50 changes: 50 additions & 0 deletions cli/cmdCmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package cli

import (
"fmt"
"strings"
"os"

"github.com/spf13/cobra"
"github.com/rivo/tview"

"github.com/mrusme/gomphotherium/mast"
"github.com/mrusme/gomphotherium/tui"
)

var cmdCmd = &cobra.Command{
Use: "cmd",
Short: "Run command",
Long: "Run command directly from the command line.",
Run: func(cmd *cobra.Command, args []string) {
timeline := mast.NewTimeline(MastodonClient)
cmdReturn := mast.CmdProcessor(
&timeline,
strings.Join(args, " "),
mast.TriggerCLI,
)

switch cmdReturn {
case mast.CodeOk:
timeline.Load()
output, err := tui.RenderTimeline(&timeline, 72, flagShowImages)
if err != nil {
panic(err)
}

fmt.Printf(tview.TranslateANSI(output))
case mast.CodeTriggerNotSupported:
fmt.Printf("Command not supported from CLI!\n")
os.Exit(-1)
default:
fmt.Printf("%v\n", cmdReturn)
os.Exit(-1)
}

return
},
}

func init() {
rootCmd.AddCommand(cmdCmd)
}
33 changes: 0 additions & 33 deletions cli/timelineCmd.go

This file was deleted.

61 changes: 60 additions & 1 deletion mast/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ const (
CodeNotOk = 1
CodeCommandNotFound = 2
CodeUserNotFound = 3
CodeTriggerNotSupported = 4

CodeQuit = -1
CodeHelp = -2
)

type CmdTrigger int
const (
TriggerCLI = 0
TriggerTUI = 1
)

var CmdContentRegex =
regexp.MustCompile(`(?m)(( {0,1}~#| {0,1}~:)\[([^\[\]]*)\]| {0,1}~!!)`)
var CmdHandleAutoCompletionRegex =
Expand Down Expand Up @@ -116,7 +123,7 @@ func CmdAutocompleter(input string, knownUsers map[string]string) ([]string) {
return entries
}

func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) {
func CmdProcessor(timeline *Timeline, input string, trigger CmdTrigger) (CmdReturnCode) {
split := strings.SplitN(input, " ", 2)
cmd := split[0]

Expand Down Expand Up @@ -152,6 +159,10 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) {
timeline.Switch(TimelineHashtag, &timelineOptions)
return CodeOk
case "whois":
if trigger != TriggerTUI {
return CodeTriggerNotSupported
}

var account *mastodon.Account
var err error

Expand Down Expand Up @@ -185,78 +196,126 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) {
case "td", "tootdirect":
return CmdToot(timeline, args, -1, VisibilityUnlisted)
case "re", "reply":
if trigger != TriggerTUI {
return CodeTriggerNotSupported
}

tootId, args, err := CmdHelperGetReplyParams(args)
if err != nil {
return CodeNotOk
}

return CmdToot(timeline, args, tootId, VisibilityPublic)
case "rep", "replyprivate":
if trigger != TriggerTUI {
return CodeTriggerNotSupported
}

tootId, args, err := CmdHelperGetReplyParams(args)
if err != nil {
return CodeNotOk
}

return CmdToot(timeline, args, tootId, VisibilityPrivate)
case "reu", "replyunlisted":
if trigger != TriggerTUI {
return CodeTriggerNotSupported
}

tootId, args, err := CmdHelperGetReplyParams(args)
if err != nil {
return CodeNotOk
}

return CmdToot(timeline, args, tootId, VisibilityUnlisted)
case "red", "replydirect":
if trigger != TriggerTUI {
return CodeTriggerNotSupported
}

tootId, args, err := CmdHelperGetReplyParams(args)
if err != nil {
return CodeNotOk
}

return CmdToot(timeline, args, tootId, VisibilityDirect)
case "rt", "retoot", "boost":
if trigger != TriggerTUI {
return CodeTriggerNotSupported
}

tootId, err := CmdHelperGetBoostParams(args)
if err != nil {
return CodeNotOk
}

return CmdBoost(timeline, tootId)
case "ut", "unretoot", "unboost":
if trigger != TriggerTUI {
return CodeTriggerNotSupported
}

tootId, err := CmdHelperGetBoostParams(args)
if err != nil {
return CodeNotOk
}

return CmdUnboost(timeline, tootId)
case "fav":
if trigger != TriggerTUI {
return CodeTriggerNotSupported
}

tootId, err := CmdHelperGetFavParams(args)
if err != nil {
return CodeNotOk
}

return CmdFav(timeline, tootId)
case "unfav":
if trigger != TriggerTUI {
return CodeTriggerNotSupported
}

tootId, err := CmdHelperGetFavParams(args)
if err != nil {
return CodeNotOk
}

return CmdUnfav(timeline, tootId)
case "open":
if trigger != TriggerTUI {
return CodeTriggerNotSupported
}

tootId, err := CmdHelperGetOpenParams(args)
if err != nil {
return CodeNotOk
}

return CmdOpen(timeline, tootId)
case "share":
if trigger != TriggerTUI {
return CodeTriggerNotSupported
}

tootId, err := CmdHelperGetShareParams(args)
if err != nil {
return CodeNotOk
}

return CmdShare(timeline, tootId)
case "?", "help":
if trigger != TriggerTUI {
return CodeTriggerNotSupported
}

return CodeHelp
case "quit", "exit", "bye", "q":
if trigger != TriggerTUI {
return CodeTriggerNotSupported
}

return CodeQuit
}

Expand Down
2 changes: 1 addition & 1 deletion tui/tui.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TUI(tuiCore TUICore) {
if key == tcell.KeyEnter {
cmd := tuiCore.CmdLine.GetText()
tuiCore.CmdLine.SetText("")
retCode := mast.CmdProcessor(&tuiCore.Timeline, cmd)
retCode := mast.CmdProcessor(&tuiCore.Timeline, cmd, mast.TriggerTUI)

switch retCode {
case mast.CodeOk:
Expand Down

0 comments on commit a9464b0

Please sign in to comment.