Skip to content
Kward Search API index

Class: Kward::PromptInterface::TerminalHandoffOutput

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

Overview

Kward owns keyboard encoding while its tab shortcuts are reserved. Child keyboard-mode mutations must not pop our stack entry or change that encoding. Other controls remain terminal-owned, including streamed OSC/DCS payloads.

Constant Summary collapse

MAX_SEQUENCE_BYTES =
4096

Instance Method Summary collapse

Constructor Details

#initialize(output) ⇒ TerminalHandoffOutput

Returns a new instance of TerminalHandoffOutput.



159
160
161
162
163
164
# File 'lib/kward/prompt_interface.rb', line 159

def initialize(output)
  @output = output
  @pending = +"".b
  @string = false
  @string_escape = false
end

Instance Method Details

#flush ⇒ Object



199
200
201
# File 'lib/kward/prompt_interface.rb', line 199

def flush
  @output.flush if @output.respond_to?(:flush)
end


194
195
196
197
# File 'lib/kward/prompt_interface.rb', line 194

def print(*values)
  values.each { |value| write(value) }
  nil
end

#write(value) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/kward/prompt_interface.rb', line 166

def write(value)
  value = value.to_s.b
  forwarded = +"".b
  value.each_byte do |byte|
    if @string
      forwarded << byte
      @string = false if byte == 0x18 || byte == 0x1a || (@string == 0x5d && byte == 0x07) || (@string_escape && byte == 0x5c)
      @string_escape = byte == 0x1b
    elsif @pending.empty?
      byte == 0x1b ? @pending << byte : forwarded << byte
    elsif byte == 0x1b
      forwarded << @pending << TerminalSequences::CONTROL_SEQUENCE_CANCEL
      @pending = +"\e".b
    elsif byte == 0x18 || byte == 0x1a
      forwarded << @pending << byte
      @pending.clear
    elsif byte < 0x20 || byte == 0x7f
      # C0 controls can execute inside CSI without ending its parameters.
      forwarded << byte
    else
      @pending << byte
      consume_sequence(forwarded)
    end
  end
  @output.write(forwarded) unless forwarded.empty?
  value.bytesize
end