Class: SPSChatCli

Inherits:
SPSChat
  • Object
show all
Defined in:
lib/sps_chat-cli.rb

Instance Method Summary collapse

Constructor Details

#initialize(host: '127.0.0.1', port: '8080', userid: 'user' + (0..1000).to_a.sample.to_s, room: '', typing_mode: false) ⇒ SPSChatCli

Returns a new instance of SPSChatCli.



16
17
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sps_chat-cli.rb', line 16

def initialize(host: '127.0.0.1', port: '8080', \
                userid: 'user' + (0..1000).to_a.sample.to_s, room: '', typing_mode: false)

  super(host: host, port: port, userid: userid, room: room)
  
  @userid, @typing_mode = userid, typing_mode
  terminated = false
  @user, @history = {}, []
  
  sleep 1
  print "\r\n"

  loop do

    @s = ''

    print "\r#{userid}> "

    begin

      c = $stdin.getch
      (terminated = true; break) if c == CTRLC
      
      if typing_mode then
        
        unless c == "\r" then
          typing c 
        else
          c = "\n"            
          @history << "#{userid}> " + @s
          send @s            
        end
      end

      @s = savebuffer @s, c
      print "\r#{@userid}>  " + ' ' * @s.length if c == BACKSPACE
      _, cols = TermInfo.screen_size
      clear_screen if ("#{@userid}>  " + @s).length > cols
      print "\r#{@userid}> " + @s 
      
    end until c == "\r" or c == "\n" or terminated

    break if terminated
    
    unless typing_mode then
      print "\r" + @userid + '> ' + @s + "\n"
      send @s
    end

  end 
end