Class | CodeRay::Encoders::Terminal |
In: |
lib/coderay/encoders/terminal.rb
|
Parent: | Encoder |
Outputs code highlighted for a color terminal.
Note: This encoder is in beta. It currently doesn‘t use the Styles.
Alias: term
By Rob Aldred (robaldred.co.uk)
Based on idea by Nathan Weizenbaum (nex-3.com)
MIT License (www.opensource.org/licenses/mit-license.php)
TOKEN_COLORS | = | { :annotation => '35', :attribute_name => '33', :attribute_value => '31', :binary => '1;35', :char => { :self => '36', :delimiter => '34' |
# File lib/coderay/encoders/terminal.rb, line 126 126: def begin_group kind 127: @opened << kind 128: @out << open_token(kind) 129: end
# File lib/coderay/encoders/terminal.rb, line 132 132: def end_group kind 133: if @opened.empty? 134: # nothing to close 135: else 136: @opened.pop 137: @out << ansi_clear 138: @out << open_token(@opened.last) 139: end 140: end
# File lib/coderay/encoders/terminal.rb, line 142 142: def end_line kind 143: if @opened.empty? 144: # nothing to close 145: else 146: @opened.pop 147: # whole lines to be highlighted, 148: # eg. added/modified/deleted lines in a diff 149: @out << "\t" * 100 + ansi_clear 150: @out << open_token(@opened.last) 151: end 152: end
# File lib/coderay/encoders/terminal.rb, line 106 106: def text_token text, kind 107: if color = (@subcolors || TOKEN_COLORS)[kind] 108: if Hash === color 109: if color[:self] 110: color = color[:self] 111: else 112: @out << text 113: return 114: end 115: end 116: 117: @out << ansi_colorize(color) 118: @out << text.gsub("\n", ansi_clear + "\n" + ansi_colorize(color)) 119: @out << ansi_clear 120: @out << ansi_colorize(@subcolors[:self]) if @subcolors && @subcolors[:self] 121: else 122: @out << text 123: end 124: end