Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 38 additions & 10 deletions swc-shell-split-window.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,47 @@ HISTORY_LINES="${HISTORY_LINES:-5}"
# Prompt colour
CYAN="[1;36m"

# If awk supports fflush(), use updated streaming command for log pane
if awk 'BEGIN { printf ""; fflush(); exit 0 }' </dev/null >/dev/null 2>&1; then
COMMAND="\
# * start reading the log
tail -f '${LOG_FILE}' |
awk -v cyan='${CYAN}' '
# * build escape character and reset sequence for printing
BEGIN {
esc = sprintf(\"%c\", 27)
reset = esc \"[0m\"
}
# * ignore lines starting with # since
# they are internal timestamps of the history file
/^#/ { next }
# * colour the line number, then reset colour and print log line,
# then flush so output appears immediately while tailing
{
printf \"%s%s%d%s %s\n\", esc, cyan, NR, reset, \$0
fflush()
}
'"
# Fallback to classic stdbuf method
else
COMMAND="\
# * 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}'"
Comment on lines +52 to +59

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you keep the comments that were in the previous code? Explaining also what's going on in the code above would nice, too.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point - comments included in c46f7e4

fi

echo $COMMAND

# 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}'"
tmux new-session -d -s "${SESSION}" "${COMMAND}"

# Get the unique (and permanent) ID for the new window
WINDOW=$(tmux list-windows -F '#{window_id}' -t "${SESSION}")
Expand Down