Skip to content

Instantly share code, notes, and snippets.

@palmin
Last active September 25, 2022 07:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save palmin/46c2d0f069d0ba6b009f9295d90e171a to your computer and use it in GitHub Desktop.
Save palmin/46c2d0f069d0ba6b009f9295d90e171a to your computer and use it in GitHub Desktop.
# include this from .bashrc, .zshrc or
# another shell startup file
# this script does nothing outside ShellFish
if [ "$LC_TERMINAL" = "ShellFish" ]; then
printURIComponent() {
awk 'BEGIN {while (y++ < 125) z[sprintf("%c", y)] = y
    while (y = substr(ARGV[1], ++j, 1))
    q = y ~ /[a-zA-Z0-9]/ ? q y : q sprintf("%%%02X", z[y])
    printf("%s", q)}' "$1"
}
printOSC() {
if [ -n "$TMUX" ]; then
awk 'BEGIN {printf "\033Ptmux;\033\033]"}'
else
awk 'BEGIN {printf "\033]"}'
fi
}
printST() {
if [ -n "$TMUX" ]; then
awk 'BEGIN {printf "\a\033\\"}'
else
awk 'BEGIN {printf "\a"}'
fi
}
# sharesheet should be called with
# filenames as arguments that will open
# in system sharesheet. Alternatively you
# can pipe in text and call it without
# arguments
sharesheet() {
printOSC
awk 'BEGIN {printf "6;sharesheet://?pwd="}'
printURIComponent "$PWD"
awk 'BEGIN {printf "&home="}'
printURIComponent "$HOME"
for var in "$@"
do
awk 'BEGIN {printf "&path="}'
printURIComponent "$var"
done
if [ $# -eq 0 ]
then
text=$(cat -)
awk 'BEGIN {printf "&text="}'
printURIComponent "$text"
fi
printST
}
# textastic should be called with a
# single filename argument that will
# open in Textastic 9.5 or later
textastic() {
printOSC
awk 'BEGIN {printf "6;textastic://?pwd="}'
printURIComponent "$PWD"
awk 'BEGIN {printf "&home="}'
printURIComponent "$HOME"
awk 'BEGIN {printf "&path="}'
printURIComponent "$1"
printST
}
# Secure ShellFish supports 24-bit colors
export COLORTERM=truecolor
# send the current directory using OSC 7
# when showing prompt to make filename
# detection work better
if [ -z "$INSIDE_EMACS" ]; then
update_terminal_cwd() {
printOSC
awk "BEGIN {printf \"7;%s\", \"file://$HOSTNAME\"}"
printURIComponent "$PWD"
printST
}
PROMPT_COMMAND="update_terminal_cwd${PROMPT_COMMAND:+; $PROMPT_COMMAND}"
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment