Make file_size segment output buffer size

This commit is contained in:
ZyX 2013-03-13 19:49:53 +04:00
parent 6f679e08aa
commit c237e66958
4 changed files with 24 additions and 13 deletions

View File

@ -16,7 +16,7 @@ def generate_directories(path):
while True:
old_path = path
path = os.path.dirname(path)
if path == old_path:
if path == old_path or not path:
break
yield path

View File

@ -20,6 +20,7 @@ vim_funcs = {
'fnamemodify': vim_get_func('fnamemodify'),
'expand': vim_get_func('expand'),
'bufnr': vim_get_func('bufnr', rettype=int),
'line2byte': vim_get_func('line2byte', rettype=int),
}
vim_modes = {
@ -195,10 +196,9 @@ def file_name(segment_info, display_no_file=False, no_file_text='[No file]'):
return file_name
@requires_segment_info
@memoize(2, cache_key=bufname, cache_reg_func=purgebuf_on_shell_and_write)
def file_size(segment_info, suffix='B', si_prefix=False):
'''Return file size.
@window_cached
def file_size(suffix='B', si_prefix=False):
'''Return file size in &encoding.
:param str suffix:
string appended to the file size
@ -206,13 +206,9 @@ def file_size(segment_info, suffix='B', si_prefix=False):
use SI prefix, e.g. MB instead of MiB
:return: file size or None if the file isn't saved or if the size is too big to fit in a number
'''
file_name = segment_info['buffer'].name
if not file_name:
return None
try:
file_size = os.stat(file_name).st_size
except:
return None
# Note: returns file size in &encoding, not in &fileencoding. But returned
# size is updated immediately; and it is valid for any buffer
file_size = vim_funcs['line2byte'](len(vim.current.buffer) + 1) - 1
return humanize_bytes(file_size, suffix, si_prefix)

View File

@ -229,7 +229,7 @@ class TestVim(TestCase):
def test_file_size(self):
segment_info = vim_module._get_segment_info()
self.assertEqual(vim.file_size(segment_info=segment_info), None)
self.assertEqual(vim.file_size(segment_info=segment_info), '0 B')
with vim_module._with('buffer', os.path.join(os.path.dirname(__file__), 'empty')) as segment_info:
self.assertEqual(vim.file_size(segment_info=segment_info), '0 B')

View File

@ -149,6 +149,12 @@ def _emul_exists(varname):
return varname[2:] in _g
raise NotImplementedError
@_logged
def _emul_line2byte(line):
buflines = _buf_lines[_buffer()]
if line == len(buflines) + 1:
return sum((len(s) for s in buflines)) + 1
raise NotImplementedError
_window_ids = [None]
_window_id = 0
@ -242,6 +248,15 @@ class _Buffer(object):
_buf_scopes.pop(bufnr)
class _Current(object):
@property
def buffer(self):
return buffers[_buffer()]
current = _Current()
_dict = None