/
tracecolor
executable file
·39 lines (33 loc) · 984 Bytes
/
tracecolor
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
#!/bin/bash
# (c) Stefan Countryman, 2017
# Add color to python stack traces
# define expressions we will look for in `sed`
exp[0]='File "[^"]*/'
exp[1]='[^"^/]*' # color the actual python file basename
exp[2]='", line '
exp[3]='[0-9]*' # color the line number
exp[4]=', in '
exp[5]='[^ ]*' # color the function name
# escape the patterns so that we can number them in `sed`
# for i in seq ${#exp[@]}; do
# exp[i-1]='\('"${exp[i-1]}"'\)'
# done
# put the whole search pattern together
pattern=''
for e in "${exp[@]}"; do
pattern+='\('"${e}"'\)'
done
#echo PATTERN:
#echo "${pattern}"
# define colors
# from http://linux.101hacks.com/ps1-examples/prompt-color-using-tput/
RED="$(tput setaf 1)"
MAGENTA="$(tput setaf 5)"
CYAN="$(tput setaf 6)"
RESET="$(tput sgr0)"
# define replacement pattern
replace="\1${RED}\2${RESET}\3${MAGENTA}\4${RESET}\5${CYAN}\6${RESET}"
#echo REPLACE:
#echo "${replace}"
# run STDIN through `sed`
sed "s_${pattern}_${replace}_"