From a9464b0f0133ce37131411ea9d85ec487786eb26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=9E=E3=83=AA=E3=82=A6=E3=82=B9?= Date: Thu, 8 Apr 2021 21:35:27 -0500 Subject: [PATCH] Implemented CLI cmd command --- README.md | 12 ++++++++- cli/cmdCmd.go | 50 +++++++++++++++++++++++++++++++++++++ cli/timelineCmd.go | 33 ------------------------- mast/cmd.go | 61 +++++++++++++++++++++++++++++++++++++++++++++- tui/tui.go | 2 +- 5 files changed, 122 insertions(+), 36 deletions(-) create mode 100644 cli/cmdCmd.go delete mode 100644 cli/timelineCmd.go diff --git a/README.md b/README.md index f1fb7eb..e9909ee 100644 --- a/README.md +++ b/README.md @@ -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!* diff --git a/cli/cmdCmd.go b/cli/cmdCmd.go new file mode 100644 index 0000000..ccec1b6 --- /dev/null +++ b/cli/cmdCmd.go @@ -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) +} diff --git a/cli/timelineCmd.go b/cli/timelineCmd.go deleted file mode 100644 index b60276e..0000000 --- a/cli/timelineCmd.go +++ /dev/null @@ -1,33 +0,0 @@ -package cli - -import ( - "fmt" - - "github.com/spf13/cobra" - "github.com/rivo/tview" - - "github.com/mrusme/gomphotherium/mast" - "github.com/mrusme/gomphotherium/tui" -) - -var timelineCmd = &cobra.Command{ - Use: "timeline", - Short: "Display timeline", - Long: "Display different timelines.", - Run: func(cmd *cobra.Command, args []string) { - timeline := mast.NewTimeline(MastodonClient) - timeline.Switch(mast.TimelineHome, nil) - timeline.Load() - output, err := tui.RenderTimeline(&timeline, 72, flagShowImages) - if err != nil { - panic(err) - } - - fmt.Printf(tview.TranslateANSI(output)) - return - }, -} - -func init() { - rootCmd.AddCommand(timelineCmd) -} diff --git a/mast/cmd.go b/mast/cmd.go index c30a62a..df5e5f4 100644 --- a/mast/cmd.go +++ b/mast/cmd.go @@ -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 = @@ -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] @@ -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 @@ -185,6 +196,10 @@ 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 @@ -192,6 +207,10 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) { return CmdToot(timeline, args, tootId, VisibilityPublic) case "rep", "replyprivate": + if trigger != TriggerTUI { + return CodeTriggerNotSupported + } + tootId, args, err := CmdHelperGetReplyParams(args) if err != nil { return CodeNotOk @@ -199,6 +218,10 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) { return CmdToot(timeline, args, tootId, VisibilityPrivate) case "reu", "replyunlisted": + if trigger != TriggerTUI { + return CodeTriggerNotSupported + } + tootId, args, err := CmdHelperGetReplyParams(args) if err != nil { return CodeNotOk @@ -206,6 +229,10 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) { return CmdToot(timeline, args, tootId, VisibilityUnlisted) case "red", "replydirect": + if trigger != TriggerTUI { + return CodeTriggerNotSupported + } + tootId, args, err := CmdHelperGetReplyParams(args) if err != nil { return CodeNotOk @@ -213,6 +240,10 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) { return CmdToot(timeline, args, tootId, VisibilityDirect) case "rt", "retoot", "boost": + if trigger != TriggerTUI { + return CodeTriggerNotSupported + } + tootId, err := CmdHelperGetBoostParams(args) if err != nil { return CodeNotOk @@ -220,6 +251,10 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) { return CmdBoost(timeline, tootId) case "ut", "unretoot", "unboost": + if trigger != TriggerTUI { + return CodeTriggerNotSupported + } + tootId, err := CmdHelperGetBoostParams(args) if err != nil { return CodeNotOk @@ -227,6 +262,10 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) { return CmdUnboost(timeline, tootId) case "fav": + if trigger != TriggerTUI { + return CodeTriggerNotSupported + } + tootId, err := CmdHelperGetFavParams(args) if err != nil { return CodeNotOk @@ -234,6 +273,10 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) { return CmdFav(timeline, tootId) case "unfav": + if trigger != TriggerTUI { + return CodeTriggerNotSupported + } + tootId, err := CmdHelperGetFavParams(args) if err != nil { return CodeNotOk @@ -241,6 +284,10 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) { return CmdUnfav(timeline, tootId) case "open": + if trigger != TriggerTUI { + return CodeTriggerNotSupported + } + tootId, err := CmdHelperGetOpenParams(args) if err != nil { return CodeNotOk @@ -248,6 +295,10 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) { return CmdOpen(timeline, tootId) case "share": + if trigger != TriggerTUI { + return CodeTriggerNotSupported + } + tootId, err := CmdHelperGetShareParams(args) if err != nil { return CodeNotOk @@ -255,8 +306,16 @@ func CmdProcessor(timeline *Timeline, input string) (CmdReturnCode) { 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 } diff --git a/tui/tui.go b/tui/tui.go index a0547db..af58aa7 100644 --- a/tui/tui.go +++ b/tui/tui.go @@ -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: