Skip to content
Kward Search API index

Class: Kward::NativeShell::StandaloneLink

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

Overview

Interpreter side of the private launcher channel. Terminal bytes are never interpreted as protocol messages.

Instance Method Summary collapse

Constructor Details

#initialize(descriptor) ⇒ StandaloneLink

Returns a new instance of StandaloneLink.



9
10
11
12
13
14
15
# File 'lib/kward/shell/standalone_link.rb', line 9

def initialize(descriptor)
  @channel = UNIXSocket.for_fd(descriptor)
  @channel.close_on_exec = true
  @channel.sync = true
  $stdout.sync = $stderr.sync = true
  request("ready")
end

Instance Method Details

#ask(instruction, runtime, input:, output:, error:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/kward/shell/standalone_link.rb', line 29

def ask(instruction, runtime, input:, output:, error:)
  send_message("ask", instruction: utf8(instruction), cwd: utf8(runtime.cwd))
  loop do
    message = receive
    case message.fetch("type")
    when "run"
      command = message.fetch("command")
      status = runtime.run_shell_agent_command(command, timeout: message.fetch("timeout"), output: output, error: error) do
        echo_command(command, prompt: yield, terminal: error)
        request("running") # Wait for the launcher to drain the echo and start capture.
      end
      send_message("result", status: status, cwd: utf8(runtime.cwd))
    when "answer"
      input.iflush
      answer = message.fetch("text")
      output.puts(answer) unless answer.empty?
      return message["draft"] unless runtime.exited
      return nil
    end
  end
end

#begin_command(command, input:) ⇒ Object



17
18
19
# File 'lib/kward/shell/standalone_link.rb', line 17

def begin_command(command, input:)
  request("begin", command: utf8(command), input_pending: !!IO.select([input], nil, nil, 0))
end

#clear_context ⇒ Object



25
26
27
# File 'lib/kward/shell/standalone_link.rb', line 25

def clear_context
  request("clear_context")
end

#finish_command(status) ⇒ Object



21
22
23
# File 'lib/kward/shell/standalone_link.rb', line 21

def finish_command(status)
  request("finish", status: status)
end