Skip to content

Commit e2acec3

Browse files
committed
massively improve Terminal encoder speed
1 parent 82fdb13 commit e2acec3

File tree

1 file changed

+67
-74
lines changed

1 file changed

+67
-74
lines changed

lib/coderay/encoders/terminal.rb

Lines changed: 67 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -19,73 +19,73 @@ class Terminal < Encoder
1919
register_for :terminal
2020

2121
TOKEN_COLORS = {
22-
:annotation => '35',
23-
:attribute_name => '33',
24-
:attribute_value => '31',
25-
:binary => '1;35',
22+
:annotation => "\e[35m",
23+
:attribute_name => "\e[33m",
24+
:attribute_value => "\e[31m",
25+
:binary => "\e[1;35m",
2626
:char => {
27-
:self => '36', :delimiter => '1;34'
27+
:self => "\e[36m", :delimiter => "\e[1;34m"
2828
},
29-
:class => '1;35',
30-
:class_variable => '36',
31-
:color => '32',
32-
:comment => '37',
33-
:complex => '1;34',
34-
:constant => ['1;34', '4'],
35-
:decoration => '35',
36-
:definition => '1;32',
37-
:directive => ['32', '4'],
38-
:doc => '46',
39-
:doctype => '1;30',
40-
:doc_string => ['31', '4'],
41-
:entity => '33',
42-
:error => ['1;33', '41'],
43-
:exception => '1;31',
44-
:float => '1;35',
45-
:function => '1;34',
46-
:global_variable => '42',
47-
:hex => '1;36',
48-
:include => '33',
49-
:integer => '1;34',
50-
:key => '35',
51-
:label => '1;15',
52-
:local_variable => '33',
53-
:octal => '1;35',
54-
:operator_name => '1;29',
55-
:predefined_constant => '1;36',
56-
:predefined_type => '1;30',
57-
:predefined => ['4', '1;34'],
58-
:preprocessor => '36',
59-
:pseudo_class => '1;34',
29+
:class => "\e[1;35m",
30+
:class_variable => "\e[36m",
31+
:color => "\e[32m",
32+
:comment => "\e[37m",
33+
:complex => "\e[1;34m",
34+
:constant => "\e[1;34m\e[4m",
35+
:decoration => "\e[35m",
36+
:definition => "\e[1;32m",
37+
:directive => "\e[32m\e[4m",
38+
:doc => "\e[46m",
39+
:doctype => "\e[1;30m",
40+
:doc_string => "\e[31m\e[4m",
41+
:entity => "\e[33m",
42+
:error => "\e[1;33m\e[41m",
43+
:exception => "\e[1;31m",
44+
:float => "\e[1;35m",
45+
:function => "\e[1;34m",
46+
:global_variable => "\e[42m",
47+
:hex => "\e[1;36m",
48+
:include => "\e[33m",
49+
:integer => "\e[1;34m",
50+
:key => "\e[35m",
51+
:label => "\e[1;15m",
52+
:local_variable => "\e[33m",
53+
:octal => "\e[1;35m",
54+
:operator_name => "\e[1;29m",
55+
:predefined_constant => "\e[1;36m",
56+
:predefined_type => "\e[1;30m",
57+
:predefined => "\e[4m\e[1;34m",
58+
:preprocessor => "\e[36m",
59+
:pseudo_class => "\e[1;34m",
6060
:regexp => {
61-
:self => '31',
62-
:content => '31',
63-
:delimiter => '1;29',
64-
:modifier => '35',
61+
:self => "\e[31m",
62+
:content => "\e[31m",
63+
:delimiter => "\e[1;29m",
64+
:modifier => "\e[35m",
6565
},
66-
:reserved => '1;31',
66+
:reserved => "\e[1;31m",
6767
:shell => {
68-
:self => '42',
69-
:content => '1;29',
70-
:delimiter => '37',
68+
:self => "\e[42m",
69+
:content => "\e[1;29m",
70+
:delimiter => "\e[37m",
7171
},
7272
:string => {
73-
:self => '32',
74-
:modifier => '1;32',
75-
:escape => '1;36',
76-
:delimiter => '1;32',
77-
:char => '1;36',
73+
:self => "\e[32m",
74+
:modifier => "\e[1;32m",
75+
:escape => "\e[1;36m",
76+
:delimiter => "\e[1;32m",
77+
:char => "\e[1;36m",
7878
},
79-
:symbol => '1;32',
80-
:tag => '1;34',
81-
:type => '1;34',
82-
:value => '36',
83-
:variable => '1;34',
79+
:symbol => "\e[1;32m",
80+
:tag => "\e[1;34m",
81+
:type => "\e[1;34m",
82+
:value => "\e[36m",
83+
:variable => "\e[1;34m",
8484

85-
:insert => '42',
86-
:delete => '41',
87-
:change => '44',
88-
:head => '45'
85+
:insert => "\e[42m",
86+
:delete => "\e[41m",
87+
:change => "\e[44m",
88+
:head => "\e[45m"
8989
}
9090
TOKEN_COLORS[:keyword] = TOKEN_COLORS[:reserved]
9191
TOKEN_COLORS[:method] = TOKEN_COLORS[:function]
@@ -114,10 +114,10 @@ def text_token text, kind
114114
end
115115
end
116116

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]
117+
@out << color
118+
@out << text.gsub("\n", "\e[0m\n" + color)
119+
@out << "\e[0m"
120+
@out << @subcolors[:self] if @subcolors
121121
else
122122
@out << text
123123
end
@@ -134,7 +134,7 @@ def end_group kind
134134
# nothing to close
135135
else
136136
@opened.pop
137-
@out << ansi_clear
137+
@out << "\e[0m"
138138
@out << open_token(@opened.last)
139139
end
140140
end
@@ -146,7 +146,7 @@ def end_line kind
146146
@opened.pop
147147
# whole lines to be highlighted,
148148
# eg. added/modified/deleted lines in a diff
149-
@out << "\t" * 100 + ansi_clear
149+
@out << (@line_filler ||= "\t" * 100 + "\e[0m")
150150
@out << open_token(@opened.last)
151151
end
152152
end
@@ -157,23 +157,16 @@ def open_token kind
157157
if color = TOKEN_COLORS[kind]
158158
if Hash === color
159159
@subcolors = color
160-
ansi_colorize(color[:self]) if color[:self]
160+
color[:self]
161161
else
162162
@subcolors = {}
163-
ansi_colorize(color)
163+
color
164164
end
165165
else
166166
@subcolors = nil
167167
''
168168
end
169169
end
170-
171-
def ansi_colorize(color)
172-
Array(color).map { |c| "\e[#{c}m" }.join
173-
end
174-
def ansi_clear
175-
ansi_colorize(0)
176-
end
177170
end
178171
end
179-
end
172+
end

0 commit comments

Comments
 (0)