Commit f21005ad authored by Dom Sekotill's avatar Dom Sekotill
Browse files

Support locally supplied term-startup.sh options

parent d00ad465
Loading
Loading
Loading
Loading
+23 −10
Original line number Diff line number Diff line
@@ -2,33 +2,46 @@
set -eu
shopt -s extglob lastpipe

declare -A TERM_STARTUP_OPTIONS=(
	[ssh]="SSH: connect to a remote host"
	[tmux]="Tmux: connect to local terminal multiplexer"
)

. ~/.shell/lib/shared.sh
. ~/.profile

declare -r CODE_HIGHLIGHT=$(tput setab 15; tput setaf 16)
declare -r CODE_RESET=$(tput sgr0)
declare -a OPTIONS=(
	ssh="SSH: connect to a remote host"
	tmux="Tmux: connect to local terminal multiplexer"
	shell="Shell: start a local shell ($SHELL)"
	manual="Enter command manually"
)
declare -a OPTIONS=()
declare -i FOCUS=0
declare USE_TMUX=true

if [[ -v TMUX ]] || ! has tmux || ! tmux has-session; then
	USE_TMUX=false
	unset OPTIONS[1]
	OPTIONS=( "${OPTIONS[@]}" )
	unset TERM_STARTUP_OPTIONS[tmux]
fi
if ! has ssh; then
	unset TERM_STARTUP_OPTIONS[ssh]
fi

for opt in "${!TERM_STARTUP_OPTIONS[@]}"; do
	OPTIONS+=( $opt="${TERM_STARTUP_OPTIONS[$opt]}" )
done

# Hard-code the following two options:
OPTIONS+=(
	shell="Shell: start a local shell ($SHELL)"
	manual="Enter command manually"
)


_clear() { clear; echo; }

interact() {
	local STATE=ssh
	trap 'echo; read -N1 -p "Press any key to exit"' EXIT
	while _clear && do_$STATE; do
		has do_$STATE || die "Unknown state: $STATE"
	while _clear && do_${STATE//-/_}; do
		has do_${STATE//-/_} || die "Unknown state: $STATE"
	done
	die "Command for the $STATE state failed: $_"
}