Skip to content

Instantly share code, notes, and snippets.

@marian13
Last active February 2, 2023 04:01
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save marian13/5dade20a431d7254db30e543167058ce to your computer and use it in GitHub Desktop.
Save marian13/5dade20a431d7254db30e543167058ce to your computer and use it in GitHub Desktop.

Byebug syntax highlighting. Works for v10.0.0 and higher.

  • Yet another hack how to enable the syntax highlighting for the byebug gem.

  • Works for v10.0.0 and higher.

  • Place the content of byebug_syntax_highlighting.rb somewhere in your codebase (Inside an initializer for example, if you are using Rails).

  • Do not forget to include byebug and rouge to the Gemfile as well.

  • Enjoy 🎉 🎉 🎉

P.S. If you like to choose a different theme, just replace Monokai from theme = Rouge::Themes::Monokai.new to any available alternative from this list.

return if (defined? Rails) && !(Rails.env.development? || Rails.env.test?)
require 'tempfile'
require 'byebug'
require 'rouge'
##
# Yet another hack how to enable the syntax highlighting for the byebug gem. Works for v10.0.0 and higher.
# (See https://gist.github.com/marian13/5dade20a431d7254db30e543167058ce)
#
# WARNING: Although this hack is based on the monkey patching,
# use this technique for other issues in your own codebases with a precaution.
#
require 'byebug/runner' unless defined? Byebug::VERSION
if Gem::Version.new(Byebug::VERSION) >= Gem::Version.new('10.0.0')
module Byebug
class SourceFileFormatter
##
# This is a replacement of the internal <tt>Byebug::SourceFileFormatter.file</tt> method
# which adds syntax highlighting capability to it.
#
# The original implementation simply returns the file path
# which is passed to the <tt>Byebug::SourceFileFormatter</tt> constructor just as <tt>file</tt>.
# (See https://github.com/deivid-rodriguez/byebug/blob/master/lib/byebug/source_file_formatter.rb#L13)
#
# The current replacement, instead of returning the original file path,
# returns a copy of it, where the syntax is highlighted by the Rouge gem(A pure Ruby code highlighter).
# (See https://github.com/rouge-ruby/rouge)
#
# In order to create a copy, it utilizes Ruby's <tt>tempfile</tt> stdlib.
# (See https://ruby-doc.org/stdlib-2.7.0/libdoc/tempfile/rdoc/Tempfile.html)
# A tempfile is automatically deleted from the underlying OS when it is garbage-collected.
#
def file
@highlighted_file ||=
begin
if defined? Rouge
source = File.read(@file)
theme = Rouge::Themes::Monokai.new
formatter = Rouge::Formatters::Terminal256.new(theme)
lexer = Rouge::Lexers::Ruby.new
dest = formatter.format(lexer.lex(source))
# Tempfile with the highlighted syntax is assigned to the instance variable
# in order to prevent its premature garbage collection.
@tempfile_with_highlighted_syntax = Tempfile.new.tap { |t| t.write(dest) }.tap(&:close)
@tempfile_with_highlighted_syntax.path
else
warn %q{Rouge(a pure Ruby code highlighter) is not defined. Maybe you forgot to require it? (require 'rouge')}
@file
end
end
end
end
end
else
warn 'Byebug version is lower than v10.0.0...'
end
group :development, :test do
gem 'byebug', '>= 10.0.0'
gem 'rouge'
end
@MattCraftsCode
Copy link

It doesn't work on byebug "11.1.3"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment