commit 4d7cfcc6a05a3ca6bcefcf70a5da175336fd90d6
parent 50da581db6c2058d325a817e17e0aa83cb6c0784
Author: Luxferre <lux@ferre>
Date:   Sat,  1 Apr 2023 09:40:19 +0300
Added optinal clipboard support
Diffstat:
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
@@ -19,6 +19,7 @@ Improvements over the original Bopher from that post:
 - status bar with currently opened resource name
 - ability to accept `gopher://` URLs from the command line
 - ability to save the URL to the currently viewed resource into the stash text file
+- optional clipboard support when stashing (only if you set `BOPHER_CLIP` envvar)
 - ability to force-download pages instead of viewing regardless of their type
 - ability to display 8-type Gophermap entries as `telnet://` URIs (not opening anything for security reasons)
 
@@ -78,10 +79,21 @@ No. Besides the ambiguity problem, it will also introduce huge external dependen
 
 ## What is the link stash?
 
-Link stash is a viable and interoperable alternative to both bookmarks and clipboard that can be implemented with pure Bash. Essentially, it's an append-only text file at a fixed location (`~/.bopher-links.txt` by default) where the user can instruct Bopher-NG to save the link to the currently viewed resource. For your convenience, links are stashed with a UTC-based timestamp and in the `gopher://` format, so they can be copied from the file later and used in other browsers. As of now, every stash file line is a valid Gophermap line, so can easily share your findings on the Gopherspace if you need to.
+Link stash is a viable and interoperable alternative to both bookmarks and clipboard that can be implemented with pure Bash. Essentially, it's an append-only text file at a fixed location (`~/.bopher-links.txt` by default) where the user can instruct Bopher-NG to save the link to the currently viewed resource. For your convenience, links are stashed with a UTC-based timestamp and in the `gopher://` format, so they can be copied from the file later and used in other browsers. As of now, every stash file line is a valid Gophermap line, so can easily share your findings on the Gopherspace whenever you want.
 
 You can override the `BOPHER_LINKSTASH` environment variable to change the location and name of this file if you need to.
 
+## What about the "normal" system clipboard?
+
+This functionality is too OS/environment-specific and thus can't be built **into** Bopher-NG. Since April 2023, however, Bopher-NG supports copying the current URL to the OS/env clipboard at the same time as stashing, if **and only if** you set the `BOPHER_CLIP` environment variable before invocation. This variable must be set to a valid command that performs copying the standard input to the system clipboard. If you plan on using it constantly, I suggest to add one of the following values to your `.bashrc`:
+
+- for Linux + X11 GUI: `export BOPHER_CLIP='xsel -bi'`
+- for Linux + Wayland (with `wl-clipboard` package): `export BOPHER_CLIP=wlcopy`
+- for macOS: `export BOPHER_CLIP=pbcopy`
+- for GNU Screen environment (on any OS): `export BOPHER_CLIP='(read -rsd$'\x04' x; screen -X register . "$x")`
+
+Even then, this functionality isn't guaranteed to work on any system. Please verify by yourselves how your operating environment handles the clipboard and adjust this variable accordingly.
+
 ## Where do the downloads go?
 
 By default, they go to your current working directory, i.e. the directory you are running this script from. To override this with a fixed path, set the `BOPHER_DLDIR` environment variable.
@@ -95,7 +107,7 @@ Bopher-NG's mouse input is supported on any terminals that support the 1000 **an
 - macOS Terminal;
 - OpenGL-based xterm-compatible terminal emulators like WezTerm, Kitty, Alacritty etc.
 
-Note that this support is required at the client side only. For instance, you can safely run `bopher-ng.sh` on any compatible Bash version in an SSH session on your terminal and still be able to use mouse there.
+Note that, unlike clipboard, this support is required at the client side only. For instance, you can safely run `bopher-ng.sh` on any compatible Bash version in an SSH session on your terminal and still be able to use mouse there.
 
 ## What is the license on this?
 
diff --git a/bopher-ng.sh b/bopher-ng.sh
@@ -13,6 +13,7 @@
 # - status bar with currently opened resource name
 # - ability to accept gopher:// URLs from the command line
 # - ability to save the URL to the currently viewed resource into the stash text file
+# - optional clipboard support when stashing (only if you set BOPHER_CLIP envvar)
 # - ability to force-download pages instead of viewing regardless of their type
 # - displaying type 8 entries (as per RFC1436) as the telnet:// URI scheme
 
@@ -312,6 +313,7 @@ stashlink() { # stash the link to the currently viewed resource
   [[ -z "$desc" ]] && desc='Unnamed'
   printf '%s [%s] %s - %s\t%s\t%s\t%s\r\n' "${fields[5]}" "$(date -u '+%F %T')" "$desc" "$gopherlink" "${fields[4]}" "${fields[2]}" "${fields[3]}" >> $BOPHER_LINKSTASH
   printf 'Stashed %s - %s%s' "$desc" "$gopherlink" "$ERESET${ESC}[$TERMROWS;0H" # just reset the cursor to the last line
+  [[ ! -z "$BOPHER_CLIP" ]] && (printf '%s' "$gopherlink" | $BOPHER_CLIP) # run an external clipboard command if it's explicitly specified
 }
 
 # Focus and click a link on a Gophermap