1
0
mirror of https://github.com/SpaceVim/SpaceVim.git synced 2025-02-04 03:40:05 +08:00
SpaceVim/bundle/defx.nvim/rplugin/python3/defx/column/size.py

52 lines
1.6 KiB
Python
Raw Normal View History

2020-06-13 14:06:35 +08:00
# ============================================================================
# FILE: size.py
# AUTHOR: Shougo Matsushita <Shougo.Matsu at gmail.com>
# License: MIT license
# ============================================================================
2020-10-31 15:58:52 +08:00
from defx.base.column import Base, Highlights
2020-06-13 14:06:35 +08:00
from defx.context import Context
2020-10-31 15:58:52 +08:00
from defx.util import Nvim, readable, Candidate
2020-06-13 14:06:35 +08:00
import typing
class Column(Base):
def __init__(self, vim: Nvim) -> None:
super().__init__(vim)
self.name = 'size'
2020-10-31 15:58:52 +08:00
self.has_get_with_highlights = True
self._length = 9
2020-06-13 14:06:35 +08:00
2020-10-31 15:58:52 +08:00
def get_with_highlights(
self, context: Context, candidate: Candidate
) -> typing.Tuple[str, Highlights]:
2020-06-13 14:06:35 +08:00
path = candidate['action__path']
if not readable(path) or path.is_dir():
2020-10-31 15:58:52 +08:00
return (' ' * self._length, [])
2020-06-13 14:06:35 +08:00
size = self._get_size(path.stat().st_size)
2020-10-31 15:58:52 +08:00
text = '{:>6s}{:>3s}'.format(size[0], size[1])
return (text, [(self.highlight_name, self.start, self._length)])
2020-06-13 14:06:35 +08:00
def _get_size(self, size: float) -> typing.Tuple[str, str]:
multiple = 1024
suffixes = ['KB', 'MB', 'GB', 'TB']
if size < multiple:
return (str(size), 'B')
for suffix in suffixes:
size /= multiple
if size < multiple:
return ('{:.1f}'.format(size), suffix)
return ('INF', '')
def length(self, context: Context) -> int:
2020-10-31 15:58:52 +08:00
return self._length
2020-06-13 14:06:35 +08:00
def highlight_commands(self) -> typing.List[str]:
commands: typing.List[str] = []
commands.append(
2020-10-31 15:58:52 +08:00
f'highlight default link {self.highlight_name} Constant')
2020-06-13 14:06:35 +08:00
return commands