Created
January 15, 2019 13:20
-
-
Save ToxicFrog/1ffcf543195a0872f92c244718634b56 to your computer and use it in GitHub Desktop.
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
## weechat-open-url | |
#!/usr/bin/env zsh | |
# | |
# Command to open a URL from weechat in a smart way. | |
# If we're ssh'd in from a machine on the same net, assume it's thoth or | |
# durandal, ssh back into it, and open the url in feh or chrome. | |
# If we aren't, assume we can't ssh into it (it's a work machine, or thoth | |
# behind someone else's NAT, or something). | |
# In that case, open images with timg or web pages with elinks. | |
client_pid=$(tmux lsc -F '#{client_activity} #{client_pid}' | sort -g | tail -n1 | cut -d' ' -f2) | |
client_ip=$(cat /proc/$client_pid/environ | tr '\0' '\n' | grep '^SSH_CLIENT' | cut -d= -f2 | cut -d' ' -f1) | |
function open-image { | |
if [[ $client_ip == 192.168.* ]]; then | |
ssh "$client_ip" \ | |
'env XAUTHORITY=$(echo /run/user/$(id -u)/xauth_*) XAUTHLOCALHOSTNAME=$(hostname) DISPLAY=:1 feh -x -F --auto-zoom' "$1" | |
else | |
tmux split-window -h -c ~ zsh -c "source .zsh/aliases.zsh; timg '$1'" | |
fi | |
} | |
function open-url { | |
if [[ $client_ip == 192.168.* ]]; then | |
ssh "$client_ip" \ | |
'env XAUTHORITY=$(echo /run/user/$(id -u)/xauth_*) XAUTHLOCALHOSTNAME=$(hostname) DISPLAY=:1 google-chrome' "$1" | |
else | |
tmux split-window -h -c ~ zsh -c "elinks -anonymous 1 '$1'" | |
fi | |
} | |
case "$1" in | |
*.jpg|*.jpeg|*.png|*.gif|*.svg) | |
open-image "$1" | |
;; | |
*) | |
open-url "$1" | |
;; | |
esac | |
[2019-01-15 07:56:08] | |
## aliases.zsh | |
if (( ${+commands[timg]} )); then | |
function timg { | |
for img in "$@"; do | |
printf '\x1B[1m%s\x1B[0m\n' "$img" | |
if [[ $img == http* ]]; then | |
curl "$img" > /tmp/$$.img | |
img="/tmp/$$.img" | |
fi | |
command timg -g ${COLUMNS}x$((2*LINES-4)) -U "$img" | |
done | sed -E '$ d' | less -ReS | |
} | |
fi | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment