parent
2428f78ea4
commit
bc33b1ce31
|
@ -21,22 +21,40 @@ def jobnum(pl, segment_info, show_zero=False):
|
|||
else:
|
||||
return str(jobnum)
|
||||
|
||||
try:
|
||||
import signal
|
||||
exit_codes = dict((k, v) for v, k in reversed(sorted(signal.__dict__.items())) \
|
||||
if v.startswith('SIG') and not v.startswith('SIG_'))
|
||||
except ImportError:
|
||||
exit_codes = dict()
|
||||
|
||||
@requires_segment_info
|
||||
def last_status(pl, segment_info):
|
||||
def last_status(pl, segment_info, signal_names=True):
|
||||
'''Return last exit code.
|
||||
|
||||
:param bool signal_names:
|
||||
If True (default), translate signal numbers to human-readable names.
|
||||
|
||||
Highlight groups used: ``exit_fail``
|
||||
'''
|
||||
if not segment_info['args'].last_exit_code:
|
||||
return None
|
||||
|
||||
try:
|
||||
if signal_names and segment_info['args'].last_exit_code - 128 in exit_codes:
|
||||
return [{'contents': exit_codes[segment_info['args'].last_exit_code - 128], 'highlight_groups': ['exit_fail']}]
|
||||
except TypeError:
|
||||
pass
|
||||
return [{'contents': str(segment_info['args'].last_exit_code), 'highlight_groups': ['exit_fail']}]
|
||||
|
||||
|
||||
@requires_segment_info
|
||||
def last_pipe_status(pl, segment_info):
|
||||
def last_pipe_status(pl, segment_info, signal_names=True):
|
||||
'''Return last pipe status.
|
||||
|
||||
:param bool signal_names:
|
||||
If True (default), translate signal numbers to human-readable names.
|
||||
|
||||
Highlight groups used: ``exit_fail``, ``exit_success``
|
||||
'''
|
||||
last_pipe_status = (
|
||||
|
@ -44,18 +62,22 @@ def last_pipe_status(pl, segment_info):
|
|||
or (segment_info['args'].last_exit_code,)
|
||||
)
|
||||
if any(last_pipe_status):
|
||||
return [
|
||||
{
|
||||
try:
|
||||
return [{
|
||||
'contents': exit_codes[status - 128] if signal_names and \
|
||||
status - 128 in exit_codes else str(status),
|
||||
'highlight_groups': ['exit_fail' if status else 'exit_success'],
|
||||
'draw_inner_divider': True
|
||||
} for status in last_pipe_status]
|
||||
except TypeError:
|
||||
return [{
|
||||
'contents': str(status),
|
||||
'highlight_groups': ['exit_fail' if status else 'exit_success'],
|
||||
'draw_inner_divider': True
|
||||
}
|
||||
for status in last_pipe_status
|
||||
]
|
||||
} for status in last_pipe_status]
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
@requires_segment_info
|
||||
def mode(pl, segment_info, override={'vicmd': 'COMMND', 'viins': 'INSERT'}, default=None):
|
||||
'''Return the current mode.
|
||||
|
|
|
@ -37,6 +37,13 @@ class TestShell(TestCase):
|
|||
self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info), [
|
||||
{'contents': '10', 'highlight_groups': ['exit_fail']}
|
||||
])
|
||||
segment_info['args'].last_exit_code = 137
|
||||
self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info), [
|
||||
{'contents': 'SIGKILL', 'highlight_groups': ['exit_fail']}
|
||||
])
|
||||
self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info, signal_names=False), [
|
||||
{'contents': '137', 'highlight_groups': ['exit_fail']}
|
||||
])
|
||||
segment_info['args'].last_exit_code = 0
|
||||
self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info), None)
|
||||
segment_info['args'].last_exit_code = None
|
||||
|
@ -72,6 +79,19 @@ class TestShell(TestCase):
|
|||
{'contents': '0', 'highlight_groups': ['exit_success'], 'draw_inner_divider': True},
|
||||
{'contents': '0', 'highlight_groups': ['exit_success'], 'draw_inner_divider': True},
|
||||
])
|
||||
segment_info['args'].last_pipe_status = [137, 0, 0]
|
||||
self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info), [
|
||||
{'contents': 'SIGKILL', 'highlight_groups': ['exit_fail'], 'draw_inner_divider': True},
|
||||
{'contents': '0', 'highlight_groups': ['exit_success'], 'draw_inner_divider': True},
|
||||
{'contents': '0', 'highlight_groups': ['exit_success'], 'draw_inner_divider': True},
|
||||
])
|
||||
|
||||
self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info, signal_names=False), [
|
||||
{'contents': '137', 'highlight_groups': ['exit_fail'], 'draw_inner_divider': True},
|
||||
{'contents': '0', 'highlight_groups': ['exit_success'], 'draw_inner_divider': True},
|
||||
{'contents': '0', 'highlight_groups': ['exit_success'], 'draw_inner_divider': True},
|
||||
])
|
||||
|
||||
segment_info['args'].last_pipe_status = [0, 0, 2]
|
||||
self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info), [
|
||||
{'contents': '0', 'highlight_groups': ['exit_success'], 'draw_inner_divider': True},
|
||||
|
|
Loading…
Reference in New Issue