Class: Textbringer::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/textbringer/controller.rb

Constant Summary collapse

@@current =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeController

Returns a new instance of Controller.



22
23
24
25
26
27
28
29
30
31
# 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
end

Instance Attribute Details

#current_prefix_argObject

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

#last_commandObject

Returns the value of attribute last_command.



8
9
10
# File 'lib/textbringer/controller.rb', line 8

def last_command
  @last_command
end

#last_keyObject (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_mapObject

Returns the value of attribute overriding_map.



8
9
10
# File 'lib/textbringer/controller.rb', line 8

def overriding_map
  @overriding_map
end

#prefix_argObject

Returns the value of attribute prefix_arg.



9
10
11
# File 'lib/textbringer/controller.rb', line 9

def prefix_arg
  @prefix_arg
end

#this_commandObject

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

.currentObject



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



33
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
# File 'lib/textbringer/controller.rb', line 33

def command_loop(tag)
  catch(tag) do
    loop do
      begin
        c = Window.current.read_char
        Window.echo_area.clear_message
        @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 { |c| key_name(c) }.join(" ")
            @key_sequence.clear
            Window.echo_area.show("#{keys} is undefined")
          end
        end
      rescue Exception => e
        handle_exception(e)
      end
      Window.redisplay
    end
  end
end

#read_charObject



78
79
80
# File 'lib/textbringer/controller.rb', line 78

def read_char
  Window.current.read_char
end

#received_keyboard_quit?Boolean

Returns:

  • (Boolean)


82
83
84
85
86
87
88
89
# File 'lib/textbringer/controller.rb', line 82

def received_keyboard_quit?
  while key = Window.current.read_char_nonblock
    if GLOBAL_MAP.lookup([key]) == :keyboard_quit
      return true
    end
  end
  false
end

#recursive_editObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/textbringer/controller.rb', line 91

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



74
75
76
# File 'lib/textbringer/controller.rb', line 74

def wait_input(msecs)
  Window.current.wait_input(msecs)
end