ref: 0c41a08ec8efa56afd65153a7e9ecd3d949baded
parent: 6cc3988782312323f3d6453c8bc64f33f3c4d5ba
author: penny <penny@limitedideas.org>
date: Mon Aug 11 16:55:39 EDT 2025
Add /url to open urls in statuses
--- a/commands.go
+++ b/commands.go
@@ -4,7 +4,7 @@
"strings"
)
-var commands = []string{"examine", "reply", "like", "thread", "open", "preview", "download", "dm", "rt", "parent", "children", "rm", "mark", "unmark", "account", "vim", "import", "pause", "resume"}+var commands = []string{"examine", "reply", "like", "thread", "open", "preview", "download", "dm", "rt", "parent", "children", "rm", "mark", "unmark", "account", "vim", "import", "pause", "resume", "url"} func processInput(input string) (command string, arguments string) {--- a/hellclient.go
+++ b/hellclient.go
@@ -21,6 +21,7 @@
block sync.Mutex
homeMap map[string]*mastodon.Status
+ urlMap map[string][]string
debugMap map[string]interface{}actionBuffer []func()
}
@@ -29,10 +30,11 @@
rl, err := readline.New("> ")homeMap := make(map[string]*mastodon.Status)
debugMap := make(map[string]interface{})+ urlMap := make(map[string][]string)
if err != nil {return nil, err
}
- return &Hellclient{rl: rl, homeMap: homeMap, debugMap: debugMap, isPaused: false}, nil+ return &Hellclient{rl: rl, homeMap: homeMap, debugMap: debugMap, isPaused: false, urlMap: urlMap}, nil}
func (hc *Hellclient) updatePrompt() {--- a/main.go
+++ b/main.go
@@ -7,6 +7,7 @@
"os"
"os/exec"
"strings"
+ "strconv"
"github.com/mattn/go-mastodon"
)
@@ -175,6 +176,18 @@
case "open":
url := fmt.Sprintf("%v/statuses/%v", client.Config.Server, postItem.ID) cmd := exec.Command("open", url, "-a", "Eldritch Café")+ cmd.Run()
+ case "url":
+ _, indexstr, _ := strings.Cut(arguments, " ")
+ urlindex, err := strconv.Atoi(indexstr)
+ if err != nil {+ urlindex = 1
+ }
+ if urlindex > len(hc.urlMap[index]) {+ fmt.Printf("Bad url index\n")+ return
+ }
+ cmd := exec.Command("open", hc.urlMap[index][urlindex-1])cmd.Run()
case "preview":
err := hc.previewPostImages(postItem, "open -W -a \"Quick Look\"")
--- a/mastodon.go
+++ b/mastodon.go
@@ -73,22 +73,43 @@
return c
}
-func (hc *Hellclient) renderStatus(content string) string {+func (hc *Hellclient) renderStatus(content string, index string) string {doc, err := html.Parse(strings.NewReader(content))
if err != nil {log.Fatal(err)
}
-
+
+ //clear out the url map
+ hc.urlMap[index] = []string{}+
for node := range doc.Descendants() {- if node.Data == "a" {+ if node.Data == "a" && node.Type == html.ElementNode {+ ismention := false
+ href := ""
for attr := range node.Attr { if node.Attr[attr].Key == "class" && strings.Contains(node.Attr[attr].Val, "mention") {node.Data = "span"
+ ismention = true
+ continue
}
+ if node.Attr[attr].Key == "href" {+ href = node.Attr[attr].Val
+ }
}
+ if !ismention {+ hc.urlMap[index] = append(hc.urlMap[index], href)
+ refnode := &html.Node{+ Type: html.TextNode,
+ Data: fmt.Sprintf(" [%v]", len(hc.urlMap[index]))}+ if node.Parent != nil {+ node.Parent.InsertBefore(refnode, node.NextSibling)
+ }
+ }
}
}
+
+
//Rip off the HTML body the parser made for us
for node := range doc.Descendants() { if node.Data == "body" {@@ -179,7 +200,7 @@
}
func (hc *Hellclient) formatStatusDetailed(post *mastodon.Status, index string, prefix string) string {- renderedPost := hc.renderStatus(post.Content)
+ renderedPost := hc.renderStatus(post.Content, index)
return fmt.Sprintf("%v%v <%v> %v", index, prefix, post.Account.Username, html2text.HTML2Text(renderedPost))}
@@ -198,7 +219,7 @@
func (hc *Hellclient) printPostDetailed(postref string, post *mastodon.Status, prefix string) *mastodon.Status {post, plaintext := hc.RenderPostPlaintext(post, postref, prefix)
- fmt.Println(plaintext)
+ fmt.Println(hyphenate(plaintext))
return post
}
@@ -224,7 +245,7 @@
postfix += "🖼️"
}
- plaintext = hyphenate(fmt.Sprintf("%v %v", poststring, postfix))+ plaintext = fmt.Sprintf("%v %v", poststring, postfix)return
}
--
⑨