commit 911fa1656b45530692e6702a5ac1c25f0a5726a2
parent d02eb8c89c62ab0a95d44676bdd595251a4fe774
Author: Rich Lane <rlane@club.cc.cmu.edu>
Date: Fri, 26 Feb 2010 22:57:35 -0800
Merge commit 'mainline/colors'
Diffstat:
3 files changed, 143 insertions(+), 13 deletions(-)
diff --git a/bin/sup b/bin/sup
@@ -83,6 +83,11 @@ global_keymap = Keymap.new do |k|
k.add :recall_draft, "Edit most recent draft message", 'R'
k.add :show_inbox, "Show the Inbox buffer", 'I'
k.add :show_console, "Show the Console buffer", '~'
+
+ ## Submap for less often used keybindings
+ k.add_multi "reload (c)olors", 'O' do |kk|
+ kk.add :reload_colors, "Reload colors", 'c'
+ end
end
## the following magic enables wide characters when used with a ruby
@@ -339,6 +344,11 @@ begin
when :show_console
b, new = bm.spawn_unless_exists("Console", :system => true) { ConsoleMode.new }
b.mode.run
+ when :reload_colors
+ Colormap.reset
+ Colormap.populate_colormap
+ bm.completely_redraw_screen
+ bm.flash "reloaded colors"
when :nothing, InputSequenceAborted
when :redraw
bm.completely_redraw_screen
diff --git a/contrib/colorpicker.rb b/contrib/colorpicker.rb
@@ -0,0 +1,104 @@
+require 'rubygems'
+
+begin
+ require 'ncursesw'
+rescue LoadError
+ require 'ncurses'
+end
+
+Ncurses.initscr
+Ncurses.noecho
+Ncurses.cbreak
+Ncurses.start_color
+
+Ncurses.curs_set 0
+Ncurses.move 0, 0
+Ncurses.clear
+Ncurses.refresh
+cc = Ncurses.COLORS
+
+Ncurses::keypad(Ncurses::stdscr, 1)
+Ncurses::mousemask(Ncurses::ALL_MOUSE_EVENTS | Ncurses::REPORT_MOUSE_POSITION, [])
+
+fail "color count is #{cc}, expected 256" unless cc == 256
+
+1.upto(255) do |c|
+ Ncurses.init_pair(c, 0, c)
+end
+
+def cell y, x, c
+ @map[[y,x]] = c
+ Ncurses.attron(Ncurses.COLOR_PAIR(c))
+ Ncurses.mvaddstr(y, x, " ")
+ Ncurses.attroff(Ncurses.COLOR_PAIR(c))
+end
+
+def handle_click y, x
+ c = @map[[y,x]] or return
+ name = case c
+ when 0...16
+ c.to_s
+ when 16...232
+ 'c' + (c-16).to_s(6).rjust(3,'0')
+ when 232...256
+ 'g' + (c-232).to_s
+ end
+
+ Ncurses.mvaddstr 11, 0, "#{name} "
+
+ Ncurses.attron(Ncurses.COLOR_PAIR(c))
+ 10.times do |i|
+ 20.times do |j|
+ y = 13 + i
+ x = j
+ Ncurses.mvaddstr(y, x, " ")
+ end
+ end
+ Ncurses.attroff(Ncurses.COLOR_PAIR(c))
+end
+
+@map = {}
+@fg = @bg = 0
+
+begin
+ 16.times do |i|
+ cell 0, i, i
+ end
+
+ 6.times do |i|
+ 6.times do |j|
+ 6.times do |k|
+ c = 16 + 6*6*i + 6*j + k
+ y = 2 + j
+ x = 7*i + k
+ cell y, x, c
+ end
+ end
+ end
+
+ 16.times do |i|
+ c = 16 + 6*6*6 + i
+ cell 9, i, c
+ end
+
+ handle_click 0, 0
+ Ncurses.refresh
+
+ while (c = Ncurses.getch)
+ case c
+ when 113 #q
+ break
+ when Ncurses::KEY_MOUSE
+ mev = Ncurses::MEVENT.new
+ Ncurses.getmouse(mev)
+ case(mev.bstate)
+ when Ncurses::BUTTON1_CLICKED
+ handle_click mev.y, mev.x
+ end
+ end
+ Ncurses.refresh
+ end
+
+ensure
+ Ncurses.endwin
+end
diff --git a/lib/sup/colormap.rb b/lib/sup/colormap.rb
@@ -1,5 +1,23 @@
module Curses
COLOR_DEFAULT = -1
+
+ NUM_COLORS = `tput colors`.to_i
+ MAX_PAIRS = `tput pairs`.to_i
+
+ def self.color! name, value
+ const_set "COLOR_#{name.to_s.upcase}", value
+ end
+
+ ## numeric colors
+ Curses::NUM_COLORS.times { |x| color! x, x }
+
+ if Curses::NUM_COLORS == 256
+ ## xterm 6x6x6 color cube
+ 6.times { |x| 6.times { |y| 6.times { |z| color! "c#{x}#{y}#{z}", 16 + z + 6*y + 36*x } } }
+
+ ## xterm 24-shade grayscale
+ 24.times { |x| color! "g#{x}", (16+6*6*6) + x }
+ end
end
module Redwood
@@ -7,12 +25,6 @@ module Redwood
class Colormap
@@instance = nil
- 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_DEFAULT]
- NUM_COLORS = (CURSES_COLORS.size - 1) * (CURSES_COLORS.size - 1)
-
DEFAULT_COLORS = {
:status => { :fg => "white", :bg => "blue", :attrs => ["bold"] },
:index_old => { :fg => "white", :bg => "default" },
@@ -56,11 +68,15 @@ class Colormap
def initialize
raise "only one instance can be created" if @@instance
@@instance = self
- @entries = {}
@color_pairs = {[Curses::COLOR_WHITE, Curses::COLOR_BLACK] => 0}
@users = []
@next_id = 0
+ reset
yield self if block_given?
+ end
+
+ def reset
+ @entries = {}
@entries[highlight_sym(:none)] = highlight_for(Curses::COLOR_WHITE,
Curses::COLOR_BLACK,
[]) + [nil]
@@ -68,8 +84,8 @@ class Colormap
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
+ raise ArgumentError, "color '#{fg}' unknown" unless (-1...Curses::NUM_COLORS).include? fg
+ raise ArgumentError, "color '#{bg}' unknown" unless (-1...Curses::NUM_COLORS).include? bg
attrs = [attr].flatten.compact
@entries[sym] = [fg, bg, attrs, nil]
@@ -127,7 +143,7 @@ class Colormap
if(cp = @color_pairs[[fg, bg]])
## nothing
else ## need to get a new colorpair
- @next_id = (@next_id + 1) % NUM_COLORS
+ @next_id = (@next_id + 1) % Curses::MAX_PAIRS
@next_id += 1 if @next_id == 0 # 0 is always white on black
id = @next_id
debug "colormap: for color #{sym}, using id #{id} -> #{fg}, #{bg}"
@@ -169,7 +185,7 @@ class Colormap
if user_colors && (ucolor = user_colors[k])
if(ufg = ucolor[:fg])
begin
- fg = Curses.const_get "COLOR_#{ufg.upcase}"
+ fg = Curses.const_get "COLOR_#{ufg.to_s.upcase}"
rescue NameError
error ||= "Warning: there is no color named \"#{ufg}\", using fallback."
warn "there is no color named \"#{ufg}\""
@@ -178,7 +194,7 @@ class Colormap
if(ubg = ucolor[:bg])
begin
- bg = Curses.const_get "COLOR_#{ubg.upcase}"
+ bg = Curses.const_get "COLOR_#{ubg.to_s.upcase}"
rescue NameError
error ||= "Warning: there is no color named \"#{ubg}\", using fallback."
warn "there is no color named \"#{ubg}\""
@@ -201,7 +217,7 @@ class Colormap
add symbol, fg, bg, attrs
end
- BufferManager.flash error if error
+ warn error if error
end
def self.instance; @@instance; end