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
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_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

#key_sequenceObject (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_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

#recursive_edit_levelObject (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_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



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.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 { |ch| key_name(ch) }.join(" ")
            @key_sequence.clear
            @prefix_arg = nil
            message("#{keys} is undefined")
          end
        end
      rescue Exception => e
        show_exception(e)
        @prefix_arg = nil
      end
      Window.redisplay
    end
  end
end

#echo_inputObject



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_charObject



83
84
85
# File 'lib/textbringer/controller.rb', line 83

def read_char
  Window.current.read_char
end

#read_char_nonblockObject



87
88
89
# File 'lib/textbringer/controller.rb', line 87

def read_char_nonblock
  Window.current.read_char_nonblock
end

#received_keyboard_quit?Boolean

Returns:

  • (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_editObject



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