Class: Echoes::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/echoes/terminal.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command: Echoes.config.shell, rows: nil, cols: nil) ⇒ Terminal

Returns a new instance of Terminal.



10
11
12
13
14
15
16
17
# File 'lib/echoes/terminal.rb', line 10

def initialize(command: Echoes.config.shell, rows: nil, cols: nil)
  size = IO.console&.winsize || [24, 80]
  @rows = rows || size[0]
  @cols = cols || size[1]
  @command = command
  @screen = Screen.new(rows: @rows, cols: @cols)
  @parser = Parser.new(@screen, writer: ->(s) { @write_io&.write(s) rescue nil })
end

Instance Attribute Details

#screenObject (readonly)

Returns the value of attribute screen.



8
9
10
# File 'lib/echoes/terminal.rb', line 8

def screen
  @screen
end

Instance Method Details

#runObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/echoes/terminal.rb', line 19

def run
  PTY.spawn(@command) do |read_io, write_io, pid|
    @read_io = read_io
    @write_io = write_io
    @pid = pid

    @read_io.winsize = [@rows, @cols]

    setup_signal_handlers

    STDIN.raw do
      reader = Thread.new { read_loop }
      write_loop
      reader.kill
    end
  end
end