10 CURSES_COLORS = [Curses::COLOR_BLACK, Curses::COLOR_RED, Curses::COLOR_GREEN,
11 Curses::COLOR_YELLOW, Curses::COLOR_BLUE,
12 Curses::COLOR_MAGENTA, Curses::COLOR_CYAN,
13 Curses::COLOR_WHITE, Curses::COLOR_DEFAULT]
14 NUM_COLORS = (CURSES_COLORS.size - 1) * (CURSES_COLORS.size - 1)
17 :status => { :fg => "white", :bg => "blue", :attrs => ["bold"] },
18 :index_old => { :fg => "white", :bg => "default" },
19 :index_new => { :fg => "white", :bg => "default", :attrs => ["bold"] },
20 :index_starred => { :fg => "yellow", :bg => "default", :attrs => ["bold"] },
21 :index_draft => { :fg => "red", :bg => "default", :attrs => ["bold"] },
22 :labellist_old => { :fg => "white", :bg => "default" },
23 :labellist_new => { :fg => "white", :bg => "default", :attrs => ["bold"] },
24 :twiddle => { :fg => "blue", :bg => "default" },
25 :label => { :fg => "yellow", :bg => "default" },
26 :message_patina => { :fg => "black", :bg => "green" },
27 :alternate_patina => { :fg => "black", :bg => "blue" },
28 :missing_message => { :fg => "black", :bg => "red" },
29 :attachment => { :fg => "cyan", :bg => "default" },
30 :cryptosig_valid => { :fg => "yellow", :bg => "default", :attrs => ["bold"] },
31 :cryptosig_unknown => { :fg => "cyan", :bg => "default" },
32 :cryptosig_invalid => { :fg => "yellow", :bg => "red", :attrs => ["bold"] },
33 :generic_notice_patina => { :fg => "cyan", :bg => "default" },
34 :quote_patina => { :fg => "yellow", :bg => "default" },
35 :sig_patina => { :fg => "yellow", :bg => "default" },
36 :quote => { :fg => "yellow", :bg => "default" },
37 :sig => { :fg => "yellow", :bg => "default" },
38 :to_me => { :fg => "green", :bg => "default" },
39 :starred => { :fg => "yellow", :bg => "default", :attrs => ["bold"] },
40 :starred_patina => { :fg => "yellow", :bg => "green", :attrs => ["bold"] },
41 :alternate_starred_patina => { :fg => "yellow", :bg => "blue", :attrs => ["bold"] },
42 :snippet => { :fg => "cyan", :bg => "default" },
43 :option => { :fg => "white", :bg => "default" },
44 :tagged => { :fg => "yellow", :bg => "default", :attrs => ["bold"] },
45 :draft_notification => { :fg => "red", :bg => "default", :attrs => ["bold"] },
46 :completion_character => { :fg => "white", :bg => "default", :attrs => ["bold"] },
47 :horizontal_selector_selected => { :fg => "yellow", :bg => "default", :attrs => ["bold"] },
48 :horizontal_selector_unselected => { :fg => "cyan", :bg => "default" },
49 :search_highlight => { :fg => "black", :bg => "yellow", :attrs => ["bold"] },
50 :system_buf => { :fg => "blue", :bg => "default" },
51 :regular_buf => { :fg => "white", :bg => "default" },
52 :modified_buffer => { :fg => "yellow", :bg => "default", :attrs => ["bold"] },
56 raise "only one instance can be created" if @@instance
59 @color_pairs = {[Curses::COLOR_WHITE, Curses::COLOR_BLACK] => 0}
62 yield self if block_given?
63 @entries[highlight_sym(:none)] = highlight_for(Curses::COLOR_WHITE,
68 def add sym, fg, bg, attr=nil, opts={}
69 raise ArgumentError, "color for #{sym} already defined" if @entries.member? sym
70 raise ArgumentError, "color '#{fg}' unknown" unless CURSES_COLORS.include? fg
71 raise ArgumentError, "color '#{bg}' unknown" unless CURSES_COLORS.include? bg
72 attrs = [attr].flatten.compact
74 @entries[sym] = [fg, bg, attrs, nil]
75 @entries[highlight_sym(sym)] = opts[:highlight] ? @entries[opts[:highlight]] : highlight_for(fg, bg, attrs) + [nil]
79 "#{sym}_highlight".intern
82 def highlight_for fg, bg, attrs
85 when Curses::COLOR_BLUE
87 when Curses::COLOR_YELLOW, Curses::COLOR_GREEN
95 when Curses::COLOR_CYAN
97 when Curses::COLOR_YELLOW
104 if fg == Curses::COLOR_WHITE && attrs.include?(Curses::A_BOLD)
108 when Curses::COLOR_BLACK
117 def color_for sym, highlight=false
118 sym = highlight_sym(sym) if highlight
119 return Curses::COLOR_BLACK if sym == :none
120 raise ArgumentError, "undefined color #{sym}" unless @entries.member? sym
122 ## if this color is cached, return it
123 fg, bg, attrs, color = @entries[sym]
124 return color if color
126 if(cp = @color_pairs[[fg, bg]])
128 else ## need to get a new colorpair
129 @next_id = (@next_id + 1) % NUM_COLORS
130 @next_id += 1 if @next_id == 0 # 0 is always white on black
132 Redwood::log "colormap: for color #{sym}, using id #{id} -> #{fg}, #{bg}"
133 Curses.init_pair id, fg, bg or raise ArgumentError,
134 "couldn't initialize curses color pair #{fg}, #{bg} (key #{id})"
136 cp = @color_pairs[[fg, bg]] = Curses.color_pair(id)
137 ## delete the old mapping, if it exists
139 @users[cp].each do |usym|
140 Redwood::log "dropping color #{usym} (#{id})"
141 @entries[usym][3] = nil
147 ## by now we have a color pair
148 color = attrs.inject(cp) { |color, attr| color | attr }
149 @entries[sym][3] = color # fill the cache
150 (@users[cp] ||= []) << sym # record entry as a user of that color pair
154 ## Try to use the user defined colors, in case of an error fall back
155 ## to the default ones.
156 def populate_colormap
157 user_colors = if File.exists? Redwood::COLOR_FN
158 Redwood::log "loading user colors from #{Redwood::COLOR_FN}"
159 Redwood::load_yaml_obj Redwood::COLOR_FN
163 Colormap::DEFAULT_COLORS.each_pair do |k, v|
164 fg = Curses.const_get "COLOR_#{v[:fg].upcase}"
165 bg = Curses.const_get "COLOR_#{v[:bg].upcase}"
166 attrs = v[:attrs] ? v[:attrs].map { |a| Curses.const_get "A_#{a.upcase}" } : []
168 if user_colors && (ucolor = user_colors[k])
169 if(ufg = ucolor[:fg])
171 fg = Curses.const_get "COLOR_#{ufg.upcase}"
173 error ||= "Warning: there is no color named \"#{ufg}\", using fallback."
174 Redwood::log "Warning: there is no color named \"#{ufg}\""
178 if(ubg = ucolor[:bg])
180 bg = Curses.const_get "COLOR_#{ubg.upcase}"
182 error ||= "Warning: there is no color named \"#{ubg}\", using fallback."
183 Redwood::log "Warning: there is no color named \"#{ubg}\""
187 if(uattrs = ucolor[:attrs])
188 attrs = [*uattrs].flatten.map do |a|
190 Curses.const_get "A_#{a.upcase}"
192 error ||= "Warning: there is no attribute named \"#{a}\", using fallback."
193 Redwood::log "Warning: there is no attribute named \"#{a}\", using fallback."
199 symbol = (k.to_s + "_color").to_sym
200 add symbol, fg, bg, attrs
203 BufferManager.flash error if error
206 def self.instance; @@instance; end
207 def self.method_missing meth, *a
208 Colormap.new unless @@instance
209 @@instance.send meth, *a