Skip to content
Kward Search API index

Class: Kward::InteractivePtyRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/interactive_pty_runner.rb

Overview

Runs a command in a PTY while forwarding caller-owned input and output IOs. This is intentionally low level: UI orchestration decides when terminal ownership is handed to the child process and how the result is presented.

Defined Under Namespace

Classes: Result

Constant Summary collapse

READ_SIZE =
4096

Instance Method Summary collapse

Constructor Details

#initialize(window_size_provider: nil) ⇒ InteractivePtyRunner

Returns a new instance of InteractivePtyRunner.



16
17
18
19
# File 'lib/kward/interactive_pty_runner.rb', line 16

def initialize(window_size_provider: nil)
  @window_size_provider = window_size_provider
  @window_size = nil
end

Instance Method Details

#run(*command, env: {}, cwd: Dir.pwd, input: $stdin, output: $stdout, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/kward/interactive_pty_runner.rb', line 21

def run(*command, env: {}, cwd: Dir.pwd, input: $stdin, output: $stdout, &block)
  pid = nil
  status = nil
  writer = nil
  input_forwarded = false

  PTY.spawn(env.to_h, *command, chdir: cwd.to_s) do |reader, child_writer, child_pid|
    pid = child_pid
    writer = child_writer
    update_window_size(reader, pid)
    with_raw_input(input) do
      drain_initial_input(input, writer)
      status, input_forwarded = forward_io(reader, writer, pid, input, output, &block)
    end
    status ||= wait_for_status(pid)
  end

  Result.new(exit_status: exit_status(status), input_forwarded: input_forwarded)
ensure
  writer&.close unless writer&.closed?
  terminate_and_reap(pid) if pid && status.nil?
end