Fix cwd segment, add use_path_separator option

cwd segment was not drawing inner separators because of new default for 
multisegment functions. use_path_separator was added because current variant 
looks bad for my taste.
This commit is contained in:
ZyX 2013-04-02 19:01:03 +04:00
parent 992e6151eb
commit 4e5bd54f79
2 changed files with 46 additions and 25 deletions

View File

@ -117,7 +117,7 @@ Highlight groups used: ``branch_clean``, ``branch_dirty``, ``branch``.
@requires_segment_info @requires_segment_info
def cwd(pl, segment_info, dir_shorten_len=None, dir_limit_depth=None): def cwd(pl, segment_info, dir_shorten_len=None, dir_limit_depth=None, use_path_separator=False):
'''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.
@ -126,7 +126,8 @@ def cwd(pl, segment_info, dir_shorten_len=None, dir_limit_depth=None):
shorten parent directory names to this length (e.g. :file:`/long/path/to/powerline` :file:`/l/p/t/powerline`) shorten parent directory names to this length (e.g. :file:`/long/path/to/powerline` :file:`/l/p/t/powerline`)
:param int dir_limit_depth: :param int dir_limit_depth:
limit directory depth to this number (e.g. :file:`/long/path/to/powerline` :file:`/to/powerline`) limit directory depth to this number (e.g. :file:`/long/path/to/powerline` :file:`/to/powerline`)
:param bool use_path_separator:
Use path separator in place of soft divider.
Divider highlight group used: ``cwd:divider``. Divider highlight group used: ``cwd:divider``.
@ -155,14 +156,20 @@ def cwd(pl, segment_info, dir_shorten_len=None, dir_limit_depth=None):
ret = [] ret = []
if not cwd[0]: if not cwd[0]:
cwd[0] = '/' cwd[0] = '/'
draw_inner_divider = not use_path_separator
for part in cwd: for part in cwd:
if not part: if not part:
continue continue
if use_path_separator:
part += os.sep
ret.append({ ret.append({
'contents': part, 'contents': part,
'divider_highlight_group': 'cwd:divider', 'divider_highlight_group': 'cwd:divider',
'draw_inner_divider': draw_inner_divider,
}) })
ret[-1]['highlight_group'] = ['cwd:current_folder', 'cwd'] ret[-1]['highlight_group'] = ['cwd:current_folder', 'cwd']
if use_path_separator:
ret[-1]['contents'] = ret[-1]['contents'][:-1]
return ret return ret

View File

@ -102,34 +102,48 @@ class TestCommon(TestCase):
segment_info = {'getcwd': getcwd, 'home': None} segment_info = {'getcwd': getcwd, 'home': None}
with replace_attr(common, 'os', new_os): with replace_attr(common, 'os', new_os):
cwd[0] = '/abc/def/ghi/foo/bar' cwd[0] = '/abc/def/ghi/foo/bar'
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info), self.assertEqual(common.cwd(pl=pl, segment_info=segment_info), [
[{'contents': '/', 'divider_highlight_group': 'cwd:divider'}, {'contents': '/', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
{'contents': 'abc', 'divider_highlight_group': 'cwd:divider'}, {'contents': 'abc', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
{'contents': 'def', 'divider_highlight_group': 'cwd:divider'}, {'contents': 'def', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
{'contents': 'ghi', 'divider_highlight_group': 'cwd:divider'}, {'contents': 'ghi', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
{'contents': 'foo', 'divider_highlight_group': 'cwd:divider'}, {'contents': 'foo', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}]) {'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True, 'highlight_group': ['cwd:current_folder', 'cwd']},
])
segment_info['home'] = '/abc/def/ghi' segment_info['home'] = '/abc/def/ghi'
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info), self.assertEqual(common.cwd(pl=pl, segment_info=segment_info), [
[{'contents': '~', 'divider_highlight_group': 'cwd:divider'}, {'contents': '~', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
{'contents': 'foo', 'divider_highlight_group': 'cwd:divider'}, {'contents': 'foo', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}]) {'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True, 'highlight_group': ['cwd:current_folder', 'cwd']},
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=3), ])
[{'contents': '~', 'divider_highlight_group': 'cwd:divider'}, self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=3), [
{'contents': 'foo', 'divider_highlight_group': 'cwd:divider'}, {'contents': '~', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}]) {'contents': 'foo', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=1), {'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True, 'highlight_group': ['cwd:current_folder', 'cwd']}
[{'contents': '', 'divider_highlight_group': 'cwd:divider'}, ])
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}]) self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=1), [
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=2, dir_shorten_len=2), {'contents': '', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
[{'contents': '~', 'divider_highlight_group': 'cwd:divider'}, {'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True, 'highlight_group': ['cwd:current_folder', 'cwd']}
{'contents': 'fo', 'divider_highlight_group': 'cwd:divider'}, ])
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'highlight_group': ['cwd:current_folder', 'cwd']}]) self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=1, use_path_separator=True), [
{'contents': '⋯/', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': False},
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': False, 'highlight_group': ['cwd:current_folder', 'cwd']}
])
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=2, dir_shorten_len=2), [
{'contents': '~', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
{'contents': 'fo', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True, 'highlight_group': ['cwd:current_folder', 'cwd']}
])
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=2, dir_shorten_len=2, use_path_separator=True), [
{'contents': '~/', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': False},
{'contents': 'fo/', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': False},
{'contents': 'bar', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': False, 'highlight_group': ['cwd:current_folder', 'cwd']}
])
ose = OSError() ose = OSError()
ose.errno = 2 ose.errno = 2
cwd[0] = ose cwd[0] = ose
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, 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'], 'draw_inner_divider': True}])
cwd[0] = OSError() cwd[0] = OSError()
self.assertRaises(OSError, common.cwd, pl=pl, segment_info=segment_info, 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)
cwd[0] = ValueError() cwd[0] = ValueError()