-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathswc-shell-split-window.sh
executable file
·122 lines (98 loc) · 4.46 KB
/
swc-shell-split-window.sh
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/bin/bash
#
# Create terminal for Software Carpentry lesson
# with the log of the commands at the top.
# Session name. Defaults to 'swc', but you can override from the
# calling process.
SESSION="${SESSION:-swc}"
# Where we'll store the executed history. Defaults to /tmp/log-file,
# but you can override from the calling process. For example:
#
# LOG_FILE=/tmp/my-log ./swc-shell-split-window.sh
LOG_FILE="${LOG_FILE:-/tmp/$SESSION-split-log-file}"
# The number of lines of history to show. Defaults to 5, but you can
# override from the calling process.
HISTORY_LINES="${HISTORY_LINES:-5}"
# If $LOG_FILE exists, truncate it, otherwise create it.
# Either way, this leaves us with an empty $LOG_FILE for tailing.
> "${LOG_FILE}"
# Prompt colour
CYAN="[1;36m"
# Create the session to be used
# * don't attach yet (-d)
# * name it $SESSION (-s "${SESSION}")
tmux new-session -d -s "${SESSION}" "\
# * start reading the log
tail -f '${LOG_FILE}' | \
# * extract the line numbers
stdbuf -o 0 nl -s'%' -n'ln' -w1 | \
# * ignore lines starting with '#' since
# they are the history file's internal timestamps
stdbuf -o 0 grep -v '%#' | \
# * colour the line numbers
awk -F'%' '{print \"\033${CYAN}\"\$1\"\033[0m\",\$2}'"
# Get the unique (and permanent) ID for the new window
WINDOW=$(tmux list-windows -F '#{window_id}' -t "${SESSION}")
# Get the unique (and permanent) ID for the log pane
LOG_PANE=$(tmux list-panes -F '#{pane_id}' -t "${WINDOW}")
LOG_PID=$(tmux list-panes -F '#{pane_pid}' -t "${WINDOW}")
# Split the log-pane (-t "${LOG_PANE}") vertically (-v)
# * make the new pane the current pane (no -d)
# * load history from the empty $LOG_FILE (HISTFILE='${LOG_FILE}')
# * lines which begin with a space character are not saved in the
# history list (HISTCONTROL=ignorespace)
# * append new history to $HISTFILE after each command
# (PROMPT_COMMAND='history -a')
# * launch Bash since POSIX doesn't specify shell history or HISTFILE
# (bash)
# * when the Bash process exits, kill the log process
tmux split-window -v -t "${LOG_PANE}" \
"HISTFILE='${LOG_FILE}' HISTCONTROL=ignorespace PROMPT_COMMAND='history -a' bash --norc; kill '${LOG_PID}'"
# Get the unique (and permanent) ID for the shell pane
SHELL_PANE=$(tmux list-panes -F '#{pane_id}' -t "${WINDOW}" |
grep -v "^${LOG_PANE}\$")
tmux send-keys -t "${SHELL_PANE}" " cd" enter
# Unset all aliases to keep your environment from diverging from the
# learner's environment.
tmux send-keys -t "${SHELL_PANE}" " unalias -a" enter
# Set nice prompt displaying
# with colour
# the command number and
# the '$'.
tmux send-keys -t "${SHELL_PANE}" " export PS1=\"\[\033${CYAN}\]\! $\[\033[0m\] \"" enter
#A prompt showing `user@host:~/directory$ ` can be achieved with:
#tmux send-keys -t "${SHELL_PANE}" " export PS1=\"\\[\\e]0;\\u@\\h: \\w\\a\\]${debian_chroot:+($debian_chroot)}\\[\\033[01;32m\\]user@host\\[\\033[00m\\]:\\[\\033[01;34m\\]\\w\\[\\033[00m\\]\\$ \"" enter
#Set terminal colours
if [ ! -z "$BGCOLOR" ]; then
tmux select-pane -t "${SHELL_PANE}" -P bg="colour$BGCOLOR"
tmux select-pane -t "${LOG_PANE}" -P bg="colour$BGCOLOR"
fi
sleep 0.1
# Clear the history so it starts over at number 1.
# The script shouldn't run any more non-shell commands in the shell
# pane after this.
tmux send-keys -t "${SHELL_PANE}" "history -c" enter
# Send Bash the clear-screen command (see clear-screen in bash(1))
tmux send-keys -t "${SHELL_PANE}" "C-l"
# Wait for Bash to act on the clear-screen. We need to push the
# earlier commands into tmux's scrollback before we can ask tmux to
# clear them out.
sleep 0.1
# Clear tmux's scrollback buffer so it matches Bash's just-cleared
# history.
tmux clear-history -t "${SHELL_PANE}"
# Need add an additional line because Bash writes a trailing newline
# to the log file after each command, tail reads through that trailing
# newline and flushes everything it read to its pane.
LOG_PANE_HEIGHT=$((${HISTORY_LINES} + 1))
# Resize the log window to show the desired number of lines
tmux resize-pane -t "${LOG_PANE}" -y "${LOG_PANE_HEIGHT}"
# Turn off tmux's status bar, because learners won't have one in their
# terminal.
# * don't print output to the terminal (-q)
# * set this option at the window level (-w). I'd like new windows in
# this session to get status bars, but it doesn't seem like there
# are per-window settings for 'status'. In any case, the -w doesn't
# seem to cause any harm.
tmux set-option -t "${WINDOW}" -q -w status off
tmux attach-session -t "${SESSION}"