ref: fe899e66e690d446fd1d0375c06d451ec9d15ef7
parent: 2d442f70d48d2e40466c87cf9abe5b04d70b8b34
author: penny <penny@limitedideas.org>
date: Sat Nov 1 08:39:30 EDT 2025
process lines in new command system
--- a/commands.go
+++ b/commands.go
@@ -9,7 +9,7 @@
var commands = []string{"examine", "reply", "like", "thread", "open", "prev", "download", "dm", "rt", "hrt", "parent", "children", "rm", "mark", "unmark", "account", "import", "pause", "resume", "url", "fpost", "ufpost", "edit", "notice", "stats", "next", "view", "bookmarks", "follow", "unfollow", "likes", "help", "reload", "attach", "detach", "pinned", "cat", "play", "translate", "read", "version", "local", "public", "block", "unblock", "unlike", "home", "page", "profile"}-func processInput(input string) (command string, arguments string, found bool) {+func processInput(input string, commands []string) (command string, arguments string, found bool) { if input == "" {command = ""
@@ -107,11 +107,24 @@
hc *Hellclient
lastindex string
lastaccount *mastodon.Account
+ commands []string
+ cmdmap map[string]cmder
+
}
+
+func (loader *cmdloader) init(cmds []cmder) {+ loader.cmdmap = make(map[string]cmder)
+ for i, _ := range cmds {+ name := cmds[i].name()
+ loader.commands = append(loader.commands, name)
+ loader.cmdmap[name] = cmds[i]
+ }
+}
+
// Cases
-// Return loaded account if tag was foundor was empty but loaded
+// Return loaded account if tag was found or was empty but loaded
// Return loaded account if index was empty
-// If there's no last account, load the account by index and return it
+// Load lastaccount if it exists or try to look one up, return account or nil
func (data *cmddata) lookupAccount(loader *cmdloader) *mastodon.Account { if data.found_index {@@ -134,15 +147,27 @@
return account
}
+func (loader *cmdloader) run(line string) (string, error) {+ data, err := loader.processLine(line)
+ if err != nil {+ return "", nil
+ }
+ if !data.cmd_found {+ return "", fmt.Errorf("Command %s not found.\n", data.command)+ }
+ return loader.cmdmap[data.command].result(data), nil
+}
+
func (loader *cmdloader) processLine(line string) (*cmddata, error) {- command, arguments, _ := processInput(line)
+ command, arguments, found := processInput(line, loader.commands)
index, content, _ := strings.Cut(arguments, " ")
- fmt.Printf("index: %s\n", index)// "." refers to most recently acted on status
// but if we have an account set, we don't want to set an index!
var dot_index bool
- if index == "." && loader.lastaccount == nil {+ if index == "." {dot_index = true
+ }
+ if index == "." && loader.lastaccount == nil {index = loader.lastindex
}
@@ -172,6 +197,7 @@
dot_index: dot_index,
found_index: foundindex,
index: index,
+ cmd_found: found,
}
if loader.lastaccount != nil {cmdctx.account = loader.lastaccount
@@ -192,6 +218,7 @@
reblogger *mastodon.Status
raw_argument string
command string
+ cmd_found bool
content string
found_index bool
dot_index bool
@@ -285,4 +312,12 @@
return fmt.Sprintf("Account lookup failed.\n")}
return fmt.Sprint(hc.formatAccount(account))
+}
+
+func (hc *Hellclient) newCmdArray() ([]cmder) {+ cmdarray := []cmder{+ &profilecmd{hc, hc.cmdload},+ &dmcmd{hc},+ }
+ return cmdarray
}
\ No newline at end of file
--- a/hellclient.go
+++ b/hellclient.go
@@ -130,6 +130,7 @@
//command processor
hc.cmdload = &cmdloader{hc: &hc}+ hc.cmdload.init(hc.newCmdArray())
markers, err := hc.client.GetTimelineMarkers(context.Background(), []string{"home"}) if err != nil {--- a/main.go
+++ b/main.go
@@ -36,7 +36,7 @@
func() {line, err := rl.Readline()
- command, arguments, found := processInput(line)
+ command, arguments, found := processInput(line, commands)
//empty line
if command == "" && arguments == "" && err == nil {@@ -53,6 +53,13 @@
hc.dispatchStatus(line, "public")
return
}
+
+ result, err := hc.cmdload.run(line)
+ if err != nil {+ fmt.Print(err)
+ }
+ fmt.Printf(result)
+ return
if !found { fmt.Printf("Command not found: \"%s\"\n", command)--
⑨