Skip to content
Closed
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
31 changes: 29 additions & 2 deletions lib/debug/backtrace_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ module DEBUGGER__
class BacktraceFormatter
attr_reader :frames

COLOR_CODES = {
green: 10,
yellow: 11,
blue: 12,
megenta: 13,
cyan: 14,
orange: 214
}

COLOR_RESET_POSTFIX = "\u001b[0m"

def initialize(frames)
@frames = frames
end
Expand All @@ -19,9 +30,25 @@ def formatted_traces(max)

def formatted_trace(i)
frame = @frames[i]
result = "#{frame.call_identifier_str} at #{frame.location_str}"
result += " #=> #{frame.return_value_str}" if frame.return_value_str
location_str = colorize(frame.location_str, :green)
call_identifier_str = colorize(frame.call_identifier_str, :yellow)

result = "#{call_identifier_str} at #{location_str}"

if frame.return_value_str
return_value_str = colorize(frame.return_value_str, :megenta)
result += " #=> #{return_value_str}"
end

result
end

private

def colorize(content, color)
color_code = COLOR_CODES[color]
color_prefix = "\u001b[38;5;#{color_code}m"
"#{color_prefix}#{content}#{COLOR_RESET_POSTFIX}"
end
end
end