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 sink. 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, sink:, tab_action_handler: nil, on_detach: nil) ⇒ Object
Constructor Details
#initialize(window_size_provider: nil) ⇒ InteractivePtyRunner
Returns a new instance of InteractivePtyRunner.
18 19 20 21 |
# File 'lib/kward/interactive_pty_runner.rb', line 18 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, sink:, tab_action_handler: nil, on_detach: nil) ⇒ Object
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 |
# File 'lib/kward/interactive_pty_runner.rb', line 23 def run(*command, env: {}, cwd: Dir.pwd, input: $stdin, sink:, tab_action_handler: nil, on_detach: nil) reader, writer, pid = PTY.spawn(env.to_h, *command, chdir: cwd.to_s) status = nil input_forwarded = false tab_action = nil forwarded = false detached = nil update_window_size(reader, pid) with_raw_input(input) do input_forwarded, tab_action = drain_initial_input(input, writer, sink, tab_action_handler: tab_action_handler) if tab_action && on_detach detached_sink = on_detach.call sink.finish if sink.respond_to?(:finish) detached = detach_process(reader, writer, pid, detached_sink, input_forwarded) reader = writer = pid = nil elsif tab_action status = terminate_and_reap(pid) else status, forwarded, tab_action = forward_io(reader, writer, pid, input, sink, tab_action_handler: tab_action_handler, on_detach: on_detach) input_forwarded ||= forwarded if tab_action && on_detach detached_sink = on_detach.call sink.finish if sink.respond_to?(:finish) detached = detach_process(reader, writer, pid, detached_sink, input_forwarded) reader = writer = pid = nil end end input_forwarded ||= forwarded end sink.finish if sink.respond_to?(:finish) && !detached status ||= wait_for_status(pid) if pid Result.new(exit_status: exit_status(status), input_forwarded: input_forwarded, tab_action: tab_action, background: detached) ensure writer&.close unless writer&.closed? reader&.close unless reader&.closed? terminate_and_reap(pid) if pid && status.nil? end |