| 1 |
import time |
| 2 |
|
| 3 |
def strip_lines(string): |
| 4 |
return '\n'.join(s.rstrip() for s in string.split('\n')).rstrip() |
| 5 |
|
| 6 |
class TerminalBase: |
| 7 |
def get_output_stripped(self): |
| 8 |
return strip_lines(self.get_output()) |
| 9 |
|
| 10 |
def clear_screen(self): |
| 11 |
result = '' |
| 12 |
while result != '>': |
| 13 |
# Cancel potentially running task |
| 14 |
self.send_ctrl("c") |
| 15 |
# Clear current command line, as fish does not do it on ^C |
| 16 |
self.send_ctrl("u") |
| 17 |
self.wait_for_last_line('>', 5, 0.01) |
| 18 |
self.send('clear') |
| 19 |
self.wait_for_last_line('> clear', 5, 0.01) |
| 20 |
self.send_line('') |
| 21 |
result = self.wait_for_text('>', 5, 0.01) |
| 22 |
|
| 23 |
def complete(self, commandline, num_tabs=1, wait=5, expected=None, fast=False): |
| 24 |
self.clear_screen() |
| 25 |
self.send(commandline) |
| 26 |
|
| 27 |
for _ in range(num_tabs): |
| 28 |
self.send_tab() |
| 29 |
|
| 30 |
if fast: |
| 31 |
return self.wait_for_text(expected, wait, 0.01) |
| 32 |
else: |
| 33 |
time.sleep(wait) |
| 34 |
return self.get_output_stripped() |
| 35 |
|
| 36 |
def wait_for_text(self, expected, timeout, poll_interval): |
| 37 |
expected = strip_lines(expected) |
| 38 |
|
| 39 |
while timeout > 0.0: |
| 40 |
result = self.get_output_stripped() |
| 41 |
if result == expected: |
| 42 |
return result |
| 43 |
timeout -= poll_interval |
| 44 |
time.sleep(poll_interval) |
| 45 |
return self.get_output_stripped() |
| 46 |
|
| 47 |
def wait_for_last_line(self, expected, timeout, poll_interval): |
| 48 |
expected = expected.rstrip() |
| 49 |
|
| 50 |
while timeout > 0.0: |
| 51 |
result = self.get_output_stripped().split('\n')[-1] |
| 52 |
if result == expected: |
| 53 |
return True |
| 54 |
timeout -= poll_interval |
| 55 |
time.sleep(poll_interval) |
| 56 |
return False |