]> git.cworth.org Git - sup/blob - lib/sup/colormap.rb
fix garbaged text in textfield when using ncursesw
[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 = (CURSES_COLORS.size - 1) * (CURSES_COLORS.size - 1)
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     :system_buf => { :fg => "blue", :bg => "default" },
51     :regular_buf => { :fg => "white", :bg => "default" },
52     :modified_buffer => { :fg => "yellow", :bg => "default", :attrs => ["bold"] },
53   }
54   
55   def initialize
56     raise "only one instance can be created" if @@instance
57     @@instance = self
58     @entries = {}
59     @color_pairs = {[Curses::COLOR_WHITE, Curses::COLOR_BLACK] => 0}
60     @users = []
61     @next_id = 0
62     yield self if block_given?
63     @entries[highlight_sym(:none)] = highlight_for(Curses::COLOR_WHITE,
64                                                    Curses::COLOR_BLACK,
65                                                    []) + [nil]
66   end
67
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
73
74     @entries[sym] = [fg, bg, attrs, nil]
75     @entries[highlight_sym(sym)] = opts[:highlight] ? @entries[opts[:highlight]] : highlight_for(fg, bg, attrs) + [nil]
76   end
77
78   def highlight_sym sym
79     "#{sym}_highlight".intern
80   end
81
82   def highlight_for fg, bg, attrs
83     hfg =
84       case fg
85       when Curses::COLOR_BLUE
86         Curses::COLOR_WHITE
87       when Curses::COLOR_YELLOW, Curses::COLOR_GREEN
88         fg
89       else
90         Curses::COLOR_BLACK
91       end
92
93     hbg = 
94       case bg
95       when Curses::COLOR_CYAN
96         Curses::COLOR_YELLOW
97       when Curses::COLOR_YELLOW
98         Curses::COLOR_BLUE
99       else
100         Curses::COLOR_CYAN
101       end
102
103     attrs =
104       if fg == Curses::COLOR_WHITE && attrs.include?(Curses::A_BOLD)
105         [Curses::A_BOLD]
106       else
107         case hfg
108         when Curses::COLOR_BLACK
109           []
110         else
111           [Curses::A_BOLD]
112         end
113       end
114     [hfg, hbg, attrs]
115   end
116
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
121
122     ## if this color is cached, return it
123     fg, bg, attrs, color = @entries[sym]
124     return color if color
125
126     if(cp = @color_pairs[[fg, bg]])
127       ## nothing
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
131       id = @next_id
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})"
135
136       cp = @color_pairs[[fg, bg]] = Curses.color_pair(id)
137       ## delete the old mapping, if it exists
138       if @users[cp]
139         @users[cp].each do |usym|
140           Redwood::log "dropping color #{usym} (#{id})"
141           @entries[usym][3] = nil
142         end
143         @users[cp] = []
144       end
145     end
146
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
151     color
152   end
153
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
160     end
161
162     error = nil
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}" } : []
167
168       if user_colors && (ucolor = user_colors[k])
169         if(ufg = ucolor[:fg])
170           begin
171             fg = Curses.const_get "COLOR_#{ufg.upcase}"
172           rescue NameError
173             error ||= "Warning: there is no color named \"#{ufg}\", using fallback."
174             Redwood::log "Warning: there is no color named \"#{ufg}\""
175           end
176         end
177
178         if(ubg = ucolor[:bg])
179           begin
180             bg = Curses.const_get "COLOR_#{ubg.upcase}"
181           rescue NameError
182             error ||= "Warning: there is no color named \"#{ubg}\", using fallback."
183             Redwood::log "Warning: there is no color named \"#{ubg}\""
184           end
185         end
186
187         if(uattrs = ucolor[:attrs])
188           attrs = [*uattrs].flatten.map do |a|
189             begin
190               Curses.const_get "A_#{a.upcase}"
191             rescue NameError
192               error ||= "Warning: there is no attribute named \"#{a}\", using fallback."
193               Redwood::log "Warning: there is no attribute named \"#{a}\", using fallback."
194             end
195           end
196         end
197       end
198
199       symbol = (k.to_s + "_color").to_sym
200       add symbol, fg, bg, attrs
201     end
202
203     BufferManager.flash error if error
204   end
205
206   def self.instance; @@instance; end
207   def self.method_missing meth, *a
208     Colormap.new unless @@instance
209     @@instance.send meth, *a
210   end
211 end
212
213 end