Skip to content

Commit 4a85580

Browse files
committed
tweak Terminal encoder, cleanup token kinds
1 parent 3cc5131 commit 4a85580

File tree

3 files changed

+181
-163
lines changed

3 files changed

+181
-163
lines changed

lib/coderay/encoders/terminal.rb

Lines changed: 101 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -19,105 +19,135 @@ class Terminal < Encoder
1919
register_for :terminal
2020

2121
TOKEN_COLORS = {
22-
:annotation => "\e[35m",
23-
:attribute_name => "\e[33m",
22+
:debug => "\e[1;37;44m",
23+
24+
:annotation => "\e[34m",
25+
:attribute_name => "\e[35m",
2426
:attribute_value => "\e[31m",
25-
:binary => "\e[1;35m",
27+
:binary => {
28+
:self => "\e[31m",
29+
:char => "\e[1;31m",
30+
:delimiter => "\e[1;31m",
31+
},
2632
:char => {
27-
:self => "\e[36m", :delimiter => "\e[1;34m"
33+
:self => "\e[35m",
34+
:delimiter => "\e[1;35m"
2835
},
29-
:class => "\e[1;35m",
36+
:class => "\e[1;35;4m",
3037
:class_variable => "\e[36m",
3138
: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-
:docstring => "\e[31m\e[4m",
41-
:entity => "\e[33m",
42-
:error => "\e[1;33m\e[41m",
39+
:comment => {
40+
:self => "\e[1;30m",
41+
:char => "\e[37m",
42+
:delimiter => "\e[37m",
43+
},
44+
:constant => "\e[1;34;4m",
45+
:decorator => "\e[35m",
46+
:definition => "\e[1;33m",
47+
:directive => "\e[33m",
48+
:docstring => "\e[31m",
49+
:doctype => "\e[1;34m",
50+
:done => "\e[1;30;2m",
51+
:entity => "\e[31m",
52+
:error => "\e[1;37;41m",
4353
:exception => "\e[1;31m",
4454
:float => "\e[1;35m",
4555
:function => "\e[1;34m",
46-
:global_variable => "\e[42m",
56+
:global_variable => "\e[1;32m",
4757
:hex => "\e[1;36m",
48-
:include => "\e[33m",
58+
:id => "\e[1;34m",
59+
:include => "\e[31m",
4960
:integer => "\e[1;34m",
50-
:key => "\e[35m",
51-
:label => "\e[1;15m",
61+
:imaginary => "\e[1;34m",
62+
:important => "\e[1;31m",
63+
:key => {
64+
:self => "\e[35m",
65+
:char => "\e[1;35m",
66+
:delimiter => "\e[1;35m",
67+
},
68+
:keyword => "\e[32m",
69+
:label => "\e[1;33m",
5270
:local_variable => "\e[33m",
53-
:octal => "\e[1;35m",
54-
:operator_name => "\e[1;29m",
71+
:namespace => "\e[1;35m",
72+
:octal => "\e[1;34m",
73+
:predefined => "\e[36m",
5574
:predefined_constant => "\e[1;36m",
56-
:predefined_type => "\e[1;30m",
57-
:predefined => "\e[4m\e[1;34m",
58-
:preprocessor => "\e[36m",
75+
:predefined_type => "\e[1;32m",
76+
:preprocessor => "\e[1;36m",
5977
:pseudo_class => "\e[1;34m",
6078
:regexp => {
61-
:self => "\e[31m",
62-
:content => "\e[31m",
63-
:delimiter => "\e[1;29m",
79+
:self => "\e[35m",
80+
:delimiter => "\e[1;35m",
6481
:modifier => "\e[35m",
82+
:char => "\e[1;35m",
6583
},
66-
:reserved => "\e[1;31m",
84+
:reserved => "\e[32m",
6785
:shell => {
68-
:self => "\e[42m",
69-
:content => "\e[1;29m",
70-
:delimiter => "\e[37m",
86+
:self => "\e[33m",
87+
:char => "\e[1;33m",
88+
:delimiter => "\e[1;33m",
89+
:escape => "\e[1;33m",
7190
},
7291
:string => {
73-
:self => "\e[32m",
74-
:modifier => "\e[1;32m",
75-
:escape => "\e[1;36m",
76-
:delimiter => "\e[1;32m",
77-
:char => "\e[1;36m",
92+
:self => "\e[31m",
93+
:modifier => "\e[1;31m",
94+
:char => "\e[1;35m",
95+
:delimiter => "\e[1;31m",
96+
:escape => "\e[1;31m",
97+
},
98+
:symbol => {
99+
:self => "\e[33m",
100+
:delimiter => "\e[1;33m",
78101
},
79-
:symbol => "\e[1;32m",
80-
:tag => "\e[1;34m",
102+
:tag => "\e[32m",
81103
:type => "\e[1;34m",
82104
:value => "\e[36m",
83-
:variable => "\e[1;34m",
105+
:variable => "\e[34m",
84106

85-
:insert => "\e[42m",
86-
:delete => "\e[41m",
87-
:change => "\e[44m",
88-
:head => "\e[45m"
107+
:insert => {
108+
:self => "\e[42m",
109+
:insert => "\e[1;32;42m",
110+
:eyecatcher => "\e[102m",
111+
},
112+
:delete => {
113+
:self => "\e[41m",
114+
:delete => "\e[1;31;41m",
115+
:eyecatcher => "\e[101m",
116+
},
117+
:change => {
118+
:self => "\e[44m",
119+
:change => "\e[37;44m",
120+
},
121+
:head => {
122+
:self => "\e[45m",
123+
:filename => "\e[37;45m"
124+
},
89125
}
126+
90127
TOKEN_COLORS[:keyword] = TOKEN_COLORS[:reserved]
91128
TOKEN_COLORS[:method] = TOKEN_COLORS[:function]
92-
TOKEN_COLORS[:imaginary] = TOKEN_COLORS[:complex]
93-
TOKEN_COLORS[:begin_group] = TOKEN_COLORS[:end_group] =
94-
TOKEN_COLORS[:escape] = TOKEN_COLORS[:delimiter]
129+
TOKEN_COLORS[:escape] = TOKEN_COLORS[:delimiter]
95130

96131
protected
97132

98133
def setup(options)
99134
super
100135
@opened = []
101-
@subcolors = nil
136+
@color_scopes = [TOKEN_COLORS]
102137
end
103138

104139
public
105140

106141
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
142+
if color = @color_scopes.last[kind]
143+
color = color[:self] if color.is_a? Hash
116144

117145
@out << color
118-
@out << text.gsub("\n", "\e[0m\n" + color)
146+
@out << (text.index("\n") ? text.gsub("\n", "\e[0m\n" + color) : text)
119147
@out << "\e[0m"
120-
@out << @subcolors[:self] if @subcolors
148+
if outer_color = @color_scopes.last[:self]
149+
@out << outer_color
150+
end
121151
else
122152
@out << text
123153
end
@@ -130,40 +160,33 @@ def begin_group kind
130160
alias begin_line begin_group
131161

132162
def end_group kind
133-
if @opened.empty?
134-
# nothing to close
135-
else
136-
@opened.pop
163+
if @opened.pop
164+
@color_scopes.pop
137165
@out << "\e[0m"
138-
@out << open_token(@opened.last)
166+
if outer_color = @color_scopes.last[:self]
167+
@out << outer_color
168+
end
139169
end
140170
end
141171

142172
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 << (@line_filler ||= "\t" * 100 + "\e[0m")
150-
@out << open_token(@opened.last)
151-
end
173+
@out << (@line_filler ||= "\t" * 100)
174+
end_group kind
152175
end
153176

154177
private
155178

156179
def open_token kind
157-
if color = TOKEN_COLORS[kind]
158-
if Hash === color
159-
@subcolors = color
180+
if color = @color_scopes.last[kind]
181+
if color.is_a? Hash
182+
@color_scopes << color
160183
color[:self]
161184
else
162-
@subcolors = {}
185+
@color_scopes << @color_scopes.last
163186
color
164187
end
165188
else
166-
@subcolors = nil
189+
@color_scopes << @color_scopes.last
167190
''
168191
end
169192
end

lib/coderay/styles/alpha.rb

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,26 +57,25 @@ class Alpha < Style
5757
.attribute-name { color:#b48 }
5858
.attribute-value { color:#700 }
5959
.binary { color:#549 }
60-
.binary .delimiter { color:#325 }
6160
.binary .char { color:#325 }
61+
.binary .delimiter { color:#325 }
62+
.char { color:#D20 }
6263
.char .content { color:#D20 }
6364
.char .delimiter { color:#710 }
64-
.char { color:#D20 }
6565
.class { color:#B06; font-weight:bold }
6666
.class-variable { color:#369 }
6767
.color { color:#0A0 }
6868
.comment { color:#777 }
6969
.comment .char { color:#444 }
7070
.comment .delimiter { color:#444 }
71-
.complex { color:#A08 }
7271
.constant { color:#036; font-weight:bold }
7372
.decorator { color:#B0B }
7473
.definition { color:#099; font-weight:bold }
7574
.delimiter { color:black }
7675
.directive { color:#088; font-weight:bold }
77-
.doc { color:#970 }
78-
.doc-string { color:#D42; font-weight:bold }
76+
.docstring { color:#D42; }
7977
.doctype { color:#34b }
78+
.done { text-decoration: line-through; color: gray }
8079
.entity { color:#800; font-weight:bold }
8180
.error { color:#F00; background-color:#FAA }
8281
.escape { color:#666 }
@@ -87,16 +86,16 @@ class Alpha < Style
8786
.global-variable { color:#d70 }
8887
.hex { color:#02b }
8988
.id { color:#33D; font-weight:bold }
90-
.imaginary { color:#f00 }
9189
.include { color:#B44; font-weight:bold }
9290
.inline { background-color: hsla(0,0%,0%,0.07); color: black }
9391
.inline-delimiter { font-weight: bold; color: #666 }
9492
.instance-variable { color:#33B }
9593
.integer { color:#00D }
94+
.imaginary { color:#f00 }
9695
.important { color:#D00 }
96+
.key { color: #606 }
9797
.key .char { color: #60f }
9898
.key .delimiter { color: #404 }
99-
.key { color: #606 }
10099
.keyword { color:#080; font-weight:bold }
101100
.label { color:#970; font-weight:bold }
102101
.local-variable { color:#963 }
@@ -108,30 +107,30 @@ class Alpha < Style
108107
.predefined-type { color:#0a5; font-weight:bold }
109108
.preprocessor { color:#579 }
110109
.pseudo-class { color:#00C; font-weight:bold }
110+
.regexp { background-color:hsla(300,100%,50%,0.06); }
111111
.regexp .content { color:#808 }
112112
.regexp .delimiter { color:#404 }
113113
.regexp .modifier { color:#C2C }
114-
.regexp { background-color:hsla(300,100%,50%,0.06); }
115114
.reserved { color:#080; font-weight:bold }
115+
.shell { background-color:hsla(120,100%,50%,0.06); }
116116
.shell .content { color:#2B2 }
117117
.shell .delimiter { color:#161 }
118-
.shell { background-color:hsla(120,100%,50%,0.06); }
118+
.string { background-color:hsla(0,100%,50%,0.05); }
119119
.string .char { color: #b0b }
120120
.string .content { color: #D20 }
121121
.string .delimiter { color: #710 }
122122
.string .modifier { color: #E40 }
123-
.string { background-color:hsla(0,100%,50%,0.05); }
123+
.symbol { color:#A60 }
124124
.symbol .content { color:#A60 }
125125
.symbol .delimiter { color:#630 }
126-
.symbol { color:#A60 }
127126
.tag { color:#070 }
128127
.type { color:#339; font-weight:bold }
129-
.value { color: #088; }
130-
.variable { color:#037 }
128+
.value { color: #088 }
129+
.variable { color:#037 }
131130
132131
.insert { background: hsla(120,100%,50%,0.12) }
133132
.delete { background: hsla(0,100%,50%,0.12) }
134-
.change { color: #bbf; background: #007; }
133+
.change { color: #bbf; background: #007 }
135134
.head { color: #f8f; background: #505 }
136135
.head .filename { color: white; }
137136
@@ -142,8 +141,6 @@ class Alpha < Style
142141
.delete .delete { color: #c00; background:transparent; font-weight:bold }
143142
.change .change { color: #88f }
144143
.head .head { color: #f4f }
145-
146-
.done { text-decoration: line-through; color: gray }
147144
TOKENS
148145

149146
end

0 commit comments

Comments
 (0)