Module: JsClient::KeyboardInput

Defined in:
lib/jschat/client.rb

Instance Method Summary collapse

Instance Method Details

#arrow_keys(data) ⇒ Object



411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/jschat/client.rb', line 411

def arrow_keys(data)
  c = data[0]
  if @sequence
    @sequence << c
    history_text = ''

    if data == 'A'
      @sequence = nil
      history_text = get_history_text(-1)
    elsif data == 'B'
      @sequence = nil
      history_text = get_history_text
    elsif data == 'O'
      return true
    elsif data == 'D'
      # left
      @sequence = nil
      @input_form.form_driver Ncurses::Form::REQ_PREV_CHAR
      @windows[:input].refresh
      return true
    elsif data == 'C'
      # right
      @input_form.form_driver Ncurses::Form::REQ_NEXT_CHAR
      @windows[:input].refresh
      @sequence = nil
      return true
    else
      @sequence = nil
      return true
    end

    begin
      @input_form.form_driver Ncurses::Form::REQ_CLR_FIELD
      @input_field.set_field_buffer(0, history_text)
      @windows[:input].addstr history_text
      @windows[:input].refresh
      @input_form.form_driver Ncurses::Form::REQ_END_LINE
    rescue Exception => exception
    end

    return true
  elsif c == 27
    @sequence = c
    return true
  end
end

#connection=(connection) ⇒ Object



655
656
657
658
# File 'lib/jschat/client.rb', line 655

def connection=(connection)
  @connection = connection
  @tab_completion = JsClient::TabComplete.new
end

#cursor_positionObject



458
459
460
# File 'lib/jschat/client.rb', line 458

def cursor_position
  cursor_position = Ncurses.getcurx(@windows[:input]) - 10
end

#display_inputObject



334
335
336
337
338
339
340
341
342
343
# File 'lib/jschat/client.rb', line 334

def display_input
  offset = @room_name.size > 0 ? @room_name.size + 3 : 0
  @input_field = Ncurses::Form::FIELD.new(1, Ncurses.COLS - offset, 0, offset, 0, 0)
  Ncurses::Form.field_opts_off(@input_field, Ncurses::Form::O_AUTOSKIP)
  Ncurses::Form.field_opts_off(@input_field, Ncurses::Form::O_STATIC)
  @input_form = Ncurses::Form::FORM.new([@input_field])
  @input_form.set_form_win @windows[:input]
  @input_form.post_form
  @input_field.set_field_buffer 0, ''
end

#display_room_nameObject



345
346
347
348
349
350
351
# File 'lib/jschat/client.rb', line 345

def display_room_name
  if @room_name
    display_input
    @windows[:input].mvprintw(0, 0, "[#{@room_name}] ")
    @windows[:input].refresh
  end
end

#display_text(message, time = Time.now.localtime) ⇒ Object



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
# File 'lib/jschat/client.rb', line 542

def display_text(message, time = Time.now.localtime)
  message = message.dup
  @windows[:text].addstr "#{time.strftime('%H:%M')} "

  if message.match /^\*/
    @windows[:text].attrset(Ncurses.COLOR_PAIR(3))
  end

  if message.match /^\* \[ERROR\]/
    @windows[:text].attrset(Ncurses.COLOR_PAIR(6))
  elsif message.match /^\[/
    channel_info = message.split(']').first.sub /\[/, ''
    message.sub! "[#{channel_info}]", ''

    @windows[:text].attrset(Ncurses.COLOR_PAIR(5))
    @windows[:text].addstr "["
    @windows[:text].attrset(Ncurses.COLOR_PAIR(4))
    @windows[:text].addstr "#{channel_info}"
    @windows[:text].attrset(Ncurses.COLOR_PAIR(5))
    @windows[:text].addstr "] "

    name = message.split('>').first.sub(/</, '').strip
    message.sub!("<#{name}> ", '')
    @windows[:text].attrset(Ncurses.COLOR_PAIR(5))
    @windows[:text].addstr '<'
    @windows[:text].attrset(Ncurses.COLOR_PAIR(0))
    @windows[:text].addstr "#{name}"
    @windows[:text].attrset(Ncurses.COLOR_PAIR(5))
    @windows[:text].addstr '>'
    @windows[:text].attrset(Ncurses.COLOR_PAIR(0))
  end

  @windows[:text].addstr "#{message}\n"
  @windows[:text].refresh
  @windows[:input].refresh

  display_time

  if message.match /^\*/
    @windows[:text].attrset(Ncurses.COLOR_PAIR(0))
  end
end

#display_timeObject



353
354
355
356
357
358
# File 'lib/jschat/client.rb', line 353

def display_time
  @windows[:info].move 0, 0
  @windows[:info].addstr "[#{Time.now.strftime('%H:%M')}]\n"
  @windows[:info].refresh
  @windows[:input].refresh
end

#display_windowsObject



318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/jschat/client.rb', line 318

def display_windows
  rows, cols = get_window_size
  @windows[:text] = Ncurses.newwin(rows - 1, cols, 0, 0)
  @windows[:info] = Ncurses.newwin(rows - 1, cols, rows - 2, 0)
  @windows[:input] = Ncurses.newwin(rows, cols, rows - 1, 0)
  @windows[:text].scrollok(true)
  @windows[:info].bkgd Ncurses.COLOR_PAIR(2)
  @windows[:input].keypad(true)
  @windows[:input].nodelay(true)

  @windows[:text].refresh
  @windows[:info].refresh
  @windows[:input].refresh
  display_input
end

#get_history_text(offset = 1) ⇒ Object



398
399
400
401
402
403
404
405
406
407
408
409
# File 'lib/jschat/client.rb', line 398

def get_history_text(offset = 1)
  offset_position = @history_position + offset
  if offset_position >= 0 and offset_position < @history.size
    @history_position = offset_position
    @history[@history_position]
  else
    if @history_position > -1 and @history_position < @history.size
      @history_position = offset_position
    end
    ''
  end
end

#get_window_sizeObject

FIXME: This doesn’t work after resize I’ve tried other ruby ncurses programs and they don’t either



391
392
393
394
395
396
# File 'lib/jschat/client.rb', line 391

def get_window_size
  Ncurses.refresh
  cols, rows = [], []
  Ncurses.stdscr.getmaxyx rows, cols
  [rows.first, cols.first]
end

#help_textObject



590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
# File 'lib/jschat/client.rb', line 590

def help_text
  "*** JsChat Help ***\nCommands start with a forward slash.  Parameters in square brackets are optional.\n/name new_name      - Change your name.  Alias: /nick\n/names [room name]  - List the people in a room.\n/join #room         - Join a room.  Alias: /j\n/switch #room       - Speak in a different room.  Alias: /s\n/part #room         - Leave a room.  Alias: /p\n/message person     - Send a private message.  Alias: /m\n/lastlog            - Display the last 100 messages for a room.\n/quit               - Quit JsChat\n*** End Help ***\n\n  TEXT\nend\n"

#manage_commands(line) ⇒ Object



607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
# File 'lib/jschat/client.rb', line 607

def manage_commands(line)
  operand = strip_command line
  case line
    when %r{^/switch}, %r{^/s}
      @connection.switch_room operand
    when %r{^/names}
      @connection.send_names operand
    when %r{^/name}, %r{^/nick}
      @connection.send_change_name operand
    when %r{^/quit}
      quit
    when %r{^/join}, %r{^/j}
      @connection.send_join operand
    when %r{^/part}, %r{^/p}
      @connection.send_part operand
    when %r{^/lastlog}
      @connection.send_lastlog
    when %r{^/clear}
      @windows[:text].clear
      @windows[:text].refresh
      display_time
    when %r{^/message}, %r{^/m}
      if operand and operand.size > 0
        message = operand.match(/([^ ]*)\s+(.*)/)
        if message
          @connection.send_private_message message[1], message[2]
        end
      end
    when %r{^/help}
      help_text.split("\n").each do |message|
        display_text message
      end
    when %r{^/}
      display_text '* Command not found.  Try using /help'
    else
      @connection.send_message(line)
  end
end

#quitObject



585
586
587
588
# File 'lib/jschat/client.rb', line 585

def quit
  Ncurses.endwin
  exit
end

#receive_data(data) ⇒ Object



462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'lib/jschat/client.rb', line 462

def receive_data(data)
  @clipboard ||= ''
  c = data[0]

  if arrow_keys data
    return
  end

  if c != Ncurses::KEY_TAB
    @tab_completion.reset
  end

  case c
    when -1
      # Return
    when Ncurses::KEY_TAB
      @tab_completion.run @connection.names, @input_field, @input_form, cursor_position
    when Ncurses::KEY_ENTER, ?\n, ?\r
      @input_form.form_driver Ncurses::Form::REQ_BEG_LINE
      line = @input_field.field_buffer(0)
      line.rstrip!

      if !line.empty? and line.length > 0
        @history << line.dup
        @history_position = @history.size
        manage_commands line
      end
      @input_form.form_driver Ncurses::Form::REQ_CLR_FIELD
    when ?\C-l
      # Refresh
      resize
    when Ncurses::KEY_BACKSPACE, ?\C-h, 127
      # Backspace
      @input_form.form_driver Ncurses::Form::REQ_DEL_PREV
      @input_form.form_driver Ncurses::Form::REQ_CLR_EOL
    when ?\C-d
      @input_form.form_driver Ncurses::Form::REQ_DEL_CHAR
    when Ncurses::KEY_LEFT, ?\C-b
      @input_form.form_driver Ncurses::Form::REQ_PREV_CHAR
    when Ncurses::KEY_RIGHT, ?\C-f
      @input_form.form_driver Ncurses::Form::REQ_NEXT_CHAR
    when ?\C-a
      @input_form.form_driver Ncurses::Form::REQ_BEG_LINE
    when ?\C-e
      @input_form.form_driver Ncurses::Form::REQ_END_LINE
    when ?\C-k
      @input_form.form_driver Ncurses::Form::REQ_CLR_EOL
    when ?\C-u
      @input_form.form_driver Ncurses::Form::REQ_BEG_LINE
      @clipboard = @input_field.field_buffer(0)
      @input_form.form_driver Ncurses::Form::REQ_CLR_FIELD
    when ?\C-y
      unless @clipboard.empty?
        cursor_position = Ncurses.getcurx(@windows[:input])
        
        text = @input_field.field_buffer(0).insert(cursor_position - 9, @clipboard)
        @input_field.set_field_buffer(0, text)
        
        @windows[:text].addstr "#{cursor_position}\n #{text}"
        @windows[:text].refresh
      end
    when ?\C-c
      quit
    when ?\C-w
      @input_form.form_driver Ncurses::Form::REQ_PREV_CHAR
      @input_form.form_driver Ncurses::Form::REQ_DEL_WORD
    else
      @input_form.form_driver c
  end
  @windows[:input].refresh
end

#remove_formsObject



306
307
308
309
# File 'lib/jschat/client.rb', line 306

def remove_forms
  @input_field.free_field
  @input_form.free_form
end

#remove_windowsObject



300
301
302
303
304
# File 'lib/jschat/client.rb', line 300

def remove_windows
  @windows.each do |name, window|
    window.delete
  end
end

#remove_windows_and_formsObject



295
296
297
298
# File 'lib/jschat/client.rb', line 295

def remove_windows_and_forms
  remove_windows
  remove_forms
end

#resizeObject



360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/jschat/client.rb', line 360

def resize
  # Save the user input
  @input_form.form_driver Ncurses::Form::REQ_BEG_LINE
  input = @input_field.field_buffer(0)
  input.rstrip!

  Ncurses.def_prog_mode
  Ncurses.endwin
  Ncurses.reset_prog_mode

  update_windows

  cols = get_window_size[0]
  if @lastlog.size > 0
    lastlog_start = cols > @lastlog.size ? @lastlog.size : cols
    @lastlog[-lastlog_start..-1].each do |message|
      display_text message[0], message[1]
    end
  end

  @input_field.set_field_buffer(0, input)
  @windows[:input].addstr input
  @input_form.form_driver Ncurses::Form::REQ_END_LINE

  Ncurses.refresh
rescue Exception => exception
  puts exception
end

#room_name=(room_name) ⇒ Object



274
275
276
# File 'lib/jschat/client.rb', line 274

def room_name=(room_name)
  @room_name = room_name 
end

#setup_screenObject



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/jschat/client.rb', line 252

def setup_screen
  Ncurses.initscr
  @windows = {}
  
  Ncurses.raw
  Ncurses.start_color
  Ncurses.noecho
  Ncurses.use_default_colors
  Ncurses.init_pair 2, Ncurses::COLOR_WHITE, Ncurses::COLOR_BLUE
  Ncurses.init_pair 3, Ncurses::COLOR_CYAN, -1
  Ncurses.init_pair 4, Ncurses::COLOR_YELLOW, -1
  Ncurses.init_pair 5, Ncurses::COLOR_BLACK, -1
  Ncurses.init_pair 6, Ncurses::COLOR_RED, -1

  @history_position = 0
  @history = []
  @lastlog = []
  @room_name = ''

  setup_windows
end

#setup_windowsObject



278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# File 'lib/jschat/client.rb', line 278

def setup_windows
  Ncurses.refresh

  display_windows

  Thread.new do
    loop do
      display_time
      sleep 60 - Time.now.sec
    end
  end

  Signal.trap('SIGWINCH') do
    resize
  end
end

#show_message(messages, time = Time.now.localtime) ⇒ Object



534
535
536
537
538
539
540
# File 'lib/jschat/client.rb', line 534

def show_message(messages, time = Time.now.localtime)
  messages.split("\n").each do |message|
    @lastlog << [message.dup, time]
    @lastlog.shift if @lastlog.size > 250
    display_text message, time
  end
end

#strip_command(line) ⇒ Object



646
647
648
649
650
651
652
653
# File 'lib/jschat/client.rb', line 646

def strip_command(line)
  matches = line.match(%r{/[a-zA-Z][^ ]*(.*)})
  if matches
    matches[1].strip
  else
    line
  end
end

#update_windowsObject



311
312
313
314
315
316
# File 'lib/jschat/client.rb', line 311

def update_windows
  rows, cols = get_window_size
  remove_windows_and_forms
  display_windows
  display_room_name
end