class CodeRay::Encoders::Terminal

Outputs code highlighted for a color terminal.

Note: This encoder is in beta. It currently doesn’t use the Styles.

Alias: term

Authors & License

By Rob Aldred (robaldred.co.uk)

Based on idea by Nathan Weizenbaum (nex-3.com)

MIT License (www.opensource.org/licenses/mit-license.php)

Constants

TOKEN_COLORS

Public Instance Methods

begin_group(kind) click to toggle source
# File lib/coderay/encoders/terminal.rb, line 126
def begin_group kind
  @opened << kind
  @out << open_token(kind)
end
Also aliased as: begin_line
begin_line(kind) click to toggle source
Alias for: begin_group
end_group(kind) click to toggle source
# File lib/coderay/encoders/terminal.rb, line 132
def end_group kind
  if @opened.empty?
    # nothing to close
  else
    @opened.pop
    @out << ansi_clear
    @out << open_token(@opened.last)
  end
end
end_line(kind) click to toggle source
# File lib/coderay/encoders/terminal.rb, line 142
def end_line kind
  if @opened.empty?
    # nothing to close
  else
    @opened.pop
    # whole lines to be highlighted,
    # eg. added/modified/deleted lines in a diff
    @out << "\t" * 100 + ansi_clear
    @out << open_token(@opened.last)
  end
end
text_token(text, kind) click to toggle source
# File lib/coderay/encoders/terminal.rb, line 106
def text_token text, kind
  if color = (@subcolors || TOKEN_COLORS)[kind]
    if Hash === color
      if color[:self]
        color = color[:self]
      else
        @out << text
        return
      end
    end
    
    @out << ansi_colorize(color)
    @out << text.gsub("\n", ansi_clear + "\n" + ansi_colorize(color))
    @out << ansi_clear
    @out << ansi_colorize(@subcolors[:self]) if @subcolors && @subcolors[:self]
  else
    @out << text
  end
end

Protected Instance Methods

setup(options) click to toggle source
# File lib/coderay/encoders/terminal.rb, line 98
def setup(options)
  super
  @opened = []
  @subcolors = nil
end