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 156
def begin_group kind
  @opened << kind
  @out << open_token(kind)
end
Also aliased as: begin_line
begin_line(kind)
Alias for: begin_group
end_group(kind) click to toggle source
# File lib/coderay/encoders/terminal.rb, line 162
def end_group kind
  if @opened.pop
    @color_scopes.pop
    @out << "\e[0m"
    if outer_color = @color_scopes.last[:self]
      @out << outer_color
    end
  end
end
end_line(kind) click to toggle source
# File lib/coderay/encoders/terminal.rb, line 172
def end_line kind
  @out << (@line_filler ||= "\t" * 100)
  end_group kind
end
text_token(text, kind) click to toggle source
# File lib/coderay/encoders/terminal.rb, line 141
def text_token text, kind
  if color = @color_scopes.last[kind]
    color = color[:self] if color.is_a? Hash
    
    @out << color
    @out << (text.index("\n") ? text.gsub("\n", "\e[0m\n" + color) : text)
    @out << "\e[0m"
    if outer_color = @color_scopes.last[:self]
      @out << outer_color
    end
  else
    @out << text
  end
end

Protected Instance Methods

setup(options) click to toggle source
Calls superclass method CodeRay::Encoders::Encoder#setup
# File lib/coderay/encoders/terminal.rb, line 133
def setup(options)
  super
  @opened = []
  @color_scopes = [TOKEN_COLORS]
end

Private Instance Methods

open_token(kind) click to toggle source
# File lib/coderay/encoders/terminal.rb, line 179
def open_token kind
  if color = @color_scopes.last[kind]
    if color.is_a? Hash
      @color_scopes << color
      color[:self]
    else
      @color_scopes << @color_scopes.last
      color
    end
  else
    @color_scopes << @color_scopes.last
    ''
  end
end