Skip to content

Commit

Permalink
Improve style of trace output
Browse files Browse the repository at this point in the history
  • Loading branch information
molovo committed Feb 2, 2017
1 parent 491029c commit 388ad5a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Crash

Proper error handling, exceptions and try/catch for ZSH
Proper error handling, exceptions and try/catch for ZSH.

## Installation

Expand Down
28 changes: 24 additions & 4 deletions crash
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,26 @@ function _crash_map_exit_code() {
echo $sig_name
}

###
# Format and output trace info
###
function _crash_format_trace_info() {
local tracetype="$1"
local -a str; str=("${(s/:/)2}")
local node=${str[1]} line=${str[2]} color

if [[ $tracetype = 'file' ]]; then
node=$(realpath $node)
color='\033[0;35m'
fi

if [[ $tracetype = 'function' ]]; then
color='\033[1;33m'
fi

echo "$color$node\033[0;m \033[0;38;5;242m:\033[0;m line $line"
}

###
# A function which throws an exception if it hasn't been caught
###
Expand Down Expand Up @@ -114,8 +134,8 @@ function _crash_global_exception_handler() {

# Print the function and file/line where the exception was thrown
echo
echo " in \033[0;33m${trace[1]}\033[0;m"
echo " at \033[0;35m${files[1]}\033[0;m"
echo " in $(_crash_format_trace_info 'function' ${trace[1]})"
echo " at $(_crash_format_trace_info 'file' ${files[1]})"

# Loop backwards through the trace, printing the function
# and file/line for each step
Expand All @@ -125,8 +145,8 @@ function _crash_global_exception_handler() {
shift files

echo
echo " $i \033[0;33m${trace[1]}\033[0;m"
echo " \033[0;35m${files[1]}\033[0;m"
echo " \033[0;38;5;242m$i\033[0;m $(_crash_format_trace_info 'function' ${trace[1]})"
echo " $(_crash_format_trace_info 'file' ${files[1]})"

i=$(( i + 1 ))
done
Expand Down

0 comments on commit 388ad5a

Please sign in to comment.