mirror of
https://github.com/powerline/powerline.git
synced 2025-07-21 12:54:58 +02:00
Replace pl.environ/getcwd/home with segment_info
This commit is contained in:
parent
854216810e
commit
3ebc16a48c
@ -64,10 +64,7 @@ def load_json_config(config_file_path, load=json.load, open_file=open_file):
|
|||||||
|
|
||||||
|
|
||||||
class PowerlineState(object):
|
class PowerlineState(object):
|
||||||
def __init__(self, use_daemon_threads, logger, ext, environ, getcwd, home):
|
def __init__(self, use_daemon_threads, logger, ext):
|
||||||
self.environ = environ
|
|
||||||
self.getcwd = getcwd
|
|
||||||
self.home = home or environ.get('HOME', None)
|
|
||||||
self.logger = logger
|
self.logger = logger
|
||||||
self.ext = ext
|
self.ext = ext
|
||||||
self.use_daemon_threads = use_daemon_threads
|
self.use_daemon_threads = use_daemon_threads
|
||||||
@ -124,14 +121,6 @@ class Powerline(object):
|
|||||||
during python session.
|
during python session.
|
||||||
:param Logger logger:
|
:param Logger logger:
|
||||||
If present, no new logger will be created and this logger will be used.
|
If present, no new logger will be created and this logger will be used.
|
||||||
:param dict environ:
|
|
||||||
Object with ``.__getitem__`` and ``.get`` methods used to obtain
|
|
||||||
environment variables. Defaults to ``os.environ``.
|
|
||||||
:param func getcwd:
|
|
||||||
Function used to get current working directory. Defaults to
|
|
||||||
``os.getcwdu`` or ``os.getcwd``.
|
|
||||||
:param str home:
|
|
||||||
Home directory. Defaults to ``environ.get('HOME')``.
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
@ -139,18 +128,12 @@ class Powerline(object):
|
|||||||
renderer_module=None,
|
renderer_module=None,
|
||||||
run_once=False,
|
run_once=False,
|
||||||
logger=None,
|
logger=None,
|
||||||
use_daemon_threads=True,
|
use_daemon_threads=True):
|
||||||
environ=os.environ,
|
|
||||||
getcwd=getattr(os, 'getcwdu', os.getcwd),
|
|
||||||
home=None):
|
|
||||||
global watcher
|
global watcher
|
||||||
self.ext = ext
|
self.ext = ext
|
||||||
self.renderer_module = renderer_module or ext
|
self.renderer_module = renderer_module or ext
|
||||||
self.run_once = run_once
|
self.run_once = run_once
|
||||||
self.logger = logger
|
self.logger = logger
|
||||||
self.environ = environ
|
|
||||||
self.getcwd = getcwd
|
|
||||||
self.home = home
|
|
||||||
self.use_daemon_threads = use_daemon_threads
|
self.use_daemon_threads = use_daemon_threads
|
||||||
|
|
||||||
if '.' not in self.renderer_module:
|
if '.' not in self.renderer_module:
|
||||||
@ -221,7 +204,7 @@ class Powerline(object):
|
|||||||
self.logger.setLevel(level)
|
self.logger.setLevel(level)
|
||||||
self.logger.addHandler(handler)
|
self.logger.addHandler(handler)
|
||||||
|
|
||||||
self.pl = PowerlineState(self.use_daemon_threads, self.logger, self.ext, self.environ, self.getcwd, self.home)
|
self.pl = PowerlineState(self.use_daemon_threads, self.logger, self.ext)
|
||||||
|
|
||||||
self.renderer_options.update(
|
self.renderer_options.update(
|
||||||
pl=self.pl,
|
pl=self.pl,
|
||||||
|
@ -77,6 +77,9 @@ class Environment(object):
|
|||||||
return default
|
return default
|
||||||
|
|
||||||
|
|
||||||
|
environ = Environment()
|
||||||
|
|
||||||
|
|
||||||
class Prompt(object):
|
class Prompt(object):
|
||||||
__slots__ = ('powerline', 'side', 'savedpsvar', 'savedps', 'args')
|
__slots__ = ('powerline', 'side', 'savedpsvar', 'savedps', 'args')
|
||||||
|
|
||||||
@ -88,7 +91,11 @@ class Prompt(object):
|
|||||||
self.args = powerline.args
|
self.args = powerline.args
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
r = self.powerline.render(width=zsh.columns(), side=self.side, segment_info=self.args)
|
r = self.powerline.render(
|
||||||
|
width=zsh.columns(),
|
||||||
|
side=self.side,
|
||||||
|
segment_info={'args': self.args, 'environ': environ}
|
||||||
|
)
|
||||||
if type(r) is not str:
|
if type(r) is not str:
|
||||||
if type(r) is bytes:
|
if type(r) is bytes:
|
||||||
return r.decode('utf-8')
|
return r.decode('utf-8')
|
||||||
@ -113,8 +120,7 @@ def set_prompt(powerline, psvar, side):
|
|||||||
|
|
||||||
|
|
||||||
def setup():
|
def setup():
|
||||||
environ = Environment()
|
powerline = ShellPowerline(Args())
|
||||||
powerline = ShellPowerline(Args(), environ=environ, getcwd=lambda: environ['PWD'])
|
|
||||||
used_powerlines.append(powerline)
|
used_powerlines.append(powerline)
|
||||||
used_powerlines.append(powerline)
|
used_powerlines.append(powerline)
|
||||||
set_prompt(powerline, 'PS1', 'left')
|
set_prompt(powerline, 'PS1', 'left')
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
from powerline.theme import Theme
|
from powerline.theme import Theme
|
||||||
from unicodedata import east_asian_width, combining
|
from unicodedata import east_asian_width, combining
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@ -18,7 +19,19 @@ def construct_returned_value(rendered_highlighted, segments, output_raw):
|
|||||||
|
|
||||||
|
|
||||||
class Renderer(object):
|
class Renderer(object):
|
||||||
def __init__(self, theme_config, local_themes, theme_kwargs, colorscheme, pl, **options):
|
segment_info = {
|
||||||
|
'environ': os.environ,
|
||||||
|
'getcwd': getattr(os, 'getcwdu', os.getcwd),
|
||||||
|
'home': os.environ.get('HOME'),
|
||||||
|
}
|
||||||
|
|
||||||
|
def __init__(self,
|
||||||
|
theme_config,
|
||||||
|
local_themes,
|
||||||
|
theme_kwargs,
|
||||||
|
colorscheme,
|
||||||
|
pl,
|
||||||
|
**options):
|
||||||
self.__dict__.update(options)
|
self.__dict__.update(options)
|
||||||
self.theme_config = theme_config
|
self.theme_config = theme_config
|
||||||
theme_kwargs['pl'] = pl
|
theme_kwargs['pl'] = pl
|
||||||
@ -53,6 +66,9 @@ class Renderer(object):
|
|||||||
segment['divider_highlight'] = None
|
segment['divider_highlight'] = None
|
||||||
return segment
|
return segment
|
||||||
|
|
||||||
|
def get_segment_info(self, segment_info):
|
||||||
|
return segment_info or self.segment_info
|
||||||
|
|
||||||
def render(self, mode=None, width=None, side=None, output_raw=False, segment_info=None, matcher_info=None):
|
def render(self, mode=None, width=None, side=None, output_raw=False, segment_info=None, matcher_info=None):
|
||||||
'''Render all segments.
|
'''Render all segments.
|
||||||
|
|
||||||
@ -63,7 +79,7 @@ class Renderer(object):
|
|||||||
reached.
|
reached.
|
||||||
'''
|
'''
|
||||||
theme = self.get_theme(matcher_info)
|
theme = self.get_theme(matcher_info)
|
||||||
segments = theme.get_segments(side, segment_info)
|
segments = theme.get_segments(side, self.get_segment_info(segment_info))
|
||||||
|
|
||||||
# Handle excluded/included segments for the current mode
|
# Handle excluded/included segments for the current mode
|
||||||
segments = [self.get_highlighting(segment, mode) for segment in segments
|
segments = [self.get_highlighting(segment, mode) for segment in segments
|
||||||
|
@ -9,6 +9,11 @@ class IpythonRenderer(ShellRenderer):
|
|||||||
escape_hl_start = '\x01'
|
escape_hl_start = '\x01'
|
||||||
escape_hl_end = '\x02'
|
escape_hl_end = '\x02'
|
||||||
|
|
||||||
|
def get_segment_info(self, segment_info):
|
||||||
|
r = self.segment_info.copy()
|
||||||
|
r['ipython'] = segment_info
|
||||||
|
return r
|
||||||
|
|
||||||
def get_theme(self, matcher_info):
|
def get_theme(self, matcher_info):
|
||||||
if matcher_info == 'in':
|
if matcher_info == 'in':
|
||||||
return self.theme
|
return self.theme
|
||||||
|
@ -19,6 +19,13 @@ class ShellRenderer(Renderer):
|
|||||||
tmux_escape = False
|
tmux_escape = False
|
||||||
screen_escape = False
|
screen_escape = False
|
||||||
|
|
||||||
|
def get_segment_info(self, segment_info):
|
||||||
|
r = self.segment_info.copy()
|
||||||
|
r.update(segment_info)
|
||||||
|
if 'PWD' in r['environ']:
|
||||||
|
r['getcwd'] = lambda: r['environ']['PWD']
|
||||||
|
return r
|
||||||
|
|
||||||
def hlstyle(self, fg=None, bg=None, attr=None):
|
def hlstyle(self, fg=None, bg=None, attr=None):
|
||||||
'''Highlight a segment.
|
'''Highlight a segment.
|
||||||
|
|
||||||
|
@ -79,8 +79,14 @@ class VimRenderer(Renderer):
|
|||||||
}
|
}
|
||||||
segment_info['buffer'] = segment_info['window'].buffer
|
segment_info['buffer'] = segment_info['window'].buffer
|
||||||
segment_info['bufnr'] = segment_info['buffer'].number
|
segment_info['bufnr'] = segment_info['buffer'].number
|
||||||
|
segment_info.update(self.segment_info)
|
||||||
winwidth = segment_info['window'].width
|
winwidth = segment_info['window'].width
|
||||||
statusline = super(VimRenderer, self).render(mode, winwidth, segment_info=segment_info, matcher_info=segment_info)
|
statusline = super(VimRenderer, self).render(
|
||||||
|
mode=mode,
|
||||||
|
width=winwidth,
|
||||||
|
segment_info=segment_info,
|
||||||
|
matcher_info=segment_info,
|
||||||
|
)
|
||||||
return statusline
|
return statusline
|
||||||
|
|
||||||
def reset_highlight(self):
|
def reset_highlight(self):
|
||||||
|
@ -15,29 +15,32 @@ from powerline.lib.vcs import guess
|
|||||||
from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment, with_docstring
|
from powerline.lib.threaded import ThreadedSegment, KwThreadedSegment, with_docstring
|
||||||
from powerline.lib.time import monotonic
|
from powerline.lib.time import monotonic
|
||||||
from powerline.lib.humanize_bytes import humanize_bytes
|
from powerline.lib.humanize_bytes import humanize_bytes
|
||||||
|
from powerline.theme import requires_segment_info
|
||||||
from collections import namedtuple
|
from collections import namedtuple
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
|
||||||
|
|
||||||
def hostname(pl, only_if_ssh=False):
|
@requires_segment_info
|
||||||
|
def hostname(pl, segment_info, only_if_ssh=False):
|
||||||
'''Return the current hostname.
|
'''Return the current hostname.
|
||||||
|
|
||||||
:param bool only_if_ssh:
|
:param bool only_if_ssh:
|
||||||
only return the hostname if currently in an SSH session
|
only return the hostname if currently in an SSH session
|
||||||
'''
|
'''
|
||||||
if only_if_ssh and not pl.environ.get('SSH_CLIENT'):
|
if only_if_ssh and not segment_info['environ'].get('SSH_CLIENT'):
|
||||||
return None
|
return None
|
||||||
return socket.gethostname()
|
return socket.gethostname()
|
||||||
|
|
||||||
|
|
||||||
|
@requires_segment_info
|
||||||
class RepositorySegment(KwThreadedSegment):
|
class RepositorySegment(KwThreadedSegment):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
super(RepositorySegment, self).__init__()
|
super(RepositorySegment, self).__init__()
|
||||||
self.directories = {}
|
self.directories = {}
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def key(pl, **kwargs):
|
def key(segment_info, **kwargs):
|
||||||
return os.path.abspath(pl.getcwd())
|
return os.path.abspath(segment_info['getcwd']())
|
||||||
|
|
||||||
def update(self, *args):
|
def update(self, *args):
|
||||||
# .compute_state() is running only in this method, and only in one
|
# .compute_state() is running only in this method, and only in one
|
||||||
@ -110,7 +113,8 @@ Highlight groups used: ``branch_clean``, ``branch_dirty``, ``branch``.
|
|||||||
''')
|
''')
|
||||||
|
|
||||||
|
|
||||||
def cwd(pl, dir_shorten_len=None, dir_limit_depth=None):
|
@requires_segment_info
|
||||||
|
def cwd(pl, segment_info, dir_shorten_len=None, dir_limit_depth=None):
|
||||||
'''Return the current working directory.
|
'''Return the current working directory.
|
||||||
|
|
||||||
Returns a segment list to create a breadcrumb-like effect.
|
Returns a segment list to create a breadcrumb-like effect.
|
||||||
@ -127,7 +131,7 @@ def cwd(pl, dir_shorten_len=None, dir_limit_depth=None):
|
|||||||
'''
|
'''
|
||||||
import re
|
import re
|
||||||
try:
|
try:
|
||||||
cwd = pl.getcwd()
|
cwd = segment_info['getcwd']()
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
if e.errno == 2:
|
if e.errno == 2:
|
||||||
# user most probably deleted the directory
|
# user most probably deleted the directory
|
||||||
@ -136,7 +140,7 @@ def cwd(pl, dir_shorten_len=None, dir_limit_depth=None):
|
|||||||
cwd = "[not found]"
|
cwd = "[not found]"
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
home = pl.home
|
home = segment_info['home']
|
||||||
if home:
|
if home:
|
||||||
cwd = re.sub('^' + re.escape(home), '~', cwd, 1)
|
cwd = re.sub('^' + re.escape(home), '~', cwd, 1)
|
||||||
cwd_split = cwd.split(os.sep)
|
cwd_split = cwd.split(os.sep)
|
||||||
@ -540,7 +544,7 @@ try:
|
|||||||
if data:
|
if data:
|
||||||
yield interface, data.bytes_recv, data.bytes_sent
|
yield interface, data.bytes_recv, data.bytes_sent
|
||||||
|
|
||||||
def _get_user(pl):
|
def _get_user(segment_info):
|
||||||
return psutil.Process(os.getpid()).username
|
return psutil.Process(os.getpid()).username
|
||||||
|
|
||||||
def cpu_load_percent(pl, measure_interval=.5):
|
def cpu_load_percent(pl, measure_interval=.5):
|
||||||
@ -567,8 +571,8 @@ except ImportError:
|
|||||||
if x is not None:
|
if x is not None:
|
||||||
yield interface, x[0], x[1]
|
yield interface, x[0], x[1]
|
||||||
|
|
||||||
def _get_user(pl): # NOQA
|
def _get_user(segment_info): # NOQA
|
||||||
return pl.environ.get('USER', None)
|
return segment_info['environ'].get('USER', None)
|
||||||
|
|
||||||
def cpu_load_percent(pl, measure_interval=.5): # NOQA
|
def cpu_load_percent(pl, measure_interval=.5): # NOQA
|
||||||
'''Return the average CPU load as a percentage.
|
'''Return the average CPU load as a percentage.
|
||||||
@ -587,7 +591,7 @@ username = False
|
|||||||
_geteuid = getattr(os, 'geteuid', lambda: 1)
|
_geteuid = getattr(os, 'geteuid', lambda: 1)
|
||||||
|
|
||||||
|
|
||||||
def user(pl):
|
def user(pl, segment_info=None):
|
||||||
'''Return the current user.
|
'''Return the current user.
|
||||||
|
|
||||||
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.
|
||||||
@ -605,6 +609,8 @@ def user(pl):
|
|||||||
'contents': username,
|
'contents': username,
|
||||||
'highlight_group': 'user' if euid != 0 else ['superuser', 'user'],
|
'highlight_group': 'user' if euid != 0 else ['superuser', 'user'],
|
||||||
}]
|
}]
|
||||||
|
if 'psutil' in globals():
|
||||||
|
user = requires_segment_info(user)
|
||||||
|
|
||||||
|
|
||||||
if os.path.exists('/proc/uptime'):
|
if os.path.exists('/proc/uptime'):
|
||||||
@ -765,9 +771,10 @@ Highlight groups used: ``network_load_sent_gradient`` (gradient) or ``network_lo
|
|||||||
''')
|
''')
|
||||||
|
|
||||||
|
|
||||||
def virtualenv(pl):
|
@requires_segment_info
|
||||||
|
def virtualenv(pl, segment_info):
|
||||||
'''Return the name of the current Python virtualenv.'''
|
'''Return the name of the current Python virtualenv.'''
|
||||||
return os.path.basename(pl.environ.get('VIRTUAL_ENV', '')) or None
|
return os.path.basename(segment_info['environ'].get('VIRTUAL_ENV', '')) or None
|
||||||
|
|
||||||
|
|
||||||
_IMAPKey = namedtuple('Key', 'username password server port folder')
|
_IMAPKey = namedtuple('Key', 'username password server port folder')
|
||||||
|
@ -5,4 +5,4 @@ from powerline.theme import requires_segment_info
|
|||||||
|
|
||||||
@requires_segment_info
|
@requires_segment_info
|
||||||
def prompt_count(pl, segment_info):
|
def prompt_count(pl, segment_info):
|
||||||
return str(segment_info.prompt_count)
|
return str(segment_info['ipython'].prompt_count)
|
||||||
|
@ -9,9 +9,9 @@ def last_status(pl, segment_info):
|
|||||||
|
|
||||||
Highlight groups used: ``exit_fail``
|
Highlight groups used: ``exit_fail``
|
||||||
'''
|
'''
|
||||||
if not segment_info.last_exit_code:
|
if not segment_info['args'].last_exit_code:
|
||||||
return None
|
return None
|
||||||
return [{'contents': str(segment_info.last_exit_code), 'highlight_group': 'exit_fail'}]
|
return [{'contents': str(segment_info['args'].last_exit_code), 'highlight_group': 'exit_fail'}]
|
||||||
|
|
||||||
|
|
||||||
@requires_segment_info
|
@requires_segment_info
|
||||||
@ -20,8 +20,9 @@ def last_pipe_status(pl, segment_info):
|
|||||||
|
|
||||||
Highlight groups used: ``exit_fail``, ``exit_success``
|
Highlight groups used: ``exit_fail``, ``exit_success``
|
||||||
'''
|
'''
|
||||||
if any(segment_info.last_pipe_status):
|
last_pipe_status = segment_info['args'].last_pipe_status
|
||||||
|
if any(last_pipe_status):
|
||||||
return [{"contents": str(status), "highlight_group": "exit_fail" if status else "exit_success"}
|
return [{"contents": str(status), "highlight_group": "exit_fail" if status else "exit_success"}
|
||||||
for status in segment_info.last_pipe_status]
|
for status in last_pipe_status]
|
||||||
else:
|
else:
|
||||||
return None
|
return None
|
||||||
|
@ -13,11 +13,12 @@ except ImportError:
|
|||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
args = get_argparser(description=__doc__).parse_args()
|
args = get_argparser(description=__doc__).parse_args()
|
||||||
kwargs = {}
|
|
||||||
if 'PWD' in os.environ:
|
|
||||||
kwargs['getcwd'] = lambda: os.environ['PWD']
|
|
||||||
powerline = ShellPowerline(args, run_once=True, **kwargs)
|
powerline = ShellPowerline(args, run_once=True, **kwargs)
|
||||||
rendered = powerline.render(width=args.width, side=args.side, segment_info=args)
|
rendered = powerline.render(
|
||||||
|
width=args.width,
|
||||||
|
side=args.side,
|
||||||
|
segment_info={'args': args, 'environ': os.environ},
|
||||||
|
)
|
||||||
try:
|
try:
|
||||||
sys.stdout.write(rendered)
|
sys.stdout.write(rendered)
|
||||||
except UnicodeEncodeError:
|
except UnicodeEncodeError:
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
# vim:fileencoding=utf-8:noet
|
# vim:fileencoding=utf-8:noet
|
||||||
import imp
|
import imp
|
||||||
import sys
|
import sys
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
class Pl(object):
|
class Pl(object):
|
||||||
@ -8,18 +9,9 @@ class Pl(object):
|
|||||||
self.errors = []
|
self.errors = []
|
||||||
self.warns = []
|
self.warns = []
|
||||||
self.debugs = []
|
self.debugs = []
|
||||||
self._cwd = None
|
|
||||||
self.prefix = None
|
self.prefix = None
|
||||||
self.environ = {}
|
|
||||||
self.home = None
|
|
||||||
self.use_daemon_threads = True
|
self.use_daemon_threads = True
|
||||||
|
|
||||||
def getcwd(self):
|
|
||||||
if isinstance(self._cwd, Exception):
|
|
||||||
raise self._cwd
|
|
||||||
else:
|
|
||||||
return self._cwd
|
|
||||||
|
|
||||||
for meth in ('error', 'warn', 'debug'):
|
for meth in ('error', 'warn', 'debug'):
|
||||||
exec (('def {0}(self, msg, *args, **kwargs):\n'
|
exec (('def {0}(self, msg, *args, **kwargs):\n'
|
||||||
' self.{0}s.append((kwargs.get("prefix") or self.prefix, msg, args, kwargs))\n').format(meth))
|
' self.{0}s.append((kwargs.get("prefix") or self.prefix, msg, args, kwargs))\n').format(meth))
|
||||||
@ -133,9 +125,7 @@ class ItemReplace(object):
|
|||||||
self.d[self.key] = self.old
|
self.d[self.key] = self.old
|
||||||
|
|
||||||
|
|
||||||
def replace_env(key, new, d=None):
|
def replace_env(key, new, environ=None, **kwargs):
|
||||||
r = None
|
r = kwargs.copy()
|
||||||
if not d:
|
r['environ'] = environ or {}
|
||||||
r = Pl()
|
return ItemReplace(r['environ'], key, new, r)
|
||||||
d = r.environ
|
|
||||||
return ItemReplace(d, key, new, r)
|
|
||||||
|
@ -65,17 +65,17 @@ class TestConfig(TestCase):
|
|||||||
from powerline.shell import ShellPowerline
|
from powerline.shell import ShellPowerline
|
||||||
args = Args(last_pipe_status=[1, 0], ext=['shell'], renderer_module='zsh_prompt')
|
args = Args(last_pipe_status=[1, 0], ext=['shell'], renderer_module='zsh_prompt')
|
||||||
with ShellPowerline(args, run_once=False) as powerline:
|
with ShellPowerline(args, run_once=False) as powerline:
|
||||||
powerline.render(segment_info=args)
|
powerline.render(segment_info={'args': args})
|
||||||
with ShellPowerline(args, run_once=False) as powerline:
|
with ShellPowerline(args, run_once=False) as powerline:
|
||||||
powerline.render(segment_info=args)
|
powerline.render(segment_info={'args': args})
|
||||||
|
|
||||||
def test_bash(self):
|
def test_bash(self):
|
||||||
from powerline.shell import ShellPowerline
|
from powerline.shell import ShellPowerline
|
||||||
args = Args(last_exit_code=1, ext=['shell'], renderer_module='bash_prompt', config=[('ext', {'shell': {'theme': 'default_leftonly'}})])
|
args = Args(last_exit_code=1, ext=['shell'], renderer_module='bash_prompt', config=[('ext', {'shell': {'theme': 'default_leftonly'}})])
|
||||||
with ShellPowerline(args, run_once=False) as powerline:
|
with ShellPowerline(args, run_once=False) as powerline:
|
||||||
powerline.render(segment_info=args)
|
powerline.render(segment_info={'args': args})
|
||||||
with ShellPowerline(args, run_once=False) as powerline:
|
with ShellPowerline(args, run_once=False) as powerline:
|
||||||
powerline.render(segment_info=args)
|
powerline.render(segment_info={'args': args})
|
||||||
|
|
||||||
def test_ipython(self):
|
def test_ipython(self):
|
||||||
from powerline.ipython import IpythonPowerline
|
from powerline.ipython import IpythonPowerline
|
||||||
|
@ -14,15 +14,22 @@ vim = None
|
|||||||
class TestShell(TestCase):
|
class TestShell(TestCase):
|
||||||
def test_last_status(self):
|
def test_last_status(self):
|
||||||
pl = Pl()
|
pl = Pl()
|
||||||
self.assertEqual(shell.last_status(pl=pl, segment_info=Args(last_exit_code=10)),
|
segment_info = {'args': Args(last_exit_code=10)}
|
||||||
|
self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info),
|
||||||
[{'contents': '10', 'highlight_group': 'exit_fail'}])
|
[{'contents': '10', 'highlight_group': 'exit_fail'}])
|
||||||
self.assertEqual(shell.last_status(pl=pl, segment_info=Args(last_exit_code=None)), None)
|
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
|
||||||
|
self.assertEqual(shell.last_status(pl=pl, segment_info=segment_info), None)
|
||||||
|
|
||||||
def test_last_pipe_status(self):
|
def test_last_pipe_status(self):
|
||||||
pl = Pl()
|
pl = Pl()
|
||||||
self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=Args(last_pipe_status=[])), None)
|
segment_info = {'args': Args(last_pipe_status=[])}
|
||||||
self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=Args(last_pipe_status=[0, 0, 0])), None)
|
self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info), None)
|
||||||
self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=Args(last_pipe_status=[0, 2, 0])),
|
segment_info['args'].last_pipe_status=[0, 0, 0]
|
||||||
|
self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info), None)
|
||||||
|
segment_info['args'].last_pipe_status=[0, 2, 0]
|
||||||
|
self.assertEqual(shell.last_pipe_status(pl=pl, segment_info=segment_info),
|
||||||
[{'contents': '0', 'highlight_group': 'exit_success'},
|
[{'contents': '0', 'highlight_group': 'exit_success'},
|
||||||
{'contents': '2', 'highlight_group': 'exit_fail'},
|
{'contents': '2', 'highlight_group': 'exit_fail'},
|
||||||
{'contents': '0', 'highlight_group': 'exit_success'}])
|
{'contents': '0', 'highlight_group': 'exit_success'}])
|
||||||
@ -30,77 +37,93 @@ class TestShell(TestCase):
|
|||||||
|
|
||||||
class TestCommon(TestCase):
|
class TestCommon(TestCase):
|
||||||
def test_hostname(self):
|
def test_hostname(self):
|
||||||
with replace_env('SSH_CLIENT', '192.168.0.12 40921 22') as pl:
|
pl = Pl()
|
||||||
|
with replace_env('SSH_CLIENT', '192.168.0.12 40921 22') as segment_info:
|
||||||
with replace_module_module(common, 'socket', gethostname=lambda: 'abc'):
|
with replace_module_module(common, 'socket', gethostname=lambda: 'abc'):
|
||||||
self.assertEqual(common.hostname(pl=pl), 'abc')
|
self.assertEqual(common.hostname(pl=pl, segment_info=segment_info), 'abc')
|
||||||
self.assertEqual(common.hostname(pl=pl, only_if_ssh=True), 'abc')
|
self.assertEqual(common.hostname(pl=pl, segment_info=segment_info, only_if_ssh=True), 'abc')
|
||||||
pl.environ.pop('SSH_CLIENT')
|
segment_info['environ'].pop('SSH_CLIENT')
|
||||||
self.assertEqual(common.hostname(pl=pl), 'abc')
|
self.assertEqual(common.hostname(pl=pl, segment_info=segment_info), 'abc')
|
||||||
self.assertEqual(common.hostname(pl=pl, only_if_ssh=True), None)
|
self.assertEqual(common.hostname(pl=pl, segment_info=segment_info, only_if_ssh=True), None)
|
||||||
|
|
||||||
def test_user(self):
|
def test_user(self):
|
||||||
new_os = new_module('os', getpid=lambda: 1)
|
new_os = new_module('os', getpid=lambda: 1)
|
||||||
new_psutil = new_module('psutil', Process=lambda pid: Args(username='def'))
|
new_psutil = new_module('psutil', Process=lambda pid: Args(username='def'))
|
||||||
with replace_env('USER', 'def') as pl:
|
pl = Pl()
|
||||||
|
with replace_env('USER', 'def') as segment_info:
|
||||||
with replace_attr(common, 'os', new_os):
|
with replace_attr(common, 'os', new_os):
|
||||||
with replace_attr(common, 'psutil', new_psutil):
|
with replace_attr(common, 'psutil', new_psutil):
|
||||||
with replace_attr(common, '_geteuid', lambda: 5):
|
with replace_attr(common, '_geteuid', lambda: 5):
|
||||||
self.assertEqual(common.user(pl=pl), [{'contents': 'def', 'highlight_group': 'user'}])
|
self.assertEqual(common.user(pl=pl, segment_info=segment_info), [
|
||||||
|
{'contents': 'def', 'highlight_group': 'user'}
|
||||||
|
])
|
||||||
with replace_attr(common, '_geteuid', lambda: 0):
|
with replace_attr(common, '_geteuid', lambda: 0):
|
||||||
self.assertEqual(common.user(pl=pl), [{'contents': 'def', 'highlight_group': ['superuser', 'user']}])
|
self.assertEqual(common.user(pl=pl, segment_info=segment_info), [
|
||||||
|
{'contents': 'def', 'highlight_group': ['superuser', 'user']}
|
||||||
|
])
|
||||||
|
|
||||||
def test_branch(self):
|
def test_branch(self):
|
||||||
pl = Pl()
|
pl = Pl()
|
||||||
pl._cwd = os.getcwd()
|
segment_info = {'getcwd': os.getcwd}
|
||||||
with replace_attr(common, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda: None, directory='/tmp/tests')):
|
with replace_attr(common, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda: None, directory='/tmp/tests')):
|
||||||
self.assertEqual(common.branch(pl=pl, status_colors=False), 'tests')
|
self.assertEqual(common.branch(pl=pl, segment_info=segment_info, status_colors=False), 'tests')
|
||||||
self.assertEqual(common.branch(pl=pl, status_colors=True),
|
self.assertEqual(common.branch(pl=pl, segment_info=segment_info, status_colors=True),
|
||||||
[{'contents': 'tests', 'highlight_group': ['branch_clean', 'branch']}])
|
[{'contents': 'tests', 'highlight_group': ['branch_clean', 'branch']}])
|
||||||
with replace_attr(common, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda: 'D ', directory='/tmp/tests')):
|
with replace_attr(common, 'guess', lambda path: Args(branch=lambda: os.path.basename(path), status=lambda: 'D ', directory='/tmp/tests')):
|
||||||
self.assertEqual(common.branch(pl=pl, status_colors=False), 'tests')
|
self.assertEqual(common.branch(pl=pl, segment_info=segment_info, status_colors=False), 'tests')
|
||||||
self.assertEqual(common.branch(pl=pl, status_colors=True),
|
self.assertEqual(common.branch(pl=pl, segment_info=segment_info, status_colors=True),
|
||||||
[{'contents': 'tests', 'highlight_group': ['branch_dirty', 'branch']}])
|
[{'contents': 'tests', 'highlight_group': ['branch_dirty', 'branch']}])
|
||||||
self.assertEqual(common.branch(pl=pl), 'tests')
|
self.assertEqual(common.branch(pl=pl, segment_info=segment_info), 'tests')
|
||||||
with replace_attr(common, 'guess', lambda path: None):
|
with replace_attr(common, 'guess', lambda path: None):
|
||||||
self.assertEqual(common.branch(pl=pl), None)
|
self.assertEqual(common.branch(pl=pl, segment_info=segment_info), None)
|
||||||
|
|
||||||
def test_cwd(self):
|
def test_cwd(self):
|
||||||
new_os = new_module('os', path=os.path, sep='/')
|
new_os = new_module('os', path=os.path, sep='/')
|
||||||
pl = Pl()
|
pl = Pl()
|
||||||
|
cwd = [None]
|
||||||
|
|
||||||
|
def getcwd():
|
||||||
|
wd = cwd[0]
|
||||||
|
if isinstance(wd, Exception):
|
||||||
|
raise wd
|
||||||
|
else:
|
||||||
|
return wd
|
||||||
|
|
||||||
|
segment_info = {'getcwd': getcwd, 'home': None}
|
||||||
with replace_attr(common, 'os', new_os):
|
with replace_attr(common, 'os', new_os):
|
||||||
pl._cwd = '/abc/def/ghi/foo/bar'
|
cwd[0] = '/abc/def/ghi/foo/bar'
|
||||||
self.assertEqual(common.cwd(pl=pl),
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info),
|
||||||
[{'contents': '/', 'divider_highlight_group': 'cwd:divider'},
|
[{'contents': '/', 'divider_highlight_group': 'cwd:divider'},
|
||||||
{'contents': 'abc', 'divider_highlight_group': 'cwd:divider'},
|
{'contents': 'abc', 'divider_highlight_group': 'cwd:divider'},
|
||||||
{'contents': 'def', 'divider_highlight_group': 'cwd:divider'},
|
{'contents': 'def', 'divider_highlight_group': 'cwd:divider'},
|
||||||
{'contents': 'ghi', 'divider_highlight_group': 'cwd:divider'},
|
{'contents': 'ghi', 'divider_highlight_group': 'cwd:divider'},
|
||||||
{'contents': 'foo', 'divider_highlight_group': 'cwd:divider'},
|
{'contents': 'foo', 'divider_highlight_group': 'cwd:divider'},
|
||||||
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}])
|
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}])
|
||||||
pl.home = '/abc/def/ghi'
|
segment_info['home'] = '/abc/def/ghi'
|
||||||
self.assertEqual(common.cwd(pl=pl),
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info),
|
||||||
[{'contents': '~', 'divider_highlight_group': 'cwd:divider'},
|
[{'contents': '~', 'divider_highlight_group': 'cwd:divider'},
|
||||||
{'contents': 'foo', 'divider_highlight_group': 'cwd:divider'},
|
{'contents': 'foo', 'divider_highlight_group': 'cwd:divider'},
|
||||||
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}])
|
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}])
|
||||||
self.assertEqual(common.cwd(pl=pl, dir_limit_depth=3),
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=3),
|
||||||
[{'contents': '~', 'divider_highlight_group': 'cwd:divider'},
|
[{'contents': '~', 'divider_highlight_group': 'cwd:divider'},
|
||||||
{'contents': 'foo', 'divider_highlight_group': 'cwd:divider'},
|
{'contents': 'foo', 'divider_highlight_group': 'cwd:divider'},
|
||||||
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}])
|
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}])
|
||||||
self.assertEqual(common.cwd(pl=pl, dir_limit_depth=1),
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=1),
|
||||||
[{'contents': '⋯', 'divider_highlight_group': 'cwd:divider'},
|
[{'contents': '⋯', 'divider_highlight_group': 'cwd:divider'},
|
||||||
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}])
|
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}])
|
||||||
self.assertEqual(common.cwd(pl=pl, dir_limit_depth=2, dir_shorten_len=2),
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=2, dir_shorten_len=2),
|
||||||
[{'contents': '~', 'divider_highlight_group': 'cwd:divider'},
|
[{'contents': '~', 'divider_highlight_group': 'cwd:divider'},
|
||||||
{'contents': 'fo', 'divider_highlight_group': 'cwd:divider'},
|
{'contents': 'fo', 'divider_highlight_group': 'cwd:divider'},
|
||||||
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}])
|
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}])
|
||||||
ose = OSError()
|
ose = OSError()
|
||||||
ose.errno = 2
|
ose.errno = 2
|
||||||
pl._cwd = ose
|
cwd[0] = ose
|
||||||
self.assertEqual(common.cwd(pl=pl, dir_limit_depth=2, dir_shorten_len=2),
|
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=2, dir_shorten_len=2),
|
||||||
[{'contents': '[not found]', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}])
|
[{'contents': '[not found]', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}])
|
||||||
pl._cwd = OSError()
|
cwd[0] = OSError()
|
||||||
self.assertRaises(OSError, common.cwd, pl=pl, dir_limit_depth=2, dir_shorten_len=2)
|
self.assertRaises(OSError, common.cwd, pl=pl, segment_info=segment_info, dir_limit_depth=2, dir_shorten_len=2)
|
||||||
pl._cwd = ValueError()
|
cwd[0] = ValueError()
|
||||||
self.assertRaises(ValueError, common.cwd, pl=pl, dir_limit_depth=2, dir_shorten_len=2)
|
self.assertRaises(ValueError, common.cwd, pl=pl, segment_info=segment_info, dir_limit_depth=2, dir_shorten_len=2)
|
||||||
|
|
||||||
def test_date(self):
|
def test_date(self):
|
||||||
pl = Pl()
|
pl = Pl()
|
||||||
@ -257,10 +280,11 @@ class TestCommon(TestCase):
|
|||||||
common.network_load.shutdown()
|
common.network_load.shutdown()
|
||||||
|
|
||||||
def test_virtualenv(self):
|
def test_virtualenv(self):
|
||||||
with replace_env('VIRTUAL_ENV', '/abc/def/ghi') as pl:
|
pl = Pl()
|
||||||
self.assertEqual(common.virtualenv(pl=pl), 'ghi')
|
with replace_env('VIRTUAL_ENV', '/abc/def/ghi') as segment_info:
|
||||||
pl.environ.pop('VIRTUAL_ENV')
|
self.assertEqual(common.virtualenv(pl=pl, segment_info=segment_info), 'ghi')
|
||||||
self.assertEqual(common.virtualenv(pl=pl), None)
|
segment_info['environ'].pop('VIRTUAL_ENV')
|
||||||
|
self.assertEqual(common.virtualenv(pl=pl, segment_info=segment_info), None)
|
||||||
|
|
||||||
def test_email_imap_alert(self):
|
def test_email_imap_alert(self):
|
||||||
# TODO
|
# TODO
|
||||||
|
Loading…
x
Reference in New Issue
Block a user