From 9e7a570e35f244ff4dd9f498b686da1a41614daa Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Wed, 31 Jul 2024 21:56:15 -0400 Subject: coprocess works --- twopane.bash | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) mode change 100755 => 100644 twopane.bash diff --git a/twopane.bash b/twopane.bash old mode 100755 new mode 100644 index b9c1051..5ba1264 --- a/twopane.bash +++ b/twopane.bash @@ -3,6 +3,7 @@ set -e set -f set -o pipefail shopt -s lastpipe +PROGNAME=${0##*/} BOT_SIZE=8 BOT_TITLE=input @@ -14,10 +15,10 @@ TWOPANE=$(mktemp -d) export TWOPANE TOP_CMD BOT_CMD TOP_TITLE BOT_TITLE BOT_SIZE trap 'rm -r "$TWOPANE"' EXIT STY=twopane.${TWOPANE##*/} + save_file() { - out=$TWOPANE/${1:?$0: filename cannot be empty string} - cat > "$out" + cat > "$TWOPANE"/"${1:?$PROGNAME: Error: filename cannot be empty string}" } save_screenrc() { @@ -32,7 +33,7 @@ layout new split focus bottom resize $BOT_SIZE -screen -ln -t "$BOT_TITLE" 0 bash "$TWOPANE"/bashrc +screen -ln -t "$BOT_TITLE" 0 bash --noprofile --rcfile "$TWOPANE"/bashrc -x -i source "$TWOPANE"/screenrc.startpane . @@ -46,7 +47,7 @@ focus bottom layout save 0 . -save_screenrc 'restart' <<'.' +save_screenrc 'restart' <<. layout new split focus bottom @@ -57,8 +58,7 @@ title 'input' source "$TWOPANE"/screenrc.startpane . -save_file bashrc <<'.' -trap "screen -X quit" EXIT +TWOPANE_FUNCTION_EXPORTS='restart sendp sendc send start' restart() { screen -X source "$TWOPANE"/screenrc.restart @@ -69,22 +69,34 @@ sendp() } sendc() { - printf '%s' "$*" | sendp + printf '%s' "$*" >&${COPROC[1]} } send() { - sendc "$*"$'\n' + printf '%s' "$*"$'\n' >&${COPROC[1]} } start() { while read -r -N1 do - sendc "$REPLY" + printf '%s' "$REPLY" >&${COPROC[1]} done } -export -f restart sendp sendc send start -bash -c "$BOT_CMD" || read -p "Error: process terminated with exit code $? -Press enter to quit." + +save_file bashrc <<. +set -x +set -f +set -o pipefail +shopt -s lastpipe +trap "screen -X quit" EXIT +$(declare -f $TWOPANE_FUNCTION_EXPORTS) +export PS1="$BOT_TITLE\$ " +coproc sendp . -screen -c "$TWOPANE"/screenrc -m -S "$STY" -ln +main() +{ + screen -c "$TWOPANE"/screenrc -m -S "$STY" -ln +} + +main "$@" -- cgit v1.2.3