Skip to content
This repository has been archived by the owner on May 3, 2020. It is now read-only.

Commit

Permalink
Version 3 of iTerm 2 supports
Browse files Browse the repository at this point in the history
  • Loading branch information
andromedarabbit committed Mar 28, 2016
1 parent e3ade26 commit a588c86
Show file tree
Hide file tree
Showing 7 changed files with 198 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
@@ -1 +1,2 @@
not-shared
*.log
2 changes: 1 addition & 1 deletion Brewfile
Expand Up @@ -27,7 +27,7 @@ cask 'gpgtools'
cask '1password'

# Shell
cask 'iterm2'
cask 'iterm2-beta'
cask 'path-finder'
cask 'go2shell'

Expand Down
4 changes: 4 additions & 0 deletions bash/.bash_profile
@@ -1,3 +1,5 @@
#!/usr/bin/env bash

# Add `~/bin` to the `$PATH`
export PATH="$HOME/bin:/usr/local/bin:$PATH"

Expand Down Expand Up @@ -57,3 +59,5 @@ complete -W "NSGlobalDomain" defaults;

# Add `killall` tab completion for common apps
complete -o "nospace" -W "Contacts Calendar Dock Finder Mail Safari iTunes SystemUIServer Terminal Twitter" killall;

test -e "${HOME}/.iterm2_shell_integration.bash" && source "${HOME}/.iterm2_shell_integration.bash"
9 changes: 9 additions & 0 deletions bash/bin/iterm2/divider
@@ -0,0 +1,9 @@
#!/bin/bash
if [ $# -eq 0 ]; then
echo "Usage: divider file"
exit 1
fi
printf '\033]1337;File=inline=1;width=100%%;height=1;preserveAspectRatio=0'
printf ":"
base64 < "$1"
printf '\a\n'
17 changes: 17 additions & 0 deletions bash/bin/iterm2/download.sh
@@ -0,0 +1,17 @@
#!/bin/bash
if [ $# -ne 1 ]; then
echo "Usage: download.sh file ..."
exit 1
fi
for fn in "$@"
do
if [ -r "$fn" ] ; then
printf '\033]1337;File=name='`echo -n "$fn" | base64`";"
wc -c "$fn" | awk '{printf "size=%d",$1}'
printf ":"
base64 < "$fn"
printf '\a'
else
echo File $fn does not exist or is not readable.
fi
done
99 changes: 99 additions & 0 deletions bash/bin/iterm2/imgcat
@@ -0,0 +1,99 @@
#!/bin/bash

# tmux requires unrecognized OSC sequences to be wrapped with DCS tmux;
# <sequence> ST, and for all ESCs in <sequence> to be replaced with ESC ESC. It
# only accepts ESC backslash for ST.
function print_osc() {
if [[ $TERM == screen* ]] ; then
printf "\033Ptmux;\033\033]"
else
printf "\033]"
fi
}

# More of the tmux workaround described above.
function print_st() {
if [[ $TERM == screen* ]] ; then
printf "\a\033\\"
else
printf "\a"
fi
}

# print_image filename inline base64contents
# filename: Filename to convey to client
# inline: 0 or 1
# base64contents: Base64-encoded contents
function print_image() {
print_osc
printf '1337;File='
if [[ -n "$1" ]]; then
printf 'name='`echo -n "$1" | base64`";"
fi
if $(base64 --version 2>&1 | grep GNU > /dev/null)
then
BASE64ARG=-d
else
BASE64ARG=-D
fi
echo -n "$3" | base64 $BASE64ARG | wc -c | awk '{printf "size=%d",$1}'
printf ";inline=$2"
printf ":"
echo -n "$3"
print_st
printf '\n'
}

function error() {
echo "ERROR: $*" 1>&2
}

function show_help() {
echo "Usage: imgcat filename ..." 1>& 2
echo " or: cat filename | imgcat" 1>& 2
}

## Main

if [ -t 0 ]; then
has_stdin=f
else
has_stdin=t
fi

# Show help if no arguments and no stdin.
if [ $has_stdin = f -a $# -eq 0 ]; then
show_help
exit
fi

# Look for command line flags.
while [ $# -gt 0 ]; do
case "$1" in
-h|--h|--help)
show_help
exit
;;
-*)
error "Unknown option flag: $1"
show_help
exit 1
;;
*)
if [ -r "$1" ] ; then
print_image "$1" 1 "$(base64 < "$1")"
else
error "imgcat: $1: No such file or directory"
exit 2
fi
;;
esac
shift
done

# Read and print stdin
if [ $has_stdin = t ]; then
print_image "" 1 "$(cat | base64)"
fi

exit 0
67 changes: 67 additions & 0 deletions bash/bin/iterm2/imgls
@@ -0,0 +1,67 @@
#!/bin/bash
# tmux requires unrecognized OSC sequences to be wrapped with DCS tmux;
# <sequence> ST, and for all ESCs in <sequence> to be replaced with ESC ESC. It
# only accepts ESC backslash for ST.
function print_osc() {
if [ x"$TERM" = "xscreen" ] ; then
printf "\033Ptmux;\033\033]"
else
printf "\033]"
fi
}

# More of the tmux workaround described above.
function print_st() {
if [ x"$TERM" = "xscreen" ] ; then
printf "\a\033\\"
else
printf "\a"
fi
}

function list_file() {
fn=$1
dims=$(php -r 'if (!is_file($argv[1])) exit(1); $a = getimagesize($argv[1]); if ($a==FALSE) exit(1); else { echo $a[0] . "x" .$a[1]; exit(0); }' "$fn")
rc=$?
if [[ $rc == 0 ]] ; then
print_osc
printf '1337;File=name='`echo -n "$fn" | base64`";"
wc -c "$fn" | awk '{printf "size=%d",$1}'
printf ";inline=1;height=3;width=3;preserveAspectRatio=true"
printf ":"
base64 < "$fn"
print_st
if [ x"$TERM" == "xscreen" ] ; then
# This works in plain-old tmux but does the wrong thing in iTerm2's tmux
# integration mode. tmux doesn't know that the cursor moves when the
# image code is sent, while iTerm2 does. I had to pick one, since
# integration mode is undetectable, so I picked the failure mode that at
# least produces useful output (there is just too much whitespace in
# integration mode). This could be fixed by not moving the cursor while
# in integration mode. A better fix would be for tmux to interpret the
# image sequence, though.
#
# tl;dr: If you use tmux in integration mode, replace this with the printf
# from the else clause.
printf '\033[4C\033[Bx'
else
printf '\033[A'
fi
echo -n "$dims "
ls -ld "$fn"
else
ls -ld "$fn"
fi
}

if [ $# -eq 0 ]; then
for fn in *
do
list_file "$fn"
done < <(ls -ls)
else
for fn in "$@"
do
list_file "$fn"
done
fi

0 comments on commit a588c86

Please sign in to comment.