Method: Textbringer::Controller#command_loop

Defined in:
lib/textbringer/controller.rb

#command_loop(tag) ⇒ Object



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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/textbringer/controller.rb', line 49

def command_loop(tag)
  catch(tag) do
    loop do
      begin
        echo_input
        c = read_event
        break if c.nil?
        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)
          @this_command_keys = @key_sequence
          @key_sequence = []
          @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)
              @top_self.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 = Keymap.key_sequence_string(@key_sequence)
            @key_sequence.clear
            @prefix_arg = nil
            message("#{keys} is undefined")
          end
        end
        Window.redisplay
      rescue Exception => e
        show_exception(e)
        @prefix_arg = nil
        @recording_keyboard_macro = nil
        Window.redisplay
        if Window.echo_area.active?
          wait_input(2000)
          Window.echo_area.clear_message
          Window.redisplay
        end
      end
    end
  end
end