Module: Yamatanooroti::VTermTestCaseModule

Included in:
VTermTestCase
Defined in:
lib/yamatanooroti/vterm.rb

Instance Method Summary collapse

Instance Method Details

#assert_screen(expected_lines) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/yamatanooroti/vterm.rb', line 89

def assert_screen(expected_lines)
  actual_lines = result
  case expected_lines
  when Array
    assert_equal(expected_lines, actual_lines)
  when String
    assert_equal(expected_lines, actual_lines.join("\n").sub(/\n*\z/, "\n"))
  end
end

#closeObject



37
38
39
40
41
42
43
# File 'lib/yamatanooroti/vterm.rb', line 37

def close
  sync
  @pty_input.close
  sync
  Process.kill('KILL', @pid)
  Process.waitpid(@pid)
end

#resultObject



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/yamatanooroti/vterm.rb', line 74

def result
  return @result if @result
  @result = []
  rows, cols = @vterm.size
  rows.times do |r|
    @result << ''
    cols.times do |c|
      cell = @screen.cell_at(r, c)
      @result.last << cell.char if cell.char
    end
    @result.last.gsub!(/ *$/, '')
  end
  @result
end

#start_terminal(height, width, command, wait: 0.1, startup_message: nil) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/yamatanooroti/vterm.rb', line 7

def start_terminal(height, width, command, wait: 0.1, startup_message: nil)
  @wait = wait
  @result = nil

  @pty_output, @pty_input, @pid = PTY.spawn('bash', '-c', %[stty rows #{height.to_s} cols #{width.to_s}; "$@"], '--', *command)

  @vterm = VTerm.new(height, width)
  @vterm.set_utf8(true)

  @screen = @vterm.screen
  @screen.reset(true)

  case startup_message
  when String
    @startup_message = ->(message) { message.start_with?(startup_message) }
  when Regexp
    @startup_message = ->(message) { startup_message.match?(message) }
  else
    @startup_message = nil
  end

  sync
end

#write(str) ⇒ Object



31
32
33
34
35
# File 'lib/yamatanooroti/vterm.rb', line 31

def write(str)
  sync
  @pty_input.write(str)
  sync
end