mirror of
https://github.com/powerline/powerline.git
synced 2025-07-25 14:54:54 +02:00
Improve terminal.py:
1. Make it print true color escape sequences when debugging. 2. Add support for automatic row detection.
This commit is contained in:
parent
6bbcb610c7
commit
870de7be21
@ -111,21 +111,33 @@ class ExpectProcess(threading.Thread):
|
|||||||
with self.child_lock:
|
with self.child_lock:
|
||||||
self.child.send(data)
|
self.child.send(data)
|
||||||
|
|
||||||
def get_highlighted_text(self, text, attrs, default_props=()):
|
def get_highlighted_text(self, text, attrs, default_props=(),
|
||||||
|
use_escapes=False):
|
||||||
ret = []
|
ret = []
|
||||||
new_attrs = attrs.copy()
|
new_attrs = attrs.copy()
|
||||||
for cell_properties, segment_text in text:
|
for cell_properties, segment_text in text:
|
||||||
segment_text = segment_text.translate({'{': '{{', '}': '}}'})
|
if use_escapes:
|
||||||
if cell_properties not in new_attrs:
|
escapes = ('\033[38;2;{0};{1};{2};48;2;{3};{4};{5}'.format(
|
||||||
new_attrs[cell_properties] = len(new_attrs) + 1
|
*(cell_properties[0] + cell_properties[1]))) + (
|
||||||
props_name = new_attrs[cell_properties]
|
';1' if cell_properties[2] else ''
|
||||||
if props_name in default_props:
|
) + (
|
||||||
ret.append(segment_text)
|
';3' if cell_properties[3] else ''
|
||||||
|
) + (
|
||||||
|
';4' if cell_properties[4] else ''
|
||||||
|
) + 'm'
|
||||||
|
ret.append(escapes + segment_text + '\033[0m')
|
||||||
else:
|
else:
|
||||||
ret.append('{' + str(props_name) + ':' + segment_text + '}')
|
segment_text = segment_text.translate({'{': '{{', '}': '}}'})
|
||||||
|
if cell_properties not in new_attrs:
|
||||||
|
new_attrs[cell_properties] = len(new_attrs) + 1
|
||||||
|
props_name = new_attrs[cell_properties]
|
||||||
|
if props_name in default_props:
|
||||||
|
ret.append(segment_text)
|
||||||
|
else:
|
||||||
|
ret.append('{' + str(props_name) + ':' + segment_text + '}')
|
||||||
return ''.join(ret), new_attrs
|
return ''.join(ret), new_attrs
|
||||||
|
|
||||||
def get_row(self, row, attrs, default_props=()):
|
def get_row(self, row, attrs, default_props=(), use_escapes=False):
|
||||||
with self.lock:
|
with self.lock:
|
||||||
return self.get_highlighted_text((
|
return self.get_highlighted_text((
|
||||||
(key, ''.join((cell.text for cell in subline)))
|
(key, ''.join((cell.text for cell in subline)))
|
||||||
@ -133,34 +145,48 @@ class ExpectProcess(threading.Thread):
|
|||||||
self.vterm.vtscreen[row, col]
|
self.vterm.vtscreen[row, col]
|
||||||
for col in range(self.dim.cols)
|
for col in range(self.dim.cols)
|
||||||
), lambda cell: cell.cell_properties_key)
|
), lambda cell: cell.cell_properties_key)
|
||||||
), attrs, default_props)
|
), attrs, default_props, use_escapes)
|
||||||
|
|
||||||
def get_screen(self, attrs, default_props=()):
|
def get_screen(self, attrs, default_props=(), use_escapes=False):
|
||||||
lines = []
|
lines = []
|
||||||
for row in range(self.dim.rows):
|
for row in range(self.dim.rows):
|
||||||
line, attrs = self.get_row(row, attrs, default_props)
|
line, attrs = self.get_row(row, attrs, default_props, use_escapes)
|
||||||
lines.append(line)
|
lines.append(line)
|
||||||
return '\n'.join(lines), attrs
|
return '\n'.join(lines), attrs
|
||||||
|
|
||||||
|
|
||||||
def test_expected_result(p, test, last_attempt, last_attempt_cb, attempts):
|
def test_expected_result(p, test, last_attempt, last_attempt_cb, attempts):
|
||||||
|
debugging_tests = not not os.environ.get('_POWERLINE_DEBUGGING_TESTS')
|
||||||
expected_text, attrs = test['expected_result']
|
expected_text, attrs = test['expected_result']
|
||||||
result = None
|
result = None
|
||||||
while attempts:
|
while attempts:
|
||||||
actual_text, all_attrs = p.get_row(test['row'], attrs)
|
if 'row' in test:
|
||||||
|
row = test['row']
|
||||||
|
else:
|
||||||
|
row = p.dim.rows - 1
|
||||||
|
while row >= 0 and not p[row, 0].text:
|
||||||
|
row -= 1
|
||||||
|
if row < 0:
|
||||||
|
row = 0
|
||||||
|
actual_text, all_attrs = p.get_row(row, attrs)
|
||||||
if actual_text == expected_text:
|
if actual_text == expected_text:
|
||||||
return True
|
return True
|
||||||
attempts -= 1
|
attempts -= 1
|
||||||
print('Actual result does not match expected. Attempts left: {0}.'.format(attempts))
|
print('Actual result does not match expected for row {0}. Attempts left: {1}.'.format(
|
||||||
|
row, attempts))
|
||||||
sleep(2)
|
sleep(2)
|
||||||
print('Result:')
|
print('Result (row {0}):'.format(row))
|
||||||
print(actual_text)
|
print(actual_text)
|
||||||
print('Expected:')
|
print('Expected:')
|
||||||
print(expected_text)
|
print(expected_text)
|
||||||
print('Attributes:')
|
print('Attributes:')
|
||||||
print(all_attrs)
|
for v, k in sorted(
|
||||||
|
((v, k) for k, v in all_attrs.items()),
|
||||||
|
key=(lambda t: '%02u'.format(t[0]) if isinstance(t[0], int) else t[0]),
|
||||||
|
):
|
||||||
|
print('{k!r}: {v!r},'.format(v=v, k=k))
|
||||||
print('Screen:')
|
print('Screen:')
|
||||||
screen, screen_attrs = p.get_screen(attrs)
|
screen, screen_attrs = p.get_screen(attrs, use_escapes=debugging_tests)
|
||||||
print(screen)
|
print(screen)
|
||||||
print(screen_attrs)
|
print(screen_attrs)
|
||||||
print('_' * 80)
|
print('_' * 80)
|
||||||
@ -223,7 +249,7 @@ def get_env(vterm_path, test_dir, *args, **kwargs):
|
|||||||
def do_terminal_tests(tests, cmd, dim, args, env, suite, cwd=None, fin_cb=None,
|
def do_terminal_tests(tests, cmd, dim, args, env, suite, cwd=None, fin_cb=None,
|
||||||
last_attempt_cb=None, attempts=None):
|
last_attempt_cb=None, attempts=None):
|
||||||
debugging_tests = not not os.environ.get('_POWERLINE_DEBUGGING_TESTS')
|
debugging_tests = not not os.environ.get('_POWERLINE_DEBUGGING_TESTS')
|
||||||
default_attempts = 1 if debugging_tests else 3
|
default_attempts = 2 if debugging_tests else 3
|
||||||
if attempts is None:
|
if attempts is None:
|
||||||
attempts = default_attempts
|
attempts = default_attempts
|
||||||
lib = os.environ.get('POWERLINE_LIBVTERM')
|
lib = os.environ.get('POWERLINE_LIBVTERM')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user