Class: Yildun::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/yildun/console.rb,
sig/yildun.rbs

Instance Method Summary collapse

Constructor Details

#initialize(terminal, input: $stdin, output: $stdout) ⇒ Console

Returns a new instance of Console.

Parameters:

  • terminal (Object)
  • input: (Object) (defaults to: $stdin)
  • output: (Object) (defaults to: $stdout)


7
8
9
# File 'lib/yildun/console.rb', line 7

def initialize(terminal, input: $stdin, output: $stdout)
  @terminal, @input, @output = terminal, input, output
end

Instance Method Details

#render ⇒ Object

Returns:

  • (Object)


34
35
36
37
38
# File 'lib/yildun/console.rb', line 34

def render
  @output.write("\e[H")
  @terminal.screen.lines.each { |line| @output.write("\e[2K#{line}\r\n") }
  @output.flush
end

#run ⇒ Object

Returns:

  • (Object)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/yildun/console.rb', line 11

def run
  return render unless @input.tty? && @output.tty? && @input.respond_to?(:raw)

  @input.raw do
    @output.write("\e[?25l")
    loop do
      resize
      @terminal.pump(timeout: 0.02)
      render
      break unless @terminal.session.alive?
      next unless IO.select([@input], nil, nil, 0.02)

      @terminal.input(@input.read_nonblock(4096))
    rescue IO::WaitReadable
      retry
    rescue EOFError, Errno::EIO
      break
    end
  ensure
    @output.write("\e[0m\e[?25h\e[H\e[2J")
  end
end