From 54e7fe91ba569fd3bee00364a7310417334e8f79 Mon Sep 17 00:00:00 2001 From: Kenny Root Date: Fri, 10 Jan 2014 14:13:25 -0800 Subject: [PATCH 1/3] Fix common.user test The username was being cached from previous tests, so clear it before running this test. --- tests/test_segments.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_segments.py b/tests/test_segments.py index d74125b9..fea6e55d 100644 --- a/tests/test_segments.py +++ b/tests/test_segments.py @@ -73,6 +73,7 @@ class TestCommon(TestCase): new_psutil = new_module('psutil', Process=lambda pid: Args(username='def')) pl = Pl() with replace_env('USER', 'def') as segment_info: + common.username = False with replace_attr(common, 'os', new_os): with replace_attr(common, 'psutil', new_psutil): with replace_attr(common, '_geteuid', lambda: 5): From c33d56e73cfd447bd261b60da185b4750a68ed28 Mon Sep 17 00:00:00 2001 From: Kenny Root Date: Fri, 10 Jan 2014 13:00:25 -0800 Subject: [PATCH 2/3] 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. --- powerline/segments/common.py | 7 ++++++- tests/test_segments.py | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/powerline/segments/common.py b/powerline/segments/common.py index 4a858d5b..5eda22ca 100644 --- a/powerline/segments/common.py +++ b/powerline/segments/common.py @@ -591,9 +591,12 @@ username = False _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. + :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. 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: pl.warn('Failed to get username') return None + if username == hide_user: + return None euid = _geteuid() return [{ 'contents': username, diff --git a/tests/test_segments.py b/tests/test_segments.py index fea6e55d..03767749 100644 --- a/tests/test_segments.py +++ b/tests/test_segments.py @@ -80,6 +80,10 @@ class TestCommon(TestCase): self.assertEqual(common.user(pl=pl, segment_info=segment_info), [ {'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): self.assertEqual(common.user(pl=pl, segment_info=segment_info), [ {'contents': 'def', 'highlight_group': ['superuser', 'user']} From aa3e86b9d9351359cf9c94a1d94f074695b8b6aa Mon Sep 17 00:00:00 2001 From: ZyX Date: Tue, 14 Jan 2014 23:38:34 +0400 Subject: [PATCH 3/3] Rewrite documentation string --- powerline/segments/common.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/powerline/segments/common.py b/powerline/segments/common.py index 5eda22ca..f6572ce7 100644 --- a/powerline/segments/common.py +++ b/powerline/segments/common.py @@ -595,7 +595,7 @@ def user(pl, segment_info=None, hide_user=None): '''Return the current user. :param str hide_user: - will suppress display of username if it matches the given string + Omit showing segment for users with names equal to this string. Highlights the user with the ``superuser`` if the effective user ID is 0.