Refactor 'start_logging' script

This commit is contained in:
Bruno Sutic 2015-03-14 23:15:37 +01:00
parent d0d7348004
commit ac30255135
No known key found for this signature in database
GPG key ID: 66D96E4F2F7EF26C

View file

@ -1,7 +1,7 @@
#!/usr/bin/env bash
# path to log file
file=$1
# path to log file - global variable
FILE="$1"
ansifilter_installed() {
type ansifilter >/dev/null 2>&1 || return 1
@ -12,38 +12,33 @@ system_osx() {
}
pipe_pane_ansifilter() {
local file=$1
tmux pipe-pane "exec cat - | ansifilter >> $file"
tmux pipe-pane "exec cat - | ansifilter >> $FILE"
}
pipe_pane_sed_osx() {
local file=$1
# Warning, very complex regex ahead.
# Some characters below might not be visible from github web view.
local ansi_codes_osx="(\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]| |]0;[^]+|[[:space:]]+$)"
tmux pipe-pane "exec cat - | sed -E \"s/$ansi_codes_osx//g\" >> $file"
tmux pipe-pane "exec cat - | sed -E \"s/$ansi_codes_osx//g\" >> $FILE"
}
pipe_pane_sed() {
local file=$1
local ansi_codes="(\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]| )"
tmux pipe-pane "exec cat - | sed -r 's/$ansi_codes//g' >> $file"
tmux pipe-pane "exec cat - | sed -r 's/$ansi_codes//g' >> $FILE"
}
start_pipe_pane() {
local file=$1
if ansifilter_installed; then
pipe_pane_ansifilter "$file"
pipe_pane_ansifilter
elif system_osx; then
# OSX uses sed '-E' flag and a slightly different regex
pipe_pane_sed_osx "$file"
pipe_pane_sed_osx
else
pipe_pane_sed "$file"
pipe_pane_sed
fi
}
main() {
local file=$1
start_pipe_pane "$file"
start_pipe_pane
}
main "$file"
main