Module: Yamatanooroti::VTermTestCaseModule

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

Instance Method Summary collapse

Instance Method Details

#assert_screen(expected_lines) ⇒ Object



100
101
102
103
104
105
106
107
108
# File 'lib/yamatanooroti/vterm.rb', line 100

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



48
49
50
51
52
53
54
# File 'lib/yamatanooroti/vterm.rb', line 48

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

#resultObject



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

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
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/yamatanooroti/vterm.rb', line 31

def write(str)
  sync
  str_to_write = String.new(encoding: Encoding::ASCII_8BIT)
  str.chars.each do |c|
    byte = c.force_encoding(Encoding::ASCII_8BIT).ord
    if c.bytesize == 1 and byte.allbits?(0x80) # with Meta key
      c = (byte ^ 0x80).chr
      str_to_write << "\e"
      str_to_write << c
    else
      str_to_write << c
    end
  end
  @pty_input.write(str_to_write)
  sync
end