Class: RawLine::KeyBindings
- Inherits:
-
Object
- Object
- RawLine::KeyBindings
- Defined in:
- lib/rawline/key_bindings.rb
Class Attribute Summary collapse
-
.default_terminal ⇒ Object
Returns the value of attribute default_terminal.
Instance Attribute Summary collapse
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
Instance Method Summary collapse
- #[](char) ⇒ Object
-
#bind(key, &block) ⇒ Object
Bind a key to an action specified via
block. - #bound?(char) ⇒ Boolean
-
#initialize(terminal: self.class.default_terminal) ⇒ KeyBindings
constructor
A new instance of KeyBindings.
- #unbind(key) ⇒ Object
Constructor Details
#initialize(terminal: self.class.default_terminal) ⇒ KeyBindings
Returns a new instance of KeyBindings.
9 10 11 12 |
# File 'lib/rawline/key_bindings.rb', line 9 def initialize(terminal: self.class.default_terminal) @terminal = terminal @keys = {} end |
Class Attribute Details
.default_terminal ⇒ Object
Returns the value of attribute default_terminal.
6 7 8 |
# File 'lib/rawline/key_bindings.rb', line 6 def default_terminal @default_terminal end |
Instance Attribute Details
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
3 4 5 |
# File 'lib/rawline/key_bindings.rb', line 3 def keys @keys end |
Instance Method Details
#[](char) ⇒ Object
14 15 16 |
# File 'lib/rawline/key_bindings.rb', line 14 def [](char) keys[char] end |
#bind(key, &block) ⇒ Object
Bind a key to an action specified via block. key can be:
-
A Symbol identifying a character or character sequence defined for the current terminal
-
A Fixnum identifying a character defined for the current terminal
-
An Array identifying a character or character sequence defined for the current terminal
-
A String identifying a character or character sequence, even if it is not defined for the current terminal
-
An Hash identifying a character or character sequence, even if it is not defined for the current terminal
If key is a hash, then:
-
It must contain only one key/value pair
-
The key identifies the name of the character or character sequence
-
The value identifies the code(s) corresponding to the character or character sequence
-
The value can be a Fixnum, a String or an Array.
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/rawline/key_bindings.rb', line 35 def bind(key, &block) case key.class.to_s when 'Symbol' then raise BindingException, "Unknown key or key sequence '#{key.to_s}' (#{key.class.to_s})" unless @terminal.keys[key] keys[@terminal.keys[key]] = block when 'Array' then raise BindingException, "Unknown key or key sequence '#{key.join(", ")}' (#{key.class.to_s})" unless @terminal.keys.has_value? key keys[key] = block when 'Fixnum' then raise BindingException, "Unknown key or key sequence '#{key.to_s}' (#{key.class.to_s})" unless @terminal.keys.has_value? [key] keys[[key]] = block when 'String' then if key.length == 1 then keys[[key.ord]] = block else bind_hash({:"#{key}" => key}, block) end when 'Hash' then raise BindingException, "Cannot bind more than one key or key sequence at once" unless key.values.length == 1 bind_hash(key, block) else raise BindingException, "Unable to bind '#{key.to_s}' (#{key.class.to_s})" end @terminal.update end |
#bound?(char) ⇒ Boolean
61 62 63 |
# File 'lib/rawline/key_bindings.rb', line 61 def bound?(char) keys[char] ? true : false end |
#unbind(key) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/rawline/key_bindings.rb', line 65 def unbind(key) block = case key.class.to_s when 'Symbol' then keys.delete @terminal.keys[key] when 'Array' then keys.delete keys[key] when 'Fixnum' then keys.delete[[key]] when 'String' then if key.length == 1 then keys.delete([key.ord]) else raise NotImplementedError, "This is no implemented yet. It needs to return the previously bound block" bind_hash({:"#{key}" => key}, block) end when 'Hash' then raise BindingException, "Cannot bind more than one key or key sequence at once" unless key.values.length == 1 bind_hash(key, -> { }) end @terminal.update block end |