]> git.cworth.org Git - sup/blob - lib/sup/colormap.rb
Merge branch 'undo-manager' into next
[sup] / lib / sup / colormap.rb
1 module Curses
2   COLOR_DEFAULT = -1
3 end
4
5 module Redwood
6
7 class Colormap
8   @@instance = nil
9
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 = 15
15
16   DEFAULT_COLORS = {
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   }
51   
52   def initialize
53     raise "only one instance can be created" if @@instance
54     @@instance = self
55     @entries = {}
56     @color_pairs = {[Curses::COLOR_WHITE, Curses::COLOR_BLACK] => 0}
57     @users = []
58     @next_id = 0
59     yield self if block_given?
60     @entries[highlight_sym(:none)] = highlight_for(Curses::COLOR_WHITE,
61                                                    Curses::COLOR_BLACK,
62                                                    []) + [nil]
63   end
64
65   def add sym, fg, bg, attr=nil, opts={}
66     raise ArgumentError, "color for #{sym} already defined" if @entries.member? sym
67     raise ArgumentError, "color '#{fg}' unknown" unless CURSES_COLORS.include? fg
68     raise ArgumentError, "color '#{bg}' unknown" unless CURSES_COLORS.include? bg
69     attrs = [attr].flatten.compact
70
71     @entries[sym] = [fg, bg, attrs, nil]
72     @entries[highlight_sym(sym)] = opts[:highlight] ? @entries[opts[:highlight]] : highlight_for(fg, bg, attrs) + [nil]
73   end
74
75   def highlight_sym sym
76     "#{sym}_highlight".intern
77   end
78
79   def highlight_for fg, bg, attrs
80     hfg =
81       case fg
82       when Curses::COLOR_BLUE
83         Curses::COLOR_WHITE
84       when Curses::COLOR_YELLOW, Curses::COLOR_GREEN
85         fg
86       else
87         Curses::COLOR_BLACK
88       end
89
90     hbg = 
91       case bg
92       when Curses::COLOR_CYAN
93         Curses::COLOR_YELLOW
94       when Curses::COLOR_YELLOW
95         Curses::COLOR_BLUE
96       else
97         Curses::COLOR_CYAN
98       end
99
100     attrs =
101       if fg == Curses::COLOR_WHITE && attrs.include?(Curses::A_BOLD)
102         [Curses::A_BOLD]
103       else
104         case hfg
105         when Curses::COLOR_BLACK
106           []
107         else
108           [Curses::A_BOLD]
109         end
110       end
111     [hfg, hbg, attrs]
112   end
113
114   def color_for sym, highlight=false
115     sym = highlight_sym(sym) if highlight
116     return Curses::COLOR_BLACK if sym == :none
117     raise ArgumentError, "undefined color #{sym}" unless @entries.member? sym
118
119     ## if this color is cached, return it
120     fg, bg, attrs, color = @entries[sym]
121     return color if color
122
123     if(cp = @color_pairs[[fg, bg]])
124       ## nothing
125     else ## need to get a new colorpair
126       @next_id = (@next_id + 1) % NUM_COLORS
127       @next_id += 1 if @next_id == 0 # 0 is always white on black
128       id = @next_id
129       Redwood::log "colormap: for color #{sym}, using id #{id} -> #{fg}, #{bg}"
130       Curses.init_pair id, fg, bg or raise ArgumentError,
131         "couldn't initialize curses color pair #{fg}, #{bg} (key #{id})"
132
133       cp = @color_pairs[[fg, bg]] = Curses.color_pair(id)
134       ## delete the old mapping, if it exists
135       if @users[cp]
136         @users[cp].each do |usym|
137           Redwood::log "dropping color #{usym} (#{id})"
138           @entries[usym][3] = nil
139         end
140         @users[cp] = []
141       end
142     end
143
144     ## by now we have a color pair
145     color = attrs.inject(cp) { |color, attr| color | attr }
146     @entries[sym][3] = color # fill the cache
147     (@users[cp] ||= []) << sym # record entry as a user of that color pair
148     color
149   end
150
151   ## Try to use the user defined colors, in case of an error fall back
152   ## to the default ones.
153   def populate_colormap
154     user_colors = if File.exists? Redwood::COLOR_FN
155       Redwood::log "loading user colors from #{Redwood::COLOR_FN}"
156       Redwood::load_yaml_obj Redwood::COLOR_FN
157     end
158
159     error = nil
160     Colormap::DEFAULT_COLORS.each_pair do |k, v|
161       fg = Curses.const_get "COLOR_#{v[:fg].upcase}"
162       bg = Curses.const_get "COLOR_#{v[:bg].upcase}"
163       attrs = v[:attrs] ? v[:attrs].map { |a| Curses.const_get "A_#{a.upcase}" } : []
164
165       if user_colors && (ucolor = user_colors[k])
166         if(ufg = ucolor[:fg])
167           begin
168             fg = Curses.const_get "COLOR_#{ufg.upcase}"
169           rescue NameError
170             error ||= "Warning: there is no color named \"#{ufg}\", using fallback."
171             Redwood::log "Warning: there is no color named \"#{ufg}\""
172           end
173         end
174
175         if(ubg = ucolor[:bg])
176           begin
177             bg = Curses.const_get "COLOR_#{ubg.upcase}"
178           rescue NameError
179             error ||= "Warning: there is no color named \"#{ubg}\", using fallback."
180             Redwood::log "Warning: there is no color named \"#{ubg}\""
181           end
182         end
183
184         if(uattrs = ucolor[:attrs])
185           attrs = [*uattrs].flatten.map do |a|
186             begin
187               Curses.const_get "A_#{a.upcase}"
188             rescue NameError
189               error ||= "Warning: there is no attribute named \"#{a}\", using fallback."
190               Redwood::log "Warning: there is no attribute named \"#{a}\", using fallback."
191             end
192           end
193         end
194       end
195
196       symbol = (k.to_s + "_color").to_sym
197       add symbol, fg, bg, attrs
198     end
199
200     BufferManager.flash error if error
201   end
202
203   def self.instance; @@instance; end
204   def self.method_missing meth, *a
205     Colormap.new unless @@instance
206     @@instance.send meth, *a
207   end
208 end
209
210 end