diff --git a/powerline/lib/vcs/__init__.py b/powerline/lib/vcs/__init__.py index 2bbc9bdf..aa53abf8 100644 --- a/powerline/lib/vcs/__init__.py +++ b/powerline/lib/vcs/__init__.py @@ -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 diff --git a/powerline/segments/vim.py b/powerline/segments/vim.py index 06da9e5d..09b4c636 100644 --- a/powerline/segments/vim.py +++ b/powerline/segments/vim.py @@ -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) diff --git a/tests/test_segments.py b/tests/test_segments.py index 491e4a3a..ec6be894 100644 --- a/tests/test_segments.py +++ b/tests/test_segments.py @@ -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') diff --git a/tests/vim.py b/tests/vim.py index 1b9b820c..08e57276 100644 --- a/tests/vim.py +++ b/tests/vim.py @@ -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