summaryrefslogtreecommitdiff
path: root/twopane.bash
blob: 2b30a3b477ae5ad1037c4816620636563f384612 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/bash
set -e
set -f
set -o pipefail
shopt -s lastpipe

BOT_SIZE=8
BOT_TITLE=input
BOT_CMD='PS1="$BOT_TITLE\$ " bash --noprofile --norc -i'
TOP_CMD='PS1="$TOP_TITLE\$ " bash --noprofile --norc -i; screen -X quit'
TOP_TITLE=output

TWOPANE=$(mktemp -d)
export TWOPANE TOP_CMD BOT_CMD TOP_TITLE BOT_TITLE BOT_SIZE
trap 'rm -r "$TWOPANE"' EXIT
STY=twopane.${TWOPANE##*/}
SCREENRC=$TWOPANE/screenrc
cat > "$SCREENRC" <<'.'
unbindall
escape \0\0
escape ``
caption string '%t'
layout new
split
focus bottom
resize $BOT_SIZE
screen -ln -t "$BOT_TITLE" 1 bash "$TWOPANE"/bashrc
focus top
screen -ln -t "$TOP_TITLE" sh -c "$TOP_CMD"
exec :!:| socat -u UNIX-RECV:"$TWOPANE"/socket STDOUT
title 'output'
focus bottom
layout save 0
.

cat > "$TWOPANE"/bashrc <<'.'
trap "screen -X quit" EXIT
sendp()
{
        socat -u STDIN UNIX-SENDTO:"$TWOPANE"/socket
}
sendc()
{
        printf '%s' "$*" | sendp
}
send()
{
        sendc "$*"$'\n'
}
export -f send sendc sendp
eval "$BOT_CMD"
.

screen -c "$SCREENRC" -m -S "$STY" -ln