]> git.cworth.org Git - sup/blobdiff - lib/sup/keymap.rb
add self as recipient on crypted sent messages
[sup] / lib / sup / keymap.rb
index 64935d9702ed1611e0ec0a0727802c1effa80b96..76c7139f45eb9478a627e49ac7c87eea81d0ee5d 100644 (file)
@@ -1,5 +1,3 @@
-require "curses"
-
 module Redwood
 
 class Keymap
@@ -9,7 +7,7 @@ class Keymap
     yield self if block_given?
   end
 
-  def keysym_to_keycode k
+  def self.keysym_to_keycode k
     case k
     when :down: Curses::KEY_DOWN
     when :up: Curses::KEY_UP
@@ -33,7 +31,7 @@ class Keymap
     end
   end
 
-  def keysym_to_string k
+  def self.keysym_to_string k
     case k
     when :down: "<down arrow>"
     when :up: "<up arrow>"
@@ -45,16 +43,10 @@ class Keymap
     when :home: "<home>"
     when :end: "<end>"
     when :enter, :return: "<enter>"
-    when :ctrl_l: "ctrl-l"
-    when :ctrl_l: "ctrl-g"
     when :tab: "tab"
     when " ": "<space>"
     else
-      if k.is_a?(String) && k.length == 1
-        k
-      else
-        raise ArgumentError, "unknown key name \"#{k}\""
-      end
+      Curses::keyname(keysym_to_keycode(k))
     end
   end
 
@@ -62,26 +54,45 @@ class Keymap
     entry = [action, help, keys]
     @order << entry
     keys.each do |k|
-      raise ArgumentError, "key #{k} already defined (action #{action})" if @map.include? k
-      kc = keysym_to_keycode k
+      kc = Keymap.keysym_to_keycode k
+      raise ArgumentError, "key '#{k}' already defined (as #{@map[kc].first})" if @map.include? kc
       @map[kc] = entry
     end
   end
 
+  def add_multi prompt, key
+    submap = Keymap.new
+    add submap, prompt, key
+    yield submap
+  end
+
   def action_for kc
     action, help, keys = @map[kc]
-    action
+    [action, help]
   end
 
+  def has_key? k; @map[k] end
+
   def keysyms; @map.values.map { |action, help, keys| keys }.flatten; end
 
-  def help_text except_for={}
-    lines = @order.map do |action, help, keys|
+  def help_lines except_for={}, prefix=""
+    lines = [] # :(
+    @order.each do |action, help, keys|
       valid_keys = keys.select { |k| !except_for[k] }
       next if valid_keys.empty?
-      [valid_keys.map { |k| keysym_to_string k }.join(", "), help]
+      case action
+      when Symbol
+        lines << [valid_keys.map { |k| prefix + Keymap.keysym_to_string(k) }.join(", "), help]
+      when Keymap
+        lines += action.help_lines({}, prefix + Keymap.keysym_to_string(keys.first))
+      end
     end.compact
-    llen = lines.map { |a, b| a.length }.max
+    lines
+  end
+
+  def help_text except_for={}
+    lines = help_lines except_for
+    llen = lines.max_of { |a, b| a.length }
     lines.map { |a, b| sprintf " %#{llen}s : %s", a, b }.join("\n")
   end
 end