]> git.cworth.org Git - sup/commitdiff
sup color customization
authorLionel Ott <white.magic@gmx.de>
Sun, 25 May 2008 06:59:49 +0000 (08:59 +0200)
committerRubyist <rubyist@south.(none)>
Tue, 3 Jun 2008 22:26:03 +0000 (15:26 -0700)
- config file is located under $SUP_HOME/config.yaml and has the following
  structure

  :colors:
    :symbol_name:
      :fg: <color>
      :bg: <color>
      :attrs:
      - <attribute>

  <color> and <attribute> can take the standard values available in the curses
  environment.
  There may be multiple attributes, but they need not be present.
- if there is an error in the user provided config file a default value will
  be used (stored in the Colormap class)

bin/sup
lib/sup.rb
lib/sup/colormap.rb

diff --git a/bin/sup b/bin/sup
index 723b1ed1e850b5eebadaf5804484793cc0a07b3f..a814b1ca67544dadae8fabed3e5d7f7d49c54c7c 100644 (file)
--- a/bin/sup
+++ b/bin/sup
@@ -79,6 +79,7 @@ def start_cursing
   Ncurses.stdscr.keypad 1
   Ncurses.curs_set 0
   Ncurses.start_color
+  Ncurses.use_default_colors
   $cursing = true
 end
 
@@ -140,53 +141,8 @@ begin
   log "starting curses"
   start_cursing
 
-  Colormap.new do |c|
-    c.add :status_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLUE, Ncurses::A_BOLD
-    c.add :index_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
-    c.add :index_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
-           Ncurses::A_BOLD
-    c.add :index_starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK, 
-           Ncurses::A_BOLD
-    c.add :index_draft_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
-           Ncurses::A_BOLD
-    c.add :labellist_old_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
-    c.add :labellist_new_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK, 
-           Ncurses::A_BOLD
-    c.add :twiddle_color, Ncurses::COLOR_BLUE, Ncurses::COLOR_BLACK
-    c.add :label_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
-    c.add :message_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_GREEN
-    c.add :alternate_patina_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_BLUE
-    c.add :missing_message_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_RED
-    c.add :attachment_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
-    c.add :cryptosig_valid_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK, Ncurses::A_BOLD
-    c.add :cryptosig_unknown_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
-    c.add :cryptosig_invalid_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_RED, Ncurses::A_BOLD
-    c.add :generic_notice_patina_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
-    c.add :quote_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
-    c.add :sig_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
-    c.add :quote_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
-    c.add :sig_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK
-    c.add :to_me_color, Ncurses::COLOR_GREEN, Ncurses::COLOR_BLACK
-    c.add :starred_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
-          Ncurses::A_BOLD
-    c.add :starred_patina_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_GREEN,
-          Ncurses::A_BOLD
-    c.add :alternate_starred_patina_color, Ncurses::COLOR_YELLOW,
-          Ncurses::COLOR_BLUE, Ncurses::A_BOLD
-    c.add :snippet_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
-    c.add :option_color, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLACK
-    c.add :tagged_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK,
-          Ncurses::A_BOLD
-    c.add :draft_notification_color, Ncurses::COLOR_RED, Ncurses::COLOR_BLACK,
-          Ncurses::A_BOLD
-    c.add :completion_character_color, Ncurses::COLOR_WHITE,
-          Ncurses::COLOR_BLACK, Ncurses::A_BOLD
-    c.add :horizontal_selector_selected_color, Ncurses::COLOR_YELLOW, Ncurses::COLOR_BLACK, Ncurses::A_BOLD
-    c.add :horizontal_selector_unselected_color, Ncurses::COLOR_CYAN, Ncurses::COLOR_BLACK
-    c.add :search_highlight_color, Ncurses::COLOR_BLACK, Ncurses::COLOR_YELLOW, Ncurses::A_BOLD, :highlight => :search_highlight_color
-  end
-
   bm = BufferManager.new
+  Colormap.new
 
   log "initializing mail index buffer"
   imode = InboxMode.new
index 9e9026747bca29324fe44cf4b4df12c1d8e54ccc..9a4d72da68de59d9d2ccd76957f837f214b99122 100644 (file)
@@ -37,6 +37,7 @@ module Redwood
 
   BASE_DIR   = ENV["SUP_BASE"] || File.join(ENV["HOME"], ".sup")
   CONFIG_FN  = File.join(BASE_DIR, "config.yaml")
+  COLOR_FN   = File.join(BASE_DIR, "colors.yaml")
   SOURCE_FN  = File.join(BASE_DIR, "sources.yaml")
   LABEL_FN   = File.join(BASE_DIR, "labels.txt")
   PERSON_FN  = File.join(BASE_DIR, "people.txt")
index 9c6869a8744989f91f89e7e26eddedd49edfce98..8129bcfa7d4dcbadf32e913a903bd90ba2626335 100644 (file)
@@ -1,3 +1,7 @@
+module Curses
+  COLOR_DEFAULT = -1
+end
+
 module Redwood
 
 class Colormap
@@ -6,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 => "black" },
+    :index_new => { :fg => "white", :bg => "black", :attrs => ["bold"] },
+    :index_starred => { :fg => "yellow", :bg => "black", :attrs => ["bold"] },
+    :index_draft => { :fg => "red", :bg => "black", :attrs => ["bold"] },
+    :labellist_old => { :fg => "white", :bg => "black" },
+    :labellist_new => { :fg => "white", :bg => "black", :attrs => ["bold"] },
+    :twiddle => { :fg => "blue", :bg => "black" },
+    :label => { :fg => "yellow", :bg => "black" },
+    :message_patina => { :fg => "black", :bg => "green" },
+    :alternate_patina => { :fg => "black", :bg => "blue" },
+    :missing_message => { :fg => "black", :bg => "red" },
+    :attachment => { :fg => "cyan", :bg => "black" },
+    :cryptosig_valid => { :fg => "yellow", :bg => "black", :attrs => ["bold"] },
+    :cryptosig_unknown => { :fg => "cyan", :bg => "black" },
+    :cryptosig_invalid => { :fg => "yellow", :bg => "red", :attrs => ["bold"] },
+    :generic_notice_patina => { :fg => "cyan", :bg => "black" },
+    :quote_patina => { :fg => "yellow", :bg => "black" },
+    :sig_patina => { :fg => "yellow", :bg => "black" },
+    :quote => { :fg => "yellow", :bg => "black" },
+    :sig => { :fg => "yellow", :bg => "black" },
+    :to_me => { :fg => "green", :bg => "black" },
+    :starred => { :fg => "yellow", :bg => "black", :attrs => ["bold"] },
+    :starred_patina => { :fg => "yellow", :bg => "green", :attrs => ["bold"] },
+    :alternate_starred_patina => { :fg => "yellow", :bg => "blue", :attrs => ["bold"] },
+    :snippet => { :fg => "cyan", :bg => "black" },
+    :option => { :fg => "white", :bg => "black" },
+    :tagged => { :fg => "red", :bg => "black", :attrs => ["bold"] },
+    :draft_notification => { :fg => "red", :bg => "black", :attrs => ["bold"] },
+    :completion_character => { :fg => "white", :bg => "black", :attrs => ["bold"] },
+    :horizontal_selector_selected => { :fg => "yellow", :bg => "black", :attrs => ["bold"] },
+    :horizontal_selector_unselected => { :fg => "cyan", :bg => "black" },
+    :search_highlight => { :fg => "black", :bg => "yellow", :attrs => ["bold"] }
+  }
   
   def initialize
     raise "only one instance can be created" if @@instance
@@ -20,6 +60,7 @@ 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={}
@@ -108,6 +149,43 @@ 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
+    if File.exists? Redwood::COLOR_FN
+      user_colors = Redwood::load_yaml_obj Redwood::COLOR_FN
+    end
+
+    errors = []
+
+    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]}\""
+        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]}\""
+        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 }
+  end
+
   def self.instance; @@instance; end
   def self.method_missing meth, *a
     Colorcolors.new unless @@instance