ref: 68af9a80aa9208dc4cf9380c9039347c63364142
parent: 320d4e0626afa1fac878bafe0b358a504e8673aa
author: penny <penny@limitedideas.org>
date: Sat Nov 1 02:42:47 EDT 2025
initial port of /profile to new command system
--- a/commands.go
+++ b/commands.go
@@ -94,41 +94,59 @@
return
}
-
type cmdflag uint8
const (
- status cmdflag = 1 << iota
+ free cmdflag = 1 << iota
+ status
account
argument
)
type cmdloader struct {- hc *Hellclient
- lastindex string
+ hc *Hellclient
+ lastindex string
lastaccount *mastodon.Account
}
-func (loader *cmdloader) processLine(line string) (*cmddata, error) {+func (data *cmddata) lookupAccount(loader *cmdloader) *mastodon.Account {+ if data.found_index {+ return data.account
+ }
+ if loader.lastaccount != nil {+ return loader.lastaccount
+ }
+ account := loader.hc.resolveAccount(data.index)
+ if account == nil {+ return data.account
+ }
+ loader.lastaccount = account
+ loader.lastindex = ""
+ return account
+}
+
+func (loader *cmdloader) processLine(line string) (*cmddata, error) {command, arguments, _ := processInput(line)
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 {+ dot_index = true
index = loader.lastindex
}
postItem, postOK := loader.hc.homeMap[index]
- //foundindex := false
+ foundindex := false
//If there's no index selected load the last post we operated on
if postOK {- //foundindex = true
+ foundindex = true
loader.lastindex = index
loader.lastaccount = nil
} else {postItem, postOK = loader.hc.homeMap[loader.lastindex]
- }
+ }
var reblogger *mastodon.Status
//Okay now see if the post we end up with is a reblog
if postOK {@@ -139,9 +157,12 @@
}
cmdctx := &cmddata{- command: command,
- content: content,
+ command: command,
+ content: content,
raw_argument: arguments,
+ dot_index: dot_index,
+ found_index: foundindex,
+ index: index,
}
if loader.lastaccount != nil {cmdctx.account = loader.lastaccount
@@ -150,7 +171,7 @@
cmdctx.status = postItem
cmdctx.account = &postItem.Account
cmdctx.reblogger = reblogger
- cmdctx.index = index
+
}
return cmdctx, nil
@@ -157,18 +178,19 @@
}
type cmddata struct {- status *mastodon.Status
- account *mastodon.Account
- reblogger *mastodon.Status
+ status *mastodon.Status
+ account *mastodon.Account
+ reblogger *mastodon.Status
raw_argument string
- command string
- content string
- found_index bool
- index string
+ command string
+ content string
+ found_index bool
+ dot_index bool
+ index string
}
type cmd struct {- data *cmddata
+ data *cmddata
cmder cmder
}
@@ -183,6 +205,9 @@
}
func (cmd *cmd) checkReqs() (err error) {+ if(cmd.cmder.flags()&free !=0) {+ return nil
+ }
if (cmd.cmder.flags()&status != 0) && (cmd.cmder.flags()&account != 0) { if cmd.data.status == nil && cmd.data.account == nil { return fmt.Errorf("%s requires a status or an account", cmd.cmder.name())@@ -227,4 +252,26 @@
fmt.Print(hc.page.String())
hc.pause(true)
return ""
+}
+
+type profilecmd struct {+ *Hellclient
+ *cmdloader
+}
+
+func (cmd *profilecmd) name() string {+ return "profile"
+}
+
+func (cmd *profilecmd) flags() cmdflag {+ return free
+}
+
+func (cmd *profilecmd) result(data *cmddata) string {+ hc := cmd.Hellclient
+ account := data.lookupAccount(cmd.cmdloader)
+ if account == nil {+ return fmt.Sprintf("Account lookup failed.\n")+ }
+ return fmt.Sprint(hc.formatAccount(account))
}
\ No newline at end of file
--- a/hellclient.go
+++ b/hellclient.go
@@ -34,6 +34,7 @@
preferences *Hellprefs
multiLineMode bool
configPath string
+ cmdload *cmdloader
//Global status map for status indexes
//Needs to be converted to a postref struct
@@ -126,6 +127,9 @@
hc.prompt.UpdatePrompt()
//start up post dispatcher
go hc.clientDispatch()
+
+ //command processor
+ hc.cmdload = &cmdloader{hc: &hc} markers, err := hc.client.GetTimelineMarkers(context.Background(), []string{"home"}) if err != nil {--- a/main.go
+++ b/main.go
@@ -91,8 +91,7 @@
}
}
- cmdload := &cmdloader{hc: hc}- cmdctx, _ := cmdload.processLine(line)
+ cmdctx, _ := hc.cmdload.processLine(line)
hc.PrintObjectProperties(cmdctx.account)
hc.PrintObjectProperties(cmdctx.status)
hc.PrintObjectProperties(cmdctx.reblogger)
@@ -100,8 +99,6 @@
fmt.Println(cmdctx.command)
fmt.Println(cmdctx.content)
fmt.Println(cmdctx.index)
- return
-
accByNameOrRef := func() (account *mastodon.Account) { if lastaccount != nil {@@ -306,11 +303,6 @@
return
}
- if arguments == "" && !postOK && lastaccount == nil {- fmt.Printf("%v requires an argument\n", command)- return
- }
-
//Commands that don't take an index
switch command {}
@@ -611,12 +603,13 @@
fmt.Print(hc.page.String())
return
case "profile":
- account := accByNameOrRef()
- if account == nil {- fmt.Printf("Account lookup failed.\n")- return
- }
- fmt.Print(hc.formatAccount(account))
+ profile := cmd{data: cmdctx, cmder: &profilecmd{hc, hc.cmdload}}+ err := profile.checkReqs()
+ if err != nil {+ fmt.Println(err)
+ return
+ }
+ fmt.Printf("%s", profile.String())return
case "account":
account := accByNameOrRef()
--
⑨