Add arg to optionally suppress user segment
This adds the option to common.user to suppress display if the username matches the given string in the new "hide_user" argument.
This commit is contained in:
parent
54e7fe91ba
commit
c33d56e73c
|
@ -591,9 +591,12 @@ username = False
|
||||||
_geteuid = getattr(os, 'geteuid', lambda: 1)
|
_geteuid = getattr(os, 'geteuid', lambda: 1)
|
||||||
|
|
||||||
|
|
||||||
def user(pl, segment_info=None):
|
def user(pl, segment_info=None, hide_user=None):
|
||||||
'''Return the current user.
|
'''Return the current user.
|
||||||
|
|
||||||
|
:param str hide_user:
|
||||||
|
will suppress display of username if it matches the given string
|
||||||
|
|
||||||
Highlights the user with the ``superuser`` if the effective user ID is 0.
|
Highlights the user with the ``superuser`` if the effective user ID is 0.
|
||||||
|
|
||||||
Highlight groups used: ``superuser`` or ``user``. It is recommended to define all highlight groups.
|
Highlight groups used: ``superuser`` or ``user``. It is recommended to define all highlight groups.
|
||||||
|
@ -604,6 +607,8 @@ def user(pl, segment_info=None):
|
||||||
if username is None:
|
if username is None:
|
||||||
pl.warn('Failed to get username')
|
pl.warn('Failed to get username')
|
||||||
return None
|
return None
|
||||||
|
if username == hide_user:
|
||||||
|
return None
|
||||||
euid = _geteuid()
|
euid = _geteuid()
|
||||||
return [{
|
return [{
|
||||||
'contents': username,
|
'contents': username,
|
||||||
|
|
|
@ -80,6 +80,10 @@ class TestCommon(TestCase):
|
||||||
self.assertEqual(common.user(pl=pl, segment_info=segment_info), [
|
self.assertEqual(common.user(pl=pl, segment_info=segment_info), [
|
||||||
{'contents': 'def', 'highlight_group': 'user'}
|
{'contents': 'def', 'highlight_group': 'user'}
|
||||||
])
|
])
|
||||||
|
self.assertEqual(common.user(pl=pl, segment_info=segment_info, hide_user='abc'), [
|
||||||
|
{'contents': 'def', 'highlight_group': 'user'}
|
||||||
|
])
|
||||||
|
self.assertEqual(common.user(pl=pl, segment_info=segment_info, hide_user='def'), None)
|
||||||
with replace_attr(common, '_geteuid', lambda: 0):
|
with replace_attr(common, '_geteuid', lambda: 0):
|
||||||
self.assertEqual(common.user(pl=pl, segment_info=segment_info), [
|
self.assertEqual(common.user(pl=pl, segment_info=segment_info), [
|
||||||
{'contents': 'def', 'highlight_group': ['superuser', 'user']}
|
{'contents': 'def', 'highlight_group': ['superuser', 'user']}
|
||||||
|
|
Loading…
Reference in New Issue