Skip to content

Commit 30ebbf5

Browse files
committed
Colorize the output snippet using IRB
1 parent e438697 commit 30ebbf5

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
99
### Added
1010

1111
- Better handling for formatting files with errors.
12+
- Colorize the output snippet using IRB.
1213

1314
## [1.0.0]
1415

lib/syntax_tree/cli.rb

+8-2
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,22 @@ def highlight_error(error, source)
236236
if line_number == error.lineno
237237
part1 = Color.red(">")
238238
part2 = Color.gray("%#{digits}d |" % line_number)
239-
warn("#{part1} #{part2} #{lines[line_index]}")
239+
warn("#{part1} #{part2} #{colorize_line(lines[line_index])}")
240240

241241
part3 = Color.gray(" %#{digits}s |" % " ")
242242
warn("#{part3} #{" " * error.column}#{Color.red("^")}")
243243
else
244244
prefix = Color.gray(" %#{digits}d |" % line_number)
245-
warn("#{prefix} #{lines[line_index]}")
245+
warn("#{prefix} #{colorize_line(lines[line_index])}")
246246
end
247247
end
248248
end
249+
250+
# Take a line of Ruby source and colorize the output.
251+
def colorize_line(line)
252+
require "irb"
253+
IRB::Color.colorize_code(line, complete: false, ignore_error: true)
254+
end
249255
end
250256
end
251257
end

0 commit comments

Comments
 (0)