Class: Terminalwire::V2::Server::Context::Stdin

Inherits:
Object
  • Object
show all
Defined in:
lib/terminalwire/v2/server/context.rb

Overview

Resource facades — thin request wrappers with a Ruby-ish interface.

Constant Summary collapse

DEFAULT_CHUNK =
64 * 1024

Instance Method Summary collapse

Constructor Details

#initialize(runtime) ⇒ Stdin

Returns a new instance of Stdin.



186
# File 'lib/terminalwire/v2/server/context.rb', line 186

def initialize(runtime) = @runtime = runtime

Instance Method Details

#each_chunk(n = DEFAULT_CHUNK) ⇒ Object

Yield each chunk as it arrives until EOF (streaming without buffering).



213
214
215
216
217
218
219
220
221
# File 'lib/terminalwire/v2/server/context.rb', line 213

def each_chunk(n = DEFAULT_CHUNK)
  return enum_for(:each_chunk, n) unless block_given?

  loop do
    data, eof = read_chunk(n)
    yield data unless data.empty?
    break if eof
  end
end

#getpassObject



189
# File 'lib/terminalwire/v2/server/context.rb', line 189

def getpass = @runtime.request(:stdin, :getpass)

#getsObject



188
# File 'lib/terminalwire/v2/server/context.rb', line 188

def gets = @runtime.request(:stdin, :gets)

#readObject

Drain the client's stdin to EOF (for piped input).



202
203
204
205
206
207
208
209
210
# File 'lib/terminalwire/v2/server/context.rb', line 202

def read
  buffer = +"".b
  loop do
    data, eof = read_chunk
    buffer << data.b # force binary: chunks may arrive as UTF-8 (msgpack
    break if eof     # str) or binary (bin); mixing them would raise.
  end
  buffer
end

#read_chunk(n = DEFAULT_CHUNK) ⇒ Object

Pull up to n bytes from the client's stdin. Returns [data, eof].



196
197
198
199
# File 'lib/terminalwire/v2/server/context.rb', line 196

def read_chunk(n = DEFAULT_CHUNK)
  response = @runtime.request(:stdin, :read_chunk, { "n" => n })
  [response["data"] || "".b, response["eof"]]
end

#tty?Boolean

Is the client's stdin a terminal (vs a pipe/file)? Branch on this to decide between prompting and draining piped data.

Returns:

  • (Boolean)


193
# File 'lib/terminalwire/v2/server/context.rb', line 193

def tty? = @runtime.terminal.stdin.tty?