shithub: hell

Download patch

ref: 1e7f89761297581bb83d3d3de8819d134ca5677e
parent: 7b71c726d69844921bd959ab0cc9b561a3d03e52
parent: 21ecafa695c946f9ca69d8bc6f80ec0b4cffd41f
author: penny64 <penny64@noreply.codeberg.org>
date: Fri Oct 3 23:58:35 EDT 2025

Merge pull request 'Graphical file picker' (#3) from file_attachments into main

Reviewed-on: https://codeberg.org/penny64/hellclient/pulls/3

--- a/config.go
+++ b/config.go
@@ -19,6 +19,7 @@
 	Browser       string  `toml:"browser" comment:"System browser command."`
 	ImageViewer   string  `toml:"image_viewer" comment:"System image viewer command."`
 	MediaImport   string  `toml:"media_importer" comment:"Alternative media command used with /import"`
+	FilePicker    string  `toml:"file_picker" comment:"Graphical file picker (or otherwise)"`
 }
 
 type account_array struct {
@@ -64,6 +65,11 @@
 		prefs.MediaImport = defaultprefs.MediaImport
 		fixed = true
 	}
+	
+	if prefs.FilePicker == "" {
+		prefs.FilePicker = defaultprefs.FilePicker
+		fixed = true
+	}
 	return prefs, fixed
 }
 
@@ -74,6 +80,7 @@
 		Browser:       "xdg-open '%s'",
 		ImageViewer:   "xdg-open '%s'",
 		MediaImport:   "xdg-open '%s'",
+		FilePicker:    "zenity --file-selection",
 	}
 	return *prefs
 }
--- a/filehandler.go
+++ b/filehandler.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+	"context"
 	"fmt"
 	"io"
 	"net/http"
@@ -9,13 +10,23 @@
 	"os/exec"
 	"path"
 	"path/filepath"
-	"context"
-	"bytes"
+	"strings"
 
 	mastodon "codeberg.org/penny64/hellclient-go-mastodon"
 	"github.com/google/shlex"
 )
 
+func pickFilename(command string) (string, error) {
+	cmdargv, err := shlex.Split(command)
+	if err != nil {
+		return "", err
+	}
+	cmd := exec.Command(cmdargv[0], cmdargv[1:]...)
+	filepath, err := cmd.Output()
+	filepathstring := strings.TrimSpace(string(filepath))
+	filepathstring = path.Clean(filepathstring)
+	return string(filepathstring), err
+}
 
 func openItemInOS(command string, url string) {
 	cmdstring := fmt.Sprintf(command, url)
@@ -148,13 +159,13 @@
 
 // Return a byte array and error given a filepath
 // Expands ~ and environmental variables
-func loadFile(path string) (*[]byte, error) {
+func loadFile(path string) (*os.File, error) {
 	path = expandDir(path)
-	data, err := os.ReadFile(path)
+	file, err := os.Open(path)
 	if err != nil {
 		return nil, fmt.Errorf("read error: %s: %s", path, err)
 	}
-	return &data, nil
+	return file, nil
 }
 
 // Upload media by path to the mastodon server
@@ -163,11 +174,11 @@
 	if err != nil {
 		return err
 	}
+	defer file.Close()
 	config := sam.rl.GetConfig()
 	config.Prompt = "Image description: "
 	description, _ := sam.rl.ReadLineWithConfig(config)
-	reader := bytes.NewReader(*file)
-	media := &mastodon.Media{File: reader, Description: description}
+	media := &mastodon.Media{File: file, Description: description}
 	attachment, err := sam.client.UploadMediaFromMedia(context.Background(), media)
 	sam.attachments = append(sam.attachments, attachment)
 	if err != nil {
--- a/hellclient.go
+++ b/hellclient.go
@@ -168,7 +168,7 @@
 	}
 
 	prompt := &PromptBar{prompt: "Hell> ", rl: rl}
-	
+
 	attacher := &StatusAttachmentHolder{Hellclient: &hc}
 
 	prompt.items = []PromptItem{
--- a/help.go
+++ b/help.go
@@ -18,6 +18,9 @@
   /download <index>            Download attached files/media from status.
   /view <index>                Preview status media in media viewer.
   /import <index>              Same as /view with an alternative media handler.
+  /attach [index]              Loads file for attaching to status.
+                               Opens the configured graphical file picker if no index is given.
+                               
   /dm <content>                Create a status with direct visibility (e.g. a dm).
   /rt <index>                  Boost a status by index.
   /parent <index>              View parent of status by index.
--- a/main.go
+++ b/main.go
@@ -99,6 +99,12 @@
 			//Contextual commands that need to handle their own requirements
 			switch command {
 			case "attach":
+				if arguments == "" {
+					arguments, err = pickFilename(hc.preferences.FilePicker)
+				}
+				if err != nil {
+					fmt.Printf("File picking error: %s\n", err)
+				}
 				err := hc.attacher.uploadAttachment(arguments)
 				if err != nil {
 					fmt.Printf("Upload error: %s\n", err)
--- a/prompt.go
+++ b/prompt.go
@@ -68,6 +68,7 @@
 type PauseIndicator struct {
 	*Hellclient
 }
+
 // Display whether the client is paused or not
 func (pi *PauseIndicator) Update() (string, bool, error) {
 	if pi.isPaused {
@@ -82,7 +83,7 @@
 		return "", false, nil
 	}
 	var sb strings.Builder
-	
+
 	sb.WriteString("ATTACHED:[")
 	for item := range sam.attachments {
 		sb.WriteString(sam.attachments[item].Type)
--