commit
4343e4edb4
|
@ -7,6 +7,7 @@
|
||||||
"file_encoding": "file_format",
|
"file_encoding": "file_format",
|
||||||
"file_type": "file_format",
|
"file_type": "file_format",
|
||||||
"branch": "information:additional",
|
"branch": "information:additional",
|
||||||
|
"file_scheme": "file_name",
|
||||||
"file_directory": "information:additional",
|
"file_directory": "information:additional",
|
||||||
"file_name_empty": "file_directory",
|
"file_name_empty": "file_directory",
|
||||||
"line_percent": "information:additional",
|
"line_percent": "information:additional",
|
||||||
|
|
|
@ -40,6 +40,10 @@
|
||||||
"draw_soft_divider": false,
|
"draw_soft_divider": false,
|
||||||
"after": " "
|
"after": " "
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"name": "file_scheme",
|
||||||
|
"priority": 20
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "file_directory",
|
"name": "file_directory",
|
||||||
"priority": 40,
|
"priority": 40,
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
from __future__ import unicode_literals, absolute_import, division
|
from __future__ import unicode_literals, absolute_import, division
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
try:
|
try:
|
||||||
import vim
|
import vim
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -153,28 +154,71 @@ def readonly_indicator(pl, segment_info, text=''):
|
||||||
return text if int(vim_getbufoption(segment_info, 'readonly')) else None
|
return text if int(vim_getbufoption(segment_info, 'readonly')) else None
|
||||||
|
|
||||||
|
|
||||||
|
SCHEME_RE = re.compile(b'^\\w[\\w\\d+\\-.]*(?=:)')
|
||||||
|
|
||||||
|
|
||||||
@requires_segment_info
|
@requires_segment_info
|
||||||
def file_directory(pl, segment_info, shorten_user=True, shorten_cwd=True, shorten_home=False):
|
def file_scheme(pl, segment_info):
|
||||||
'''Return file directory (head component of the file path).
|
'''Return the protocol part of the file.
|
||||||
|
|
||||||
:param bool shorten_user:
|
Protocol is the part of the full filename just before the colon which
|
||||||
shorten ``$HOME`` directory to :file:`~/`
|
starts with a latin letter and contains only latin letters, digits, plus,
|
||||||
|
period or hyphen (refer to `RFC3986
|
||||||
|
<http://tools.ietf.org/html/rfc3986#section-3.1>`_ for the description of
|
||||||
|
URI scheme). If there is no such a thing ``None`` is returned, effectively
|
||||||
|
removing segment.
|
||||||
|
|
||||||
:param bool shorten_cwd:
|
.. note::
|
||||||
shorten current directory to :file:`./`
|
Segment will not check whether there is ``//`` just after the
|
||||||
|
colon or if there is at least one slash after the scheme. Reason: it is
|
||||||
:param bool shorten_home:
|
not always present. E.g. when opening file inside a zip archive file
|
||||||
shorten all directories in :file:`/home/` to :file:`~user/` instead of :file:`/home/user/`.
|
name will look like :file:`zipfile:/path/to/archive.zip::file.txt`.
|
||||||
|
``file_scheme`` segment will catch ``zipfile`` part here.
|
||||||
'''
|
'''
|
||||||
name = buffer_name(segment_info['buffer'])
|
name = buffer_name(segment_info['buffer'])
|
||||||
if not name:
|
if not name:
|
||||||
return None
|
return None
|
||||||
file_directory = vim_funcs['fnamemodify'](name, (':~' if shorten_user else '')
|
match = SCHEME_RE.match(name)
|
||||||
+ (':.' if shorten_cwd else '') + ':h')
|
if match:
|
||||||
if not file_directory:
|
return match.group(0).decode('ascii')
|
||||||
|
|
||||||
|
|
||||||
|
@requires_segment_info
|
||||||
|
def file_directory(pl, segment_info, remove_scheme=True, shorten_user=True, shorten_cwd=True, shorten_home=False):
|
||||||
|
'''Return file directory (head component of the file path).
|
||||||
|
|
||||||
|
:param bool remove_scheme:
|
||||||
|
Remove scheme part from the segment name, if present. See documentation
|
||||||
|
of file_scheme segment for the description of what scheme is. Also
|
||||||
|
removes the colon.
|
||||||
|
|
||||||
|
:param bool shorten_user:
|
||||||
|
Shorten ``$HOME`` directory to :file:`~/`. Does not work for files with
|
||||||
|
scheme.
|
||||||
|
|
||||||
|
:param bool shorten_cwd:
|
||||||
|
Shorten current directory to :file:`./`. Does not work for files with
|
||||||
|
scheme present.
|
||||||
|
|
||||||
|
:param bool shorten_home:
|
||||||
|
Shorten all directories in :file:`/home/` to :file:`~user/` instead of
|
||||||
|
:file:`/home/user/`. Does not work for files with scheme present.
|
||||||
|
'''
|
||||||
|
name = buffer_name(segment_info['buffer'])
|
||||||
|
if not name:
|
||||||
return None
|
return None
|
||||||
if shorten_home and file_directory.startswith('/home/'):
|
match = SCHEME_RE.match(name)
|
||||||
file_directory = b'~' + file_directory[6:]
|
if match:
|
||||||
|
if remove_scheme:
|
||||||
|
name = name[len(match.group(0)) + 1:] # Remove scheme and colon
|
||||||
|
file_directory = vim_funcs['fnamemodify'](name, ':h')
|
||||||
|
else:
|
||||||
|
file_directory = vim_funcs['fnamemodify'](name, (':~' if shorten_user else '')
|
||||||
|
+ (':.' if shorten_cwd else '') + ':h')
|
||||||
|
if not file_directory:
|
||||||
|
return None
|
||||||
|
if shorten_home and file_directory.startswith('/home/'):
|
||||||
|
file_directory = b'~' + file_directory[6:]
|
||||||
file_directory = file_directory.decode('utf-8', 'powerline_vim_strtrans_error')
|
file_directory = file_directory.decode('utf-8', 'powerline_vim_strtrans_error')
|
||||||
return file_directory + os.sep
|
return file_directory + os.sep
|
||||||
|
|
||||||
|
|
|
@ -619,6 +619,15 @@ class TestVim(TestCase):
|
||||||
self.assertEqual(vim.readonly_indicator(pl=pl, segment_info=segment_info), '')
|
self.assertEqual(vim.readonly_indicator(pl=pl, segment_info=segment_info), '')
|
||||||
self.assertEqual(vim.readonly_indicator(pl=pl, segment_info=segment_info, text='L'), 'L')
|
self.assertEqual(vim.readonly_indicator(pl=pl, segment_info=segment_info, text='L'), 'L')
|
||||||
|
|
||||||
|
def test_file_scheme(self):
|
||||||
|
pl = Pl()
|
||||||
|
segment_info = vim_module._get_segment_info()
|
||||||
|
self.assertEqual(vim.file_scheme(pl=pl, segment_info=segment_info), None)
|
||||||
|
with vim_module._with('buffer', '/tmp/’’/abc') as segment_info:
|
||||||
|
self.assertEqual(vim.file_scheme(pl=pl, segment_info=segment_info), None)
|
||||||
|
with vim_module._with('buffer', 'zipfile:/tmp/abc.zip::abc/abc.vim') as segment_info:
|
||||||
|
self.assertEqual(vim.file_scheme(pl=pl, segment_info=segment_info), 'zipfile')
|
||||||
|
|
||||||
def test_file_directory(self):
|
def test_file_directory(self):
|
||||||
pl = Pl()
|
pl = Pl()
|
||||||
segment_info = vim_module._get_segment_info()
|
segment_info = vim_module._get_segment_info()
|
||||||
|
@ -632,6 +641,14 @@ class TestVim(TestCase):
|
||||||
self.assertEqual(vim.file_directory(pl=pl, segment_info=segment_info), '/tmp/')
|
self.assertEqual(vim.file_directory(pl=pl, segment_info=segment_info), '/tmp/')
|
||||||
os.environ['HOME'] = '/tmp'
|
os.environ['HOME'] = '/tmp'
|
||||||
self.assertEqual(vim.file_directory(pl=pl, segment_info=segment_info), '~/')
|
self.assertEqual(vim.file_directory(pl=pl, segment_info=segment_info), '~/')
|
||||||
|
with vim_module._with('buffer', 'zipfile:/tmp/abc.zip::abc/abc.vim') as segment_info:
|
||||||
|
self.assertEqual(vim.file_directory(pl=pl, segment_info=segment_info, remove_scheme=False), 'zipfile:/tmp/abc.zip::abc/')
|
||||||
|
self.assertEqual(vim.file_directory(pl=pl, segment_info=segment_info, remove_scheme=True), '/tmp/abc.zip::abc/')
|
||||||
|
self.assertEqual(vim.file_directory(pl=pl, segment_info=segment_info), '/tmp/abc.zip::abc/')
|
||||||
|
os.environ['HOME'] = '/tmp'
|
||||||
|
self.assertEqual(vim.file_directory(pl=pl, segment_info=segment_info, remove_scheme=False), 'zipfile:/tmp/abc.zip::abc/')
|
||||||
|
self.assertEqual(vim.file_directory(pl=pl, segment_info=segment_info, remove_scheme=True), '/tmp/abc.zip::abc/')
|
||||||
|
self.assertEqual(vim.file_directory(pl=pl, segment_info=segment_info), '/tmp/abc.zip::abc/')
|
||||||
|
|
||||||
def test_file_name(self):
|
def test_file_name(self):
|
||||||
pl = Pl()
|
pl = Pl()
|
||||||
|
|
|
@ -525,7 +525,10 @@ class _Buffer(object):
|
||||||
import os
|
import os
|
||||||
if type(name) is not bytes:
|
if type(name) is not bytes:
|
||||||
name = name.encode('utf-8')
|
name = name.encode('utf-8')
|
||||||
self._name = os.path.abspath(name)
|
if b':/' in name:
|
||||||
|
self._name = name
|
||||||
|
else:
|
||||||
|
self._name = os.path.abspath(name)
|
||||||
|
|
||||||
def __getitem__(self, line):
|
def __getitem__(self, line):
|
||||||
return self._buf_lines[line]
|
return self._buf_lines[line]
|
||||||
|
|
Loading…
Reference in New Issue