Module: PtyCompat::NodePty

Defined in:
lib/pty_compat/node_pty.rb

Overview

Provide a similar interface as the PTY one that could work on platforms that don't support PTY (for example Windows). Internally uses NodeJS's node-pty.

Public API collapse

Instance Method Details

#last_statusProcess::Status

Returns Last process status.

Returns:

  • (Process::Status)

    Last process status



36
37
38
# File 'lib/pty_compat/node_pty.rb', line 36

def last_status
  @last_wait_thr.value
end

#spawn(*args) {|r, w, pid| ... } ⇒ Array<IO, Integer>?

Spawn a command in a PTY and return or yield its outputs, input and pid

Parameters:

  • args (Array)

    The command arguments to execute (see ::PTY#spawn)

Yields:

  • An optional code called with all PTY outputs, input and PID.

Yield Parameters:

  • r (IO)

    The reader output (containing stdout and stderr).

  • w (IO)

    The writer input (containing stdin).

  • pid (Integer)

    The process PID.

Returns:

  • (Array<IO, Integer>, nil)

    The reader, writer and PID of the process, or nil if used with a yielded block.

    • r [IO] The reader output (containing stdout and stderr).
    • w [IO] The writer input (containing stdin).
    • pid [Integer] The process PID.


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/pty_compat/node_pty.rb', line 20

def spawn(*args)
  env, cmd = args.first.is_a?(Hash) ? [[args.first], args[1..]] : [[], args]
  node_args = env + ['node', "#{__dir__}/assets/node_pty_bridge.js"] + cmd
  if block_given?
    Open3.popen3(*node_args) do |stdin, stdout, stderr, wait_thr|
      @last_wait_thr = wait_thr
      yield popen3_to_pty(stdin, stdout, stderr, wait_thr)
    end
  else
    stdin, stdout, stderr, wait_thr = Open3.popen3(*node_args)
    @last_wait_thr = wait_thr
    popen3_to_pty(stdin, stdout, stderr, wait_thr)
  end
end