Class: Kward::NativeShell::Worker
- Inherits:
-
Object
- Object
- Kward::NativeShell::Worker
- Defined in:
- lib/kward/shell/native_worker.rb
Overview
A single-threaded interpreter process owns the terminal and mutable shell state. Requests/state use a private socket, never terminal input/output.
Class Method Summary collapse
Class Method Details
.run(descriptor) ⇒ Object
11 12 13 14 15 16 17 18 19 20 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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/kward/shell/native_worker.rb', line 11 def self.run(descriptor) control = UNIXSocket.for_fd(descriptor) control.close_on_exec = true control.sync = true $stdout.sync = true $stderr.sync = true runtime = nil Signal.trap("INT") { raise Interrupt } %w[TERM HUP USR2].each { |signal| Signal.trap(signal) { raise SystemExit } } %w[TSTP TTIN TTOU].each { |signal| Signal.trap(signal, "IGNORE") } line = control.gets return unless line request = JSON.parse(line) runtime = Runtime.new(cwd: request.fetch("cwd"), env: ENV.to_h, aliases: request.fetch("aliases", {}), interactive: true, process_owned: true) runtime.with_signals do loop do status = begin $stdin.echo = true if request["config"] apply_configuration(runtime, request["config"]) elsif request["suppress_git_pager"] runtime.with_pager_suppressed { runtime.run(request.fetch("command")) } else runtime.run(request.fetch("command")) end rescue Interrupt 130 ensure $stdin.echo = false end status = runtime.finish if runtime.exited $stdout.write("#{request.fetch('marker')}:#{status}:\n") control.puts(JSON.generate(cwd: runtime.cwd, env: runtime.environment, aliases: runtime.aliases, exited: runtime.exited)) break if runtime.exited loop do begin runtime.poll_signals break if runtime.exited || IO.select([control], nil, nil, 0.05) rescue Interrupt # Ctrl-C at an idle prompt cancels input, not the shell lifetime. end end if runtime.exited runtime.finish break end line = control.gets break unless line request = JSON.parse(line) end end rescue EOFError, Errno::EIO, Errno::EPIPE nil ensure runtime&.close end |