Method: Pssh::Pty#initialize

Defined in:
lib/pssh/pty.rb

#initializePty

Returns a new instance of Pty.



12
13
14
15
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
# File 'lib/pssh/pty.rb', line 12

def initialize
  @stream = ''
  set_command
  clear_environment
  Thread.new do
    begin
      @read, @write, @pid = PTY.spawn(@command)
      @write.winsize = $stdout.winsize
      if new?
        system("clear")
        pssh = "# [ pssh terminal ]\n# Type `exit` to terminate this terminal.\n"
        $stdout.puts pssh
        Signal.trap(:WINCH) do
          resize!
        end
        system("stty raw -echo")
      end

      begin
        io = [@read]
        io << $stdin if new?
        rs, ws = IO.select(io)
        r = rs[0]
        while (data = r.read_nonblock(2048)) do
          if new? && r == $stdin
            @write.write_nonblock data
          else
            $stdout.write_nonblock data if new?
            data.encode!('UTF-16', 'UTF-8', :invalid => :replace, :replace => '')
            data.encode!('UTF-8', 'UTF-16')
            if data.valid_encoding?
              store data
              Pssh.socket.write data
            end
          end
        end
      rescue Exception => e
        if e.is_a?(Errno::EAGAIN)
          retry
        else
          system("stty -raw echo") if new?
          puts 'Terminating Pssh.'
          Kernel.exit!
        end
      end
    end
  end
end