Class: Textbringer::Controller
- Inherits:
-
Object
- Object
- Textbringer::Controller
- Defined in:
- lib/textbringer/controller.rb
Constant Summary collapse
- @@current =
nil
Instance Attribute Summary collapse
-
#current_prefix_arg ⇒ Object
Returns the value of attribute current_prefix_arg.
-
#key_sequence ⇒ Object
readonly
Returns the value of attribute key_sequence.
-
#last_command ⇒ Object
Returns the value of attribute last_command.
-
#last_key ⇒ Object
readonly
Returns the value of attribute last_key.
-
#overriding_map ⇒ Object
Returns the value of attribute overriding_map.
-
#prefix_arg ⇒ Object
Returns the value of attribute prefix_arg.
-
#recursive_edit_level ⇒ Object
readonly
Returns the value of attribute recursive_edit_level.
-
#this_command ⇒ Object
Returns the value of attribute this_command.
Class Method Summary collapse
Instance Method Summary collapse
- #command_loop(tag) ⇒ Object
- #echo_input ⇒ Object
-
#initialize ⇒ Controller
constructor
A new instance of Controller.
- #key_name(key) ⇒ Object
- #read_char ⇒ Object
- #read_char_nonblock ⇒ Object
- #received_keyboard_quit? ⇒ Boolean
- #recursive_edit ⇒ Object
- #wait_input(msecs) ⇒ Object
Constructor Details
#initialize ⇒ Controller
Returns a new instance of Controller.
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/textbringer/controller.rb', line 22 def initialize @key_sequence = [] @last_key = nil @recursive_edit_level = 0 @this_command = nil @last_command = nil @overriding_map = nil @prefix_arg = nil @current_prefix_arg = nil @echo_immediately = false end |
Instance Attribute Details
#current_prefix_arg ⇒ Object
Returns the value of attribute current_prefix_arg.
9 10 11 |
# File 'lib/textbringer/controller.rb', line 9 def current_prefix_arg @current_prefix_arg end |
#key_sequence ⇒ Object (readonly)
Returns the value of attribute key_sequence.
10 11 12 |
# File 'lib/textbringer/controller.rb', line 10 def key_sequence @key_sequence end |
#last_command ⇒ Object
Returns the value of attribute last_command.
8 9 10 |
# File 'lib/textbringer/controller.rb', line 8 def last_command @last_command end |
#last_key ⇒ Object (readonly)
Returns the value of attribute last_key.
10 11 12 |
# File 'lib/textbringer/controller.rb', line 10 def last_key @last_key end |
#overriding_map ⇒ Object
Returns the value of attribute overriding_map.
8 9 10 |
# File 'lib/textbringer/controller.rb', line 8 def overriding_map @overriding_map end |
#prefix_arg ⇒ Object
Returns the value of attribute prefix_arg.
9 10 11 |
# File 'lib/textbringer/controller.rb', line 9 def prefix_arg @prefix_arg end |
#recursive_edit_level ⇒ Object (readonly)
Returns the value of attribute recursive_edit_level.
10 11 12 |
# File 'lib/textbringer/controller.rb', line 10 def recursive_edit_level @recursive_edit_level end |
#this_command ⇒ Object
Returns the value of attribute this_command.
8 9 10 |
# File 'lib/textbringer/controller.rb', line 8 def this_command @this_command end |
Class Method Details
.current ⇒ Object
14 15 16 |
# File 'lib/textbringer/controller.rb', line 14 def self.current @@current end |
.current=(controller) ⇒ Object
18 19 20 |
# File 'lib/textbringer/controller.rb', line 18 def self.current=(controller) @@current = controller end |
Instance Method Details
#command_loop(tag) ⇒ Object
34 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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/textbringer/controller.rb', line 34 def command_loop(tag) catch(tag) do loop do begin echo_input c = read_char break if c.nil? Window.echo_area. @last_key = c @key_sequence << @last_key cmd = key_binding(@key_sequence) if cmd.is_a?(Symbol) || cmd.respond_to?(:call) @key_sequence.clear @this_command = cmd @current_prefix_arg = @prefix_arg @prefix_arg = nil begin run_hooks(:pre_command_hook, remove_on_error: true) if cmd.is_a?(Symbol) send(cmd) else cmd.call end ensure run_hooks(:post_command_hook, remove_on_error: true) @last_command = @this_command @this_command = nil end else if cmd.nil? keys = @key_sequence.map { |ch| key_name(ch) }.join(" ") @key_sequence.clear @prefix_arg = nil ("#{keys} is undefined") end end rescue Exception => e show_exception(e) @prefix_arg = nil end Window.redisplay end end end |
#echo_input ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'lib/textbringer/controller.rb', line 126 def echo_input if @prefix_arg || !@key_sequence.empty? if !@echo_immediately return if wait_input(1000) end @echo_immediately = true s = String.new if @prefix_arg s << "C-u" if @prefix_arg != [4] s << "(#{@prefix_arg.inspect})" end end if !@key_sequence.empty? s << " " if !s.empty? s << @key_sequence.map { |ch| key_name(ch) }.join(" ") end s << "-" Window.echo_area.show(s) Window.echo_area.redisplay Window.current.window.noutrefresh Window.update else @echo_immediately = false end end |
#key_name(key) ⇒ Object
111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/textbringer/controller.rb', line 111 def key_name(key) case key when Symbol "<#{key}>" when "\e" "ESC" when "\C-m" "RET" when /\A[\0-\b\v-\x1f\x7f]\z/ "C-" + (key.ord ^ 0x40).chr.downcase else key.to_s end end |
#read_char ⇒ Object
83 84 85 |
# File 'lib/textbringer/controller.rb', line 83 def read_char Window.current.read_char end |
#read_char_nonblock ⇒ Object
87 88 89 |
# File 'lib/textbringer/controller.rb', line 87 def read_char_nonblock Window.current.read_char_nonblock end |
#received_keyboard_quit? ⇒ Boolean
91 92 93 94 95 96 97 98 |
# File 'lib/textbringer/controller.rb', line 91 def received_keyboard_quit? while key = read_char_nonblock if GLOBAL_MAP.lookup([key]) == :keyboard_quit return true end end false end |
#recursive_edit ⇒ Object
100 101 102 103 104 105 106 107 108 109 |
# File 'lib/textbringer/controller.rb', line 100 def recursive_edit @recursive_edit_level += 1 begin if command_loop(RECURSIVE_EDIT_TAG) raise Quit end ensure @recursive_edit_level -= 1 end end |
#wait_input(msecs) ⇒ Object
79 80 81 |
# File 'lib/textbringer/controller.rb', line 79 def wait_input(msecs) Window.current.wait_input(msecs) end |