Module: Lijab::InputHandler

Defined in:
lib/lijab/input.rb

Constant Summary collapse

DEFAULT_PROMPT =
"> "

Class Method Summary collapse

Class Method Details

.completer(line) ⇒ Object



196
197
198
199
200
201
202
203
204
# File 'lib/lijab/input.rb', line 196

def completer(line)
   return if !Main.connected

   if line[0] == ?/
      Commands::completer(line)
   else
      Main.contacts.completer(line)
   end
end

.delete_last_typedObject



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/lijab/input.rb', line 160

def delete_last_typed
   if @last_typed.is_a?(Array)
      @last_typed.each do |line|
         # line length + multiline prompt + \n
         # FIXME: put the multiline prompt somewhere
         print "\b" * (line.length + 6)
         print "#{ANSIMove.up(1)}"
      end
      print "#{ANSIMove.down(1)}" if @last_typed.length > 0
   else
      print "\b" * @last_typed.length
   end
end

.delete_typedObject



174
175
176
177
178
179
180
# File 'lib/lijab/input.rb', line 174

def delete_typed
   if @multiline
      delete_last_typed()
   else
      print "\b" * Readline::line_buffer.length
   end
end

.initObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/lijab/input.rb', line 18

def init
   Readline::completer_word_break_characters = ""
   Readline::completion_append_character = " "
   Readline::completion_proc = method(:completer).to_proc
   Readline::pre_input_proc = lambda do
      print "#{ANSI.cleartoeol}" ; STDOUT.flush
      unless @last_to.empty?
         Readline::insert_text("#{@last_to}: ")
         Readline::redisplay
      end
   end

   if Config.opts[:ctrl_c_quits]
      trap("SIGINT") { Main.quit }
   else
      trap("SIGINT") do
         Readline::line_buffer = ""
         puts
         Out::make_infoline
         print "#{@prompt}"
         STDOUT.flush
      end
   end

   read_typed_history()

   init_char_input_stuff()

   @input_thread = Thread.new { read_input() }
end

.init_char_input_stuffObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/lijab/input.rb', line 58

def init_char_input_stuff
   # i'm surprised this doesn't make typing fucking unbearable

   @on_char_input_blocks = []

   @on_char_input_blocks << lambda do |c|
      to, msg = Readline::line_buffer.split(":", 2).strip
      if to && msg && Main.contacts.key?(to)
         # TODO: try to see if a thread improves things
         Main.contacts[to].typed_stuff
      end
      c
   end

   Readline::char_input_proc = lambda do |c|
      ret = c
      @on_char_input_blocks.each do |block|
         ret = block.call(c)
         break if ret != c
      end
      ret
   end
end

.multiline(enable, first_line = "") ⇒ Object



221
222
223
224
225
226
227
228
229
230
# File 'lib/lijab/input.rb', line 221

def multiline(enable, first_line="")
   @multiline = enable
   @multilines = []
   if enable
      @multilines.push(first_line) unless first_line.empty?
      prompt("---> ")
   else
      reset_prompt()
   end
end

.multiline?Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/lijab/input.rb', line 217

def multiline?
   @multiline
end

.on_char_input(&block) ⇒ Object



82
83
84
# File 'lib/lijab/input.rb', line 82

def on_char_input(&block)
   @on_char_input_blocks << block
end

.process_input(text) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/lijab/input.rb', line 136

def process_input(text)
   return if text.empty?

   if !Main.connected
      # FIXME: brute force ftw!
      Out::error("not connected :-(", false)
      return
   end

   if text[0] == ?/
      Commands::run(*text[1..-1].split(" ", 2))
      @last_to = ""
   else
      to, msg = text.split(":", 2)
      return unless to && msg && !msg.empty? && Main.contacts.key?(to)
      msg = msg[1..-1] if msg[0].chr == " " # goddammit, whitespace will be the death of me

      @last_to = to
      jid = Jabber::JID.new(to)
      jid = nil unless jid.resource
      Main.contacts[to].send_message(msg, jid)
   end
end

.prompt(p = nil) ⇒ Object



49
50
51
52
# File 'lib/lijab/input.rb', line 49

def prompt(p=nil)
   return @prompt unless p
   @prompt = p
end

.read_inputObject

next unless to && msg && Main.contacts.key?(to)

end

end



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/lijab/input.rb', line 101

def read_input
   loop do
      Out::make_infoline

      t = Readline::readline(@prompt, true)

      @last_typed = t || ""

      if !t
         if @multiline
            @last_typed = @multilines
            process_input(@multilines.join("\n"))
            multiline(false)
         else
            if Config.opts[:ctrl_c_quits]
               puts ; next
            else
               Main.quit
            end
         end
      elsif !@multiline && t =~ /^\s*$/
         Readline::HISTORY.pop
      else
         Readline::HISTORY.pop if Readline::HISTORY.to_a[-2] == t

         if @multiline
            @multilines.push(t)
            @last_typed = @multilines
         else
            process_input(t)
         end
      end
   end
end

.read_typed_historyObject



212
213
214
215
# File 'lib/lijab/input.rb', line 212

def read_typed_history
   path = Config.[:typed]
   File.read(path).each { |l| Readline::HISTORY.push(l.chomp) } if File.file?(path)
end

.redisplay_inputObject



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/lijab/input.rb', line 182

def redisplay_input()
   if @multiline && !@multilines.empty?
      puts "#{ANSI.clearline}#{DEFAULT_PROMPT}#{@multilines[0]}"
      @multilines[1..-1].each do |line|
         puts "#{ANSI.clearline}#{@prompt}#{line}"
      end
   end

   Out::make_infoline()
   print "#{@prompt}#{Readline::line_buffer}"
   STDOUT.flush
end

.reset_promptObject



54
55
56
# File 'lib/lijab/input.rb', line 54

def reset_prompt
   @prompt = DEFAULT_PROMPT
end

.save_typed_historyObject



206
207
208
209
210
# File 'lib/lijab/input.rb', line 206

def save_typed_history
   File.open(Config.[:typed], 'w') do |f|
      f.puts(Readline::HISTORY.to_a[-300..-1] || Readline::HISTORY.to_a)
   end
end