Class: Terminalwire::V2::Server::Context::Stdin
- Inherits:
-
Object
- Object
- Terminalwire::V2::Server::Context::Stdin
- 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
-
#each_chunk(n = DEFAULT_CHUNK) ⇒ Object
Yield each chunk as it arrives until EOF (streaming without buffering).
- #getpass ⇒ Object
- #gets ⇒ Object
-
#initialize(runtime) ⇒ Stdin
constructor
A new instance of Stdin.
-
#read ⇒ Object
Drain the client's stdin to EOF (for piped input).
-
#read_chunk(n = DEFAULT_CHUNK) ⇒ Object
Pull up to
nbytes from the client's stdin. -
#tty? ⇒ Boolean
Is the client's stdin a terminal (vs a pipe/file)? Branch on this to decide between prompting and draining piped data.
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 |
#getpass ⇒ Object
189 |
# File 'lib/terminalwire/v2/server/context.rb', line 189 def getpass = @runtime.request(:stdin, :getpass) |
#gets ⇒ Object
188 |
# File 'lib/terminalwire/v2/server/context.rb', line 188 def gets = @runtime.request(:stdin, :gets) |
#read ⇒ Object
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.
193 |
# File 'lib/terminalwire/v2/server/context.rb', line 193 def tty? = @runtime.terminal.stdin.tty? |