@@ -19,73 +19,73 @@ class Terminal < Encoder
19
19
register_for :terminal
20
20
21
21
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" ,
26
26
:char => {
27
- :self => '36' , :delimiter => '1;34'
27
+ :self => " \e [36m" , :delimiter => " \e [1;34m"
28
28
} ,
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" ,
60
60
: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" ,
65
65
} ,
66
- :reserved => '1;31' ,
66
+ :reserved => " \e [1;31m" ,
67
67
:shell => {
68
- :self => '42' ,
69
- :content => '1;29' ,
70
- :delimiter => '37' ,
68
+ :self => " \e [42m" ,
69
+ :content => " \e [1;29m" ,
70
+ :delimiter => " \e [37m" ,
71
71
} ,
72
72
: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" ,
78
78
} ,
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" ,
84
84
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"
89
89
}
90
90
TOKEN_COLORS [ :keyword ] = TOKEN_COLORS [ :reserved ]
91
91
TOKEN_COLORS [ :method ] = TOKEN_COLORS [ :function ]
@@ -114,10 +114,10 @@ def text_token text, kind
114
114
end
115
115
end
116
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 ]
117
+ @out << color
118
+ @out << text . gsub ( "\n " , " \e [0m \ n " + color )
119
+ @out << " \e [0m"
120
+ @out << @subcolors [ :self ] if @subcolors
121
121
else
122
122
@out << text
123
123
end
@@ -134,7 +134,7 @@ def end_group kind
134
134
# nothing to close
135
135
else
136
136
@opened . pop
137
- @out << ansi_clear
137
+ @out << " \e [0m"
138
138
@out << open_token ( @opened . last )
139
139
end
140
140
end
@@ -146,7 +146,7 @@ def end_line kind
146
146
@opened . pop
147
147
# whole lines to be highlighted,
148
148
# eg. added/modified/deleted lines in a diff
149
- @out << "\t " * 100 + ansi_clear
149
+ @out << ( @line_filler ||= "\t " * 100 + " \e [0m" )
150
150
@out << open_token ( @opened . last )
151
151
end
152
152
end
@@ -157,23 +157,16 @@ def open_token kind
157
157
if color = TOKEN_COLORS [ kind ]
158
158
if Hash === color
159
159
@subcolors = color
160
- ansi_colorize ( color [ :self ] ) if color [ :self ]
160
+ color [ :self ]
161
161
else
162
162
@subcolors = { }
163
- ansi_colorize ( color )
163
+ color
164
164
end
165
165
else
166
166
@subcolors = nil
167
167
''
168
168
end
169
169
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
177
170
end
178
171
end
179
- end
172
+ end
0 commit comments