diff --git a/swc-shell-split-window.sh b/swc-shell-split-window.sh index b1d3c58..242a39b 100755 --- a/swc-shell-split-window.sh +++ b/swc-shell-split-window.sh @@ -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 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}'" +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}")