Class: Kward::InteractivePtyRunner
- Inherits:
-
Object
- Object
- Kward::InteractivePtyRunner
- 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
-
#initialize(window_size_provider: nil) ⇒ InteractivePtyRunner
constructor
A new instance of InteractivePtyRunner.
- #run(*command, env: {}, cwd: Dir.pwd, input: $stdin, output: $stdout) ⇒ Object
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) ⇒ Object
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 |
# File 'lib/kward/interactive_pty_runner.rb', line 21 def run(*command, env: {}, cwd: Dir.pwd, input: $stdin, output: $stdout) pid = nil status = nil PTY.spawn(env.to_h, *command, chdir: cwd.to_s) do |reader, writer, child_pid| pid = child_pid update_window_size(reader, pid) with_raw_input(input) do drain_initial_input(input, writer) loop do update_window_size(reader, pid) readable = IO.select([reader, input], nil, nil, 0.02)&.first || [] forward_pty_output(reader, output) if readable.include?(reader) forward_input(input, writer) if readable.include?(input) if (finished_status = finished_status(pid)) status = finished_status break end rescue Errno::EIO, IOError break end end status ||= wait_for_status(pid) ensure writer&.close unless writer&.closed? end Result.new(exit_status: exit_status(status)) end |