Last active
March 22, 2022 19:30
-
-
Save NonLogicalDev/ee1e8eab62d9b349b705270e87319d05 to your computer and use it in GitHub Desktop.
osc52 is a utility for copying to system clipboard via osc52 escape sequences.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# | |
# osc52 is a utility for copying to system clipboard via osc52 escape sequences. | |
# | |
# If your terminal supports these sequences this enables you | |
# to copy things from remote SSH sessions to system clipboard. | |
# | |
# Convenient when you're using lots of in-browser terminals or working on a remote server. | |
# | |
# Usage: | |
# osc52 [FILE] | |
# echo "data" | osc52 | |
# | |
# credits: https://gist.github.com/KirinDave/cc087efb43c364aadbc57bb497d7e4c3 | |
# credits: https://github.com/prashantv/dotfiles/blob/d29e6d8d0693cb9b8a297ec761df59a62b506703/bin/clip | |
set -eu | |
# MAXLEN=74994 | |
MAXLEN=$(( 73 * 1024 )) | |
TARGET=${1:-} | |
DATA="" | |
if [[ $TARGET == "-" || $TARGET == "" ]]; then | |
DATA=$( cat ) | |
LEN=$( echo "$DATA" | wc -c ) | |
TARGET='STDIN' | |
else | |
if [[ ! -r $TARGET ]]; then | |
echo "Can not read file: $1" | |
exit 1 | |
fi | |
DATA=$( cat "$TARGET" ) | |
LEN=$( cat "$TARGET" | wc -c ) | |
fi | |
echo "Copying $TARGET to clipboard" >&2 | |
if [[ "$LEN" -gt "$MAXLEN" ]]; then | |
printf "Input is %d bytes too long" "$(( LEN - MAXLEN ))" >&2 | |
fi | |
OSC_DATA=$(printf %s "$DATA" | head -c $MAXLEN | base64 | tr -d '\n\r') | |
# If fd 1 is a terminal, print directly | |
if [[ -t 1 ]]; then | |
printf "\033]52;c;%s\a" "$OSC_DATA" | |
exit 0 | |
fi | |
# Otherwise, print to /dev/tty | |
printf "\033]52;c;%s\a" "$OSC_DATA" > /dev/tty |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment