Skip to content
Kward Search API index

Class: Kward::NativeShell::StandaloneTerminal

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

Overview

The inner PTY is the shell's controlling terminal; the launcher only relays bytes, resizes it, and retains a bounded pre-input snapshot.

Constant Summary collapse

CHANNEL_FD =
30
CAPTURE_BYTES =
32_000
CHUNK_BYTES =
16_384
POLL_SECONDS =
0.05

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#cwd ⇒ Object (readonly)

Returns the value of attribute cwd.



248
249
250
# File 'lib/kward/shell/standalone_terminal.rb', line 248

def cwd
  @cwd
end

Instance Method Details

#context_snapshot ⇒ Object



250
251
252
# File 'lib/kward/shell/standalone_terminal.rb', line 250

def context_snapshot
  { cwd: @cwd, last_command: @last_command, last_output: @last_output, exit_status: @status_of_command }
end

#run(ruby_arguments, descriptors) ⇒ 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
50
51
52
53
54
# File 'lib/kward/shell/standalone_terminal.rb', line 21

def run(ruby_arguments, descriptors)
  @lock = Mutex.new
  @pending_input = +"".b
  @null = File.open(File::NULL, "w")
  @channel, child = UNIXSocket.pair
  @channel.sync = true
  @reader, @writer, @pid = PTY.spawn(terminal_defaults, *ruby_arguments, descriptors.merge(CHANNEL_FD => child))
  child.close
  @writer.sync = true
  @reader.console_mode = $stdin.console_mode
  resize
  handlers = %w[HUP TERM INT].to_h do |signal|
    [signal, Signal.trap(signal) { @signal = signal }]
  end
  $stdin.raw(intr: false) do
    server = Thread.new { serve }
    server.report_on_exception = false
    begin
      relay
    ensure
      @channel.shutdown rescue nil
      server.kill
      server.join
    end
  end
  @reader.close
  @writer.close
  reap_child
  @status&.exitstatus || (@status&.termsig && 128 + @status.termsig) || 1
ensure
  handlers&.each { |signal, handler| Signal.trap(signal, handler) }
  [@reader, @writer, @channel, child, @null].compact.each { |io| io.close unless io.closed? }
  reap_child
end

#run_for_agent(command, timeout_seconds: nil, cancellation: nil) ⇒ Object



258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/kward/shell/standalone_terminal.rb', line 258

def run_for_agent(command, timeout_seconds: nil, cancellation: nil)
  cancellation&.raise_if_cancelled!
  respond("run", command: command, timeout: timeout_seconds || self.timeout_seconds)
  JSON.parse(@channel.readline).fetch("type") == "running" or raise IOError, "Shell agent command did not start"
  @lock.synchronize do
    drain_output
    start_capture
    @agent_running = true
    respond("ok")
  end
  Process.kill("INT", @pid) if cancellation&.cancelled?
  result = JSON.parse(@channel.readline)
  @cwd = result.fetch("cwd")
  output = @lock.synchronize do
    captured = stop_capture
    write_terminal("\r\n") unless @at_line_start
    captured
  end
  @last_command, @last_output, @status_of_command = command, output, result.fetch("status")
  cancellation&.raise_if_cancelled!
  Kwsh::Result.new(output: output, exit_status: @status_of_command)
ensure
  @lock.synchronize do
    @agent_running = false
    @capture = nil
  end
end

#timeout_seconds ⇒ Object



254
255
256
# File 'lib/kward/shell/standalone_terminal.rb', line 254

def timeout_seconds
  Kwsh::DEFAULT_TIMEOUT_SECONDS
end