Class: RailsConsole::PtySession

Inherits:
Object
  • Object
show all
Defined in:
lib/rails-console/pty_session.rb

Constant Summary collapse

READ_SIZE =
8192

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command: nil, sandbox: true) ⇒ PtySession

Returns a new instance of PtySession.



13
14
15
16
17
# File 'lib/rails-console/pty_session.rb', line 13

def initialize(command: nil, sandbox: true)
  command ||= sandbox ? RailsConsole.sandbox_command : RailsConsole.command

  @output, @input, @pid = PTY.spawn(self.class.spawn_env, command)
end

Instance Attribute Details

#pidObject (readonly)

Returns the value of attribute pid.



9
10
11
# File 'lib/rails-console/pty_session.rb', line 9

def pid
  @pid
end

Class Method Details

.spawn_envObject

Pagers hang inside the web terminal, so force a pass-through one.



20
21
22
23
24
25
# File 'lib/rails-console/pty_session.rb', line 20

def self.spawn_env
  {
    'PAGER' => 'cat',
    'TERM' => 'xterm-256color',
  }
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


27
28
29
30
31
# File 'lib/rails-console/pty_session.rb', line 27

def alive?
  Process.waitpid(pid, Process::WNOHANG).nil?
rescue Errno::ECHILD
  false
end

#kill!Object



33
34
35
36
37
38
# File 'lib/rails-console/pty_session.rb', line 33

def kill!
  Process.kill('TERM', pid)
  Process.waitpid(pid)
rescue Errno::ESRCH, Errno::ECHILD
  nil
end

#read_nonblock(size = READ_SIZE) ⇒ Object



40
41
42
43
44
# File 'lib/rails-console/pty_session.rb', line 40

def read_nonblock(size = READ_SIZE)
  @output.read_nonblock(size)
rescue IO::WaitReadable, EOFError
  nil
end