Skip to content
r edited this page Apr 1, 2011 · 9 revisions

An often-requested feature is to be able to display code with syntax highlighting.

If you have CodeRay and the Term::ANSIColor gem installed, trepan can be instructed to highlight syntax in a window that supports ANSI terminal commands.

Here is how to use.

First, make sure the syntax-highlighting gems are installed:

$ gem install coderay term-ansicolor  # use sudo if not rvm

Next, tell the debugger to use syntax highlighting. Do one of these three things:

  • add —highlight as a trepan flag:
$ trepan --highlight your-ruby-program.rb
  • or set the highlight value to :term on a Trepan.debugger() call:
# work, work, work...
Trepan.debugger(:highlight => :term)
# Stop will be next executable code after here.
  • or use the debugger “set highlight” command:
(trepan): set highlight on
  highlight is on.

One thing I found I needed to do was customize the colors. In the distribution is Ruby program sample/list-terminal-colors.rb . Running that should list the names, default values and colors found in Term::ANSIColor. To change a color, basically you need to change a value in constant CodeRay::Encoders::Term::TOKEN_COLORS.

The code I use is is in sample/rocky-trepanx-colors.rb and is as follows:

TERM_TOKEN_COLORS = {
  :comment => '3;37',        # sienna #8b4726
  :constant => '1;34',       # Bold Midnight Blue #191970
  :class => '1;2',           # 
  :doctype => '1;2',         # 
  :global_variable => '36',  # yellow brownish 
  :integer => '29',          # black #00000
  :label => '4',             # black underline
  :method => '34',           # blue #0000FF 
  :pre_constant => '3;33',   # goldenrod4 #8b6914
  :regexp => {
    :content => '36',        # dark cyan #008b8b
    :delimiter => '1;29',    # bold black
    :modifier => '35',
    :function => '1;29'
  },
  :string => {
    :content => '1;37',      # ivory4 (grey) #8b8b83
    :delimiter => '1;29',    # bold black
  },
  :reserved => '1;32',       # bold dark olive green #556b2f RGB: 85, 107, 47
  :symbol   => '35',         # purple4 #551A8B RGB: 85, 26, 139
}
  module CodeRay::Encoders
   class Term < Encoder
     TERM_TOKEN_COLORS.each_pair do |key, value|
       TOKEN_COLORS[key] = value
     end
   end
  end

Getting ANSI terminal colors in GNU Emacs 23 requires its own customization. Here is what I have inside my .emacs startup profile

(ansi-color-for-comint-mode-on)
(custom-set-variables
  ;; custom-set-variables was added by Custom. ...
 '(ansi-color-for-comint-mode-on t)
 '(ansi-color-names-vector ["black" "red" "DarkOliveGreen4" "CadetBlue" "blue" "Purple" "DarkGoldenrod" "ivory4"])
 '(ansi-term-color-vector [unspecified "black" "red" "DarkOliveGreen4" "CadetBlue" "blue" "Purple" "DarkGoldenrod" "ivory4"])
   ...)

Here is what my terminal colors for a Ruby program look like:

And my terminal colors for a Ruby 1.92 YARV disassembly: