diff options
author | Takashi Kokubun <takashikkbn@gmail.com> | 2019-04-25 21:16:21 +0900 |
---|---|---|
committer | Takashi Kokubun <takashikkbn@gmail.com> | 2019-04-26 00:47:39 +0900 |
commit | 94af6cd383f9dc3ae1204a5fba8f56ee7826cbce (patch) | |
tree | 42ae2c951135341abf7b891917fa3b10c87c2707 /lib/irb/workspace.rb | |
parent | 790f6709ae418345829d12f053cf270f4d535f1c (diff) |
Colorize IRB's code_around_binding
Closes: https://github.com/ruby/ruby/pull/2150
Diffstat (limited to 'lib/irb/workspace.rb')
-rw-r--r-- | lib/irb/workspace.rb | 11 |
1 files changed, 7 insertions, 4 deletions
diff --git a/lib/irb/workspace.rb b/lib/irb/workspace.rb index 71778a8dd4..ff8f5da64f 100644 --- a/lib/irb/workspace.rb +++ b/lib/irb/workspace.rb @@ -118,23 +118,26 @@ EOF def code_around_binding file, pos = @binding.source_location - unless defined?(::SCRIPT_LINES__[file]) && lines = ::SCRIPT_LINES__[file] + if defined?(::SCRIPT_LINES__[file]) && lines = ::SCRIPT_LINES__[file] + code = ::SCRIPT_LINES__[file].join('') + else begin - lines = File.readlines(file) + code = File.read(file) rescue SystemCallError return end end + lines = Color.colorize_code(code).lines pos -= 1 start_pos = [pos - 5, 0].max end_pos = [pos + 5, lines.size - 1].min - fmt = " %2s %#{end_pos.to_s.length}d: %s" + fmt = " %2s #{Color.colorize("%#{end_pos.to_s.length}d", [:BLUE, :BOLD])}: %s" body = (start_pos..end_pos).map do |current_pos| sprintf(fmt, pos == current_pos ? '=>' : '', current_pos + 1, lines[current_pos]) end.join("") - "\nFrom: #{file} @ line #{pos + 1} :\n\n#{body}\n" + "\nFrom: #{file} @ line #{pos + 1} :\n\n#{body}#{Color.clear}\n" end def IRB.delete_caller |