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:
parent
992e6151eb
commit
4e5bd54f79
|
@ -117,7 +117,7 @@ Highlight groups used: ``branch_clean``, ``branch_dirty``, ``branch``.
|
|||
|
||||
|
||||
@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.
|
||||
|
||||
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`)
|
||||
:param int dir_limit_depth:
|
||||
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``.
|
||||
|
||||
|
@ -155,14 +156,20 @@ def cwd(pl, segment_info, dir_shorten_len=None, dir_limit_depth=None):
|
|||
ret = []
|
||||
if not cwd[0]:
|
||||
cwd[0] = '/'
|
||||
draw_inner_divider = not use_path_separator
|
||||
for part in cwd:
|
||||
if not part:
|
||||
continue
|
||||
if use_path_separator:
|
||||
part += os.sep
|
||||
ret.append({
|
||||
'contents': part,
|
||||
'divider_highlight_group': 'cwd:divider',
|
||||
'draw_inner_divider': draw_inner_divider,
|
||||
})
|
||||
ret[-1]['highlight_group'] = ['cwd:current_folder', 'cwd']
|
||||
if use_path_separator:
|
||||
ret[-1]['contents'] = ret[-1]['contents'][:-1]
|
||||
return ret
|
||||
|
||||
|
||||
|
|
|
@ -102,34 +102,48 @@ class TestCommon(TestCase):
|
|||
segment_info = {'getcwd': getcwd, 'home': None}
|
||||
with replace_attr(common, 'os', new_os):
|
||||
cwd[0] = '/abc/def/ghi/foo/bar'
|
||||
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info),
|
||||
[{'contents': '/', 'divider_highlight_group': 'cwd:divider'},
|
||||
{'contents': 'abc', 'divider_highlight_group': 'cwd:divider'},
|
||||
{'contents': 'def', 'divider_highlight_group': 'cwd:divider'},
|
||||
{'contents': 'ghi', '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']}])
|
||||
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info), [
|
||||
{'contents': '/', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
||||
{'contents': 'abc', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
||||
{'contents': 'def', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
||||
{'contents': 'ghi', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
||||
{'contents': 'foo', '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']},
|
||||
])
|
||||
segment_info['home'] = '/abc/def/ghi'
|
||||
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info),
|
||||
[{'contents': '~', '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']}])
|
||||
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=3),
|
||||
[{'contents': '~', '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']}])
|
||||
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info, dir_limit_depth=1),
|
||||
[{'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=2, dir_shorten_len=2),
|
||||
[{'contents': '~', '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']}])
|
||||
self.assertEqual(common.cwd(pl=pl, segment_info=segment_info), [
|
||||
{'contents': '~', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
||||
{'contents': 'foo', '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=3), [
|
||||
{'contents': '~', 'divider_highlight_group': 'cwd:divider', 'draw_inner_divider': True},
|
||||
{'contents': 'foo', '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=1), [
|
||||
{'contents': '⋯', '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=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.errno = 2
|
||||
cwd[0] = ose
|
||||
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()
|
||||
self.assertRaises(OSError, common.cwd, pl=pl, segment_info=segment_info, dir_limit_depth=2, dir_shorten_len=2)
|
||||
cwd[0] = ValueError()
|
||||
|
|
Loading…
Reference in New Issue