shithub: hell

Download patch

ref: dd1f814b2de4cab4dde76cd9e78c9c6da2f4455d
parent: cd81887caa011b15968a6c8a926171a8119c516c
author: penny <penny@limitedideas.org>
date: Tue Nov 4 09:14:34 EST 2025

port /url add url processing flag

--- a/commands.go
+++ b/commands.go
@@ -6,6 +6,7 @@
 	"strings"
 	"time"
 	"context"
+	"strconv"
 
 	mastodon "codeberg.org/penny64/hellclient-go-mastodon"
 )
@@ -110,6 +111,8 @@
 	want_recent
 	// Set up a templater for us
 	templater
+	// Load url from url index
+	load_url
 )
 
 type cmdloader struct {
@@ -165,6 +168,8 @@
 		return "", fmt.Errorf("Command %s not found.\n", data.command)
 	}
 	flags := loader.cmdmap[data.command].flags()
+	
+	// Command needs us to resolve @user@domain targets as indexes
 	if flags&acc_resolve != 0 {
 		data.account = data.lookupAccount(loader)
 		if data.status != nil && data.status.ID != data.account.ID {
@@ -171,6 +176,8 @@
 			data.status = nil
 		}
 	}
+	
+	// Command wants us to load the user's most recently made status
 	if flags&want_recent != 0 && !data.found_index {
 		if hc.recentpost != nil {
 			data.status = hc.recentpost
@@ -177,11 +184,25 @@
 			data.account = &hc.recentpost.Account
 		}
 	}
+
+	// Command needs us to set up a post templater
 	if flags&templater != 0 && data.status != nil {
 		formatter := &StatusFormatter{prefs: hc.preferences, status: data.status, postContext: hc.ctxref, localindex: data.index}
 		templater := newStatusTemplateRenderer(formatter)
 		data.templater = templater
 	}
+
+	// Command needs targeturl set
+	if flags&load_url != 0 && data.status != nil {
+		urlindex, err := strconv.Atoi(data.content)
+		if err != nil {
+			urlindex = 1
+		}
+		if urlindex <= len(hc.homeref.urlmap[data.index]) {
+			data.targeturl = hc.homeref.urlmap[data.index][urlindex-1]
+		}
+	}
+		
 	err = data.checkReqs(loader.cmdmap[data.command].flags())
 	if err != nil {
 		return "", err
@@ -254,6 +275,7 @@
 	found_index  bool
 	dot_index    bool
 	index        string
+	targeturl string
 	templater    *templateRenderer
 }
 
@@ -287,6 +309,10 @@
 		return fmt.Errorf("%s requires an argument\n", cmd.command)
 	}
 
+	if (flags&load_url != 0) && (cmd.targeturl == "") {
+		return fmt.Errorf("%s: bad url index\n", cmd.command)
+	}
+
 	return nil
 }
 
@@ -787,12 +813,12 @@
 	cmd.doer = func(data *cmddata) string {
 		var err error
 		markfunc := func() { _, err = hc.client.Bookmark(context.Background(), data.status.ID) }
-			hc.dispatchAnon(markfunc).Wait()
-			if err != nil {
-				return fmt.Sprintf("err: %s\n", err)
-			} 
-			line, _ := data.templater.render("Bookmarked: $index $username $content $media_descriptions\n")
-			return line
+		hc.dispatchAnon(markfunc).Wait()
+		if err != nil {
+			return fmt.Sprintf("err: %s\n", err)
+		} 
+		line, _ := data.templater.render("Bookmarked: $index $username $content $media_descriptions\n")
+		return line
 	}
 	return cmd
 }
@@ -804,17 +830,56 @@
 	cmd.bflags = status | templater
 	cmd.doer = func(data *cmddata) string {
 		var err error
-		markfunc := func() { _, err = hc.client.Unbookmark(context.Background(), data.status.ID) }
-			hc.dispatchAnon(markfunc).Wait()
-			if err != nil {
-				return fmt.Sprintf("err: %s\n", err)
-			} 
-			line, _ := data.templater.render("Unbookmarked: $index $username $content $media_descriptions\n")
-			return line
+		var postCopy *mastodon.Status
+		unmarkfunc := func() {
+			postCopy, err = hc.client.GetStatus(context.Background(), data.status.ID)
+		}
+		hc.dispatchAnon(unmarkfunc).Wait()
+
+		if err != nil {
+			return fmt.Sprintf("err: %s\n", err)
+		}
+
+		if !postCopy.Bookmarked.(bool) {
+			return "Status not bookmarked.\n"
+		}
+		
+		unmarkfunc = func() { _, err = hc.client.Unbookmark(context.Background(), data.status.ID) }
+		hc.dispatchAnon(unmarkfunc).Wait()
+		if err != nil {
+			return fmt.Sprintf("err: %s\n", err)
+		} 
+		line, _ := data.templater.render("Unbookmarked: $index $username $content $media_descriptions\n")
+		return line
 	}
 	return cmd
 }
 
+func (hc *Hellclient) statusurlcmd(name string, command *string,) cmder {
+	cmd := &basiccmd{}
+	cmd.hc = hc
+	cmd.bname = name
+	cmd.bflags = status
+	cmd.doer = func(data *cmddata) string {
+		url := fmt.Sprintf("%v/statuses/%v", hc.client.Config.Server, data.status.ID)
+		openItemInOS(*command, url)
+		return ""
+	}
+	return cmd
+}
+
+func (hc *Hellclient) urlcmd(name string, command *string,) cmder {
+	cmd := &basiccmd{}
+	cmd.hc = hc
+	cmd.bname = name
+	cmd.bflags = load_url | status
+	cmd.doer = func(data *cmddata) string {
+		openItemInOS(*command, data.targeturl)
+		return ""
+	}
+	return cmd
+}
+
 // Commmands are lazy evaluated in this order
 // Single/two letter matches need to match the most common commands
 func (hc *Hellclient) newCmdArray() []cmder {
@@ -846,6 +911,8 @@
 		hc.homecmd(),
 		hc.localcmd(),
 		hc.publiccmd(),
+		hc.statusurlcmd("open", &hc.preferences.Browser),
+		hc.urlcmd("url", &hc.preferences.Browser),
 	}
 	return cmdarray
 }
--- a/main.go
+++ b/main.go
@@ -124,61 +124,10 @@
 
 			//Commands require status indexes
 			switch command {
-			case "mark":
-				if !postOK {
-					fmt.Printf("mark requires a status to operate on\n")
-					return
-				}
-				markfunc := func() { _, err = client.Bookmark(context.Background(), postItem.ID) }
-				hc.dispatchAnon(markfunc).Wait()
-				if err != nil {
-					printMastodonErr(err)
-				} else {
-					line, _ := templater.render("Bookmarked: $index $username $content $media_descriptions\n")
-					fmt.Print(line)
-				}
-				return
-			case "unmark":
-				if !postOK {
-					fmt.Printf("unmark requires a status to operate on\n")
-					return
-				}
-				var postCopy *mastodon.Status
-				unmarkfunc := func() {
-					postCopy, err = client.GetStatus(context.Background(), postItem.ID)
-				}
-				hc.dispatchAnon(unmarkfunc).Wait()
-				if err != nil {
-					fmt.Printf("Error removing bookmark: %s\n", err)
-					return
-				}
-				if !postCopy.Bookmarked.(bool) {
-					fmt.Printf("Post not bookmarked.\n")
-					return
-				}
-				hc.dispatchAnon(func() { _, err = client.Unbookmark(context.Background(), postItem.ID) }).Wait()
-				if err != nil {
-					printMastodonErr(err)
-				} else {
-					line, _ := templater.render("Unbookmarked: $index $username $content $media_descriptions\n")
-					fmt.Print(line)
-				}
-				return
-			case "open":
-				if !postOK {
-					fmt.Printf("open requires a status to operate on\n")
-					return
-				}
-
-				url := fmt.Sprintf("%v/statuses/%v", client.Config.Server, postItem.ID)
-				openItemInOS(hc.preferences.Browser, url)
-				return
 			case "url":
 				_, indexstr, _ := strings.Cut(arguments, " ")
-				urlindex, err := strconv.Atoi(indexstr)
-				if err != nil {
-					urlindex = 1
-				}
+				urlindex, _ := strconv.Atoi(indexstr)
+
 				if urlindex > len(hc.homeref.urlmap[index]) {
 					fmt.Printf("Bad url index\n")
 					return
--