Skip to content
Kward Search API index

Module: Kward::NativeShell::Program

Defined in:
lib/kward/shell/native_program.rb

Overview

Read one complete command at a time. In stdin mode the next byte may belong to read or an external command, so never buffer past the logical boundary.

Class Method Summary collapse

Class Method Details

.each(input, aliases: {}, on_read: nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/kward/shell/native_program.rb', line 15

def self.each(input, aliases: {}, on_read: nil)
  source = +"".b
  line = 1
  loop do
    begin
      byte = input.readpartial(1)
    rescue EOFError
      yield Parser.parse(source_encoding(source), aliases: aliases, line: line) unless source.empty?
      break
    end
    on_read&.call(byte)
    source << byte
    next unless byte == "\n"
    begin
      tree = Parser.parse(source_encoding(source), aliases: aliases, line: line, eof: false)
    rescue IncompleteInput
      next
    end
    yield tree
    line += source.count("\n")
    source = +"".b
  end
end