]> git.cworth.org Git - sup/blobdiff - lib/sup/colormap.rb
bugfix: edit-message-mode headers broken by case changes
[sup] / lib / sup / colormap.rb
index d13e86ca65639ba15fa04d3ab4df6bbb246c3099..469ed0b91874342a8fde6787008aa625b9d4e576 100644 (file)
@@ -1,4 +1,6 @@
-require "curses"
+module Curses
+  COLOR_DEFAULT = -1
+end
 
 module Redwood
 
@@ -8,8 +10,44 @@ class Colormap
   CURSES_COLORS = [Curses::COLOR_BLACK, Curses::COLOR_RED, Curses::COLOR_GREEN,
                    Curses::COLOR_YELLOW, Curses::COLOR_BLUE,
                    Curses::COLOR_MAGENTA, Curses::COLOR_CYAN,
-                   Curses::COLOR_WHITE]
+                   Curses::COLOR_WHITE, Curses::COLOR_DEFAULT]
   NUM_COLORS = 15
+
+  DEFAULT_COLORS = {
+    :status => { :fg => "white", :bg => "blue", :attrs => ["bold"] },
+    :index_old => { :fg => "white", :bg => "default" },
+    :index_new => { :fg => "white", :bg => "default", :attrs => ["bold"] },
+    :index_starred => { :fg => "yellow", :bg => "default", :attrs => ["bold"] },
+    :index_draft => { :fg => "red", :bg => "default", :attrs => ["bold"] },
+    :labellist_old => { :fg => "white", :bg => "default" },
+    :labellist_new => { :fg => "white", :bg => "default", :attrs => ["bold"] },
+    :twiddle => { :fg => "blue", :bg => "default" },
+    :label => { :fg => "yellow", :bg => "default" },
+    :message_patina => { :fg => "black", :bg => "green" },
+    :alternate_patina => { :fg => "black", :bg => "blue" },
+    :missing_message => { :fg => "black", :bg => "red" },
+    :attachment => { :fg => "cyan", :bg => "default" },
+    :cryptosig_valid => { :fg => "yellow", :bg => "default", :attrs => ["bold"] },
+    :cryptosig_unknown => { :fg => "cyan", :bg => "default" },
+    :cryptosig_invalid => { :fg => "yellow", :bg => "red", :attrs => ["bold"] },
+    :generic_notice_patina => { :fg => "cyan", :bg => "default" },
+    :quote_patina => { :fg => "yellow", :bg => "default" },
+    :sig_patina => { :fg => "yellow", :bg => "default" },
+    :quote => { :fg => "yellow", :bg => "default" },
+    :sig => { :fg => "yellow", :bg => "default" },
+    :to_me => { :fg => "green", :bg => "default" },
+    :starred => { :fg => "yellow", :bg => "default", :attrs => ["bold"] },
+    :starred_patina => { :fg => "yellow", :bg => "green", :attrs => ["bold"] },
+    :alternate_starred_patina => { :fg => "yellow", :bg => "blue", :attrs => ["bold"] },
+    :snippet => { :fg => "cyan", :bg => "default" },
+    :option => { :fg => "white", :bg => "default" },
+    :tagged => { :fg => "yellow", :bg => "default", :attrs => ["bold"] },
+    :draft_notification => { :fg => "red", :bg => "default", :attrs => ["bold"] },
+    :completion_character => { :fg => "white", :bg => "default", :attrs => ["bold"] },
+    :horizontal_selector_selected => { :fg => "yellow", :bg => "default", :attrs => ["bold"] },
+    :horizontal_selector_unselected => { :fg => "cyan", :bg => "default" },
+    :search_highlight => { :fg => "black", :bg => "yellow", :attrs => ["bold"] }
+  }
   
   def initialize
     raise "only one instance can be created" if @@instance
@@ -24,14 +62,14 @@ class Colormap
                                                    []) + [nil]
   end
 
-  def add sym, fg, bg, *attrs
-    raise ArgumentError, "color for #{sym} already defined" if
-      @entries.member? sym
+  def add sym, fg, bg, attr=nil, opts={}
+    raise ArgumentError, "color for #{sym} already defined" if @entries.member? sym
     raise ArgumentError, "color '#{fg}' unknown" unless CURSES_COLORS.include? fg
     raise ArgumentError, "color '#{bg}' unknown" unless CURSES_COLORS.include? bg
+    attrs = [attr].flatten.compact
 
     @entries[sym] = [fg, bg, attrs, nil]
-    @entries[highlight_sym(sym)] = highlight_for(fg, bg, attrs) + [nil]
+    @entries[highlight_sym(sym)] = opts[:highlight] ? @entries[opts[:highlight]] : highlight_for(fg, bg, attrs) + [nil]
   end
 
   def highlight_sym sym
@@ -53,6 +91,8 @@ class Colormap
       case bg
       when Curses::COLOR_CYAN
         Curses::COLOR_YELLOW
+      when Curses::COLOR_YELLOW
+        Curses::COLOR_BLUE
       else
         Curses::COLOR_CYAN
       end
@@ -108,9 +148,61 @@ class Colormap
     color
   end
 
+  ## Try to use the user defined colors, in case of an error fall back
+  ## to the default ones.
+  def populate_colormap
+    user_colors = if File.exists? Redwood::COLOR_FN
+      Redwood::log "loading user colors from #{Redwood::COLOR_FN}"
+      Redwood::load_yaml_obj Redwood::COLOR_FN
+    end
+
+    error = nil
+    Colormap::DEFAULT_COLORS.each_pair do |k, v|
+      fg = Curses.const_get "COLOR_#{v[:fg].upcase}"
+      bg = Curses.const_get "COLOR_#{v[:bg].upcase}"
+      attrs = v[:attrs] ? v[:attrs].map { |a| Curses.const_get "A_#{a.upcase}" } : []
+
+      if user_colors && (ucolor = user_colors[k])
+        if(ufg = ucolor[:fg])
+          begin
+            fg = Curses.const_get "COLOR_#{ufg.upcase}"
+          rescue NameError
+            error ||= "Warning: there is no color named \"#{ufg}\", using fallback."
+            Redwood::log "Warning: there is no color named \"#{ufg}\""
+          end
+        end
+
+        if(ubg = ucolor[:bg])
+          begin
+            bg = Curses.const_get "COLOR_#{ubg.upcase}"
+          rescue NameError
+            error ||= "Warning: there is no color named \"#{ubg}\", using fallback."
+            Redwood::log "Warning: there is no color named \"#{ubg}\""
+          end
+        end
+
+        if(uattrs = ucolor[:attrs])
+          attrs = [*uattrs].flatten.map do |a|
+            begin
+              Curses.const_get "A_#{a.upcase}"
+            rescue NameError
+              error ||= "Warning: there is no attribute named \"#{a}\", using fallback."
+              Redwood::log "Warning: there is no attribute named \"#{a}\", using fallback."
+            end
+          end
+        end
+      end
+
+      symbol = (k.to_s + "_color").to_sym
+      add symbol, fg, bg, attrs
+    end
+
+    BufferManager.flash error if error
+  end
+
   def self.instance; @@instance; end
   def self.method_missing meth, *a
-    Colorcolors.new unless @@instance
+    Colormap.new unless @@instance
     @@instance.send meth, *a
   end
 end