Commit 37a92516 authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Add a tmux => x-display helper script

parent 87cac5d9
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -19,7 +19,8 @@ fi
source_each ~/.shell/$(get_platform)/profile ~/.sh_local ~/.bash_local

# Automatically run terminal multiplexer on SSH connect
[ -n "$SSH_TTY" ] && [ "x$TERM" != xscreen ] && has tmux && exec tmux -u attach
[ -n "$SSH_TTY" ] && [ "x$TERM" != xscreen ] && has tmux &&
	exec ~/.shell/bin/tmux-display-helper start-client

# Get Bash aliases and functions
source_first ~/.bashrc /etc/bash.bashrc /etc/bashrc
+2 −0
Original line number Diff line number Diff line
@@ -12,6 +12,8 @@ alias ls='ls -lh'

alias syslog='tail -F /var/log/syslog'

alias client-display='~/.shell/bin/tmux-display-helper run-command'


## Common miss-spellings
## =====================
+115 −0
Original line number Diff line number Diff line
#!/bin/sh
set -e

SCRIPT=`basename $0`
USAGE="$SCRIPT [-hH] {start-client|run-command [COMMAND [ARGUMENTS ...]]}"

DESCRIPTION="
Assist with running graphical programs in the configured X
server where the current tmux client was run.
"

ADDITIONAL="There are two parts to this process:
  1) Use this script to set the client's DISPLAY variable namespaced with the
     client's TTY in the tmux server, then start the client attached to the
     server.
  2) Run graphical programs from within tmux terminals with this script,
     which will get the DISPLAY variable for the current client.
"

OPTIONS="Options:
  -h --help       Show the usage and help message and exit.
  -H --long-help  Show the longer usages and help message and exit.
"

COMMANDS="Commands:
  start-client  Start a new tmux client and add configurations for this script
                to the server.
  run-command   Run a command from inside a tmux client started with this
                script, with the DISPLAY environment variable set correctly for
				the client's terminal.
"


die() {
	code=${CODE-$?}
	[ $code -gt 0 ] || code=1
	echo "Error: $*" >&2
	exit $code
}

warn() {
	echo "Warning: $SCRIPT: $*" >&2
}

usage() {
	echo "$USAGE"
	echo "$DESCRIPTION"
	[ x"$LONG_HELP" != xyes ] ||
		echo "$ADDITIONAL"
	echo "$OPTIONS"
	echo "$COMMANDS"
}

bad_arg() {
	usage >&2
	CODE=2 die "$*"
}

id() { sha1sum|cut -f1 -d' '; }

set_var() {
	# usage: set_var NAME VALUE
	VAR_NAME=${1}_`tty|id`
	tmux set-environment -g "$VAR_NAME" "$2"
}

unset_var() {
	# usage: unset_var NAME
	VAR_NAME=${1}_`tty|id`
	tmux set-environment -gu "$VAR_NAME"
}


get_var() {
	# usage: get_var NAME
	VAR_NAME=${1}_`tmux display-message -p '#{client_tty}'|id`
	if ! VAR_CMD=`tmux show-environment -g "$VAR_NAME" 2>/dev/null`; then
		warn "This client was not started with '$SCRIPT start-client'"
		return
	fi
	eval $VAR_CMD
	eval ${1}="\$$VAR_NAME"
}


start_client() {
	[ $# -eq 1 ] || bad_arg "start-client takes no arguments"
	[ -z "$TMUX" ] || die "this command must be run from outside of tmux"
	tmux start-server  # no-op if already started
	set_var DISPLAY "$DISPLAY"
	tmux attach-session; code=$?
	unset_var DISPLAY
	exit $code
}


run_command() {
	shift
	[ -n "$TMUX" ] || die "this command must be run inside a tmux instance"
	get_var DISPLAY
	[ -n "$DISPLAY" ] && export DISPLAY
	exec "$@"
}


while [ $# -gt 0 ]; do
	case $1 in
		-h|--help) usage; exit 0 ;;
		-H|--long-help) LONG_HELP=yes usage; exit 0 ;;
		start-client) start_client "$@" ;;
		run-command) run_command "$@" ;;
		*) bad_arg "Unknown argument: $1" ;;
	esac
	shift
done