]> git.cworth.org Git - sup/commitdiff
various color tweaks
authorWilliam Morgan <wmorgan-sup@masanjin.net>
Wed, 4 Jun 2008 02:11:34 +0000 (19:11 -0700)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Wed, 4 Jun 2008 02:21:22 +0000 (19:21 -0700)
- fix NME when there's no colors.yaml
- remove top-level "colors:" attribute of colors.yaml
- allow single attributes to be non-arrays
- flash at most one error
- other minor rejiggering

bin/sup
lib/sup/colormap.rb

diff --git a/bin/sup b/bin/sup
index a814b1ca67544dadae8fabed3e5d7f7d49c54c7c..99df4b1602c135c5c95e740d1332355f18630a04 100644 (file)
--- a/bin/sup
+++ b/bin/sup
@@ -79,7 +79,6 @@ def start_cursing
   Ncurses.stdscr.keypad 1
   Ncurses.curs_set 0
   Ncurses.start_color
-  Ncurses.use_default_colors
   $cursing = true
 end
 
@@ -142,7 +141,7 @@ begin
   start_cursing
 
   bm = BufferManager.new
-  Colormap.new
+  Colormap.new.populate_colormap
 
   log "initializing mail index buffer"
   imode = InboxMode.new
index 8129bcfa7d4dcbadf32e913a903bd90ba2626335..296692b8d9efbac3ba628fab4fa9cde2d832c26e 100644 (file)
@@ -60,7 +60,6 @@ class Colormap
     @entries[highlight_sym(:none)] = highlight_for(Curses::COLOR_WHITE,
                                                    Curses::COLOR_BLACK,
                                                    []) + [nil]
-    populate_colormap
   end
 
   def add sym, fg, bg, attr=nil, opts={}
@@ -152,43 +151,58 @@ class Colormap
   ## Try to use the user defined colors, in case of an error fall back
   ## to the default ones.
   def populate_colormap
-    if File.exists? Redwood::COLOR_FN
-      user_colors = Redwood::load_yaml_obj Redwood::COLOR_FN
+    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
 
-    errors = []
-
+    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].map { |a| Curses.const_get "A_#{a.upcase}" } rescue attrs
-
-      if(ucolor = user_colors[:colors][k])
-        begin
-          fg = Curses.const_get "COLOR_#{ucolor[:fg].upcase}"
-        rescue NameError
-          errors << "Warning: There is no color named \"#{ucolor[:fg]}\", using fallback."
-          Redwood::log "Warning: There is no color named \"#{ucolor[:fg]}\""
+      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
-        begin
-          bg = Curses.const_get "COLOR_#{ucolor[:bg].upcase}"
-        rescue NameError
-          errors << "Warning: There is no color named \"#{ucolor[:bg]}\", using fallback."
-          Redwood::log "Warning: There is no color named \"#{ucolor[:bg]}\""
+
+        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
-        attrs = ucolor[:attrs].map {|a| Curses.const_get "A_#{a.upcase}" } rescue attrs
       end
 
       symbol = (k.to_s + "_color").to_sym
       add symbol, fg, bg, attrs
     end
 
-    errors.each { |e| BufferManager.flash e }
+    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