From a03d9f1673dd4b74866f9f312f759aa73a3ef06f Mon Sep 17 00:00:00 2001 From: Jonathan Hodgson Date: Fri, 6 Sep 2019 22:52:02 +0100 Subject: [PATCH] Adds with script --- bin/.bin/with | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 bin/.bin/with diff --git a/bin/.bin/with b/bin/.bin/with new file mode 100755 index 00000000..6b51f93f --- /dev/null +++ b/bin/.bin/with @@ -0,0 +1,67 @@ +#!/usr/bin/env bash + +# Originally borrowed from here: https://github.com/tomnomnom/with/blob/master/with +# As such credit should go to tomnomnom + +# with tool +# usage: with +# opens an interactive shell instance that automatically prefixes all subsequent commands with program name + +pmpt="$*>" + +prefix=$* + +which $1 2>&1 > /dev/null +program_installed=$? +if [ ! $program_installed -eq 0 ]; then + echo "error: Program" $1 "is not installed" + exit 1 +fi + +stop=false +while [ true ]; do + while IFS="" read -r -e -d $'\n' -p "$pmpt " options; do + history -s "$options" + + curr_command="$(echo $options | { read first rest ; echo $first ; })" + + if [ -z "$curr_command" ]; then + # null case + eval "${prefix}" + else + # remove with + if [ $curr_command == - ]; then + prefix=$( echo $prefix | awk '{$NF="";sub(/[ \t]+$/,"")}1' ) + pmpt="$prefix>" + # nesting withs + elif [[ $curr_command == +* ]]; then + parsed=${options#"+"} + prefix=$( echo "$prefix" "$parsed" ) + pmpt="$prefix>" + # process shell commands + elif [[ $curr_command == :* ]]; then + parsed=${options#":"} + if [ "$parsed" = "q" ]; then + # terminate out of while loop instead of exiting so we can do some safe exiting + stop=true + break + fi + eval "$parsed" + else + eval "$prefix $options" + fi + fi + # see if there are any commands left + if [ -z "$prefix" ]; then + echo "No program. Returning to shell." + # null case + stop=true + break + fi + done + + if [ stop=true ]; then + break + fi + +done