Debian Codegraph @

lua/lualine/components/filesize.lua

raw
1 -- Copyright (c) 2020-2021 shadmansaleh
2 -- MIT license, see LICENSE for more details.
3 local function filesize()
4 local file = vim.fn.expand('%:p')
5 if file == nil or #file == 0 then
6 return ''
7 end
8 local size = vim.fn.getfsize(file)
9 if size <= 0 then
10 return ''
11 end
12
13 local suffixes = { 'b', 'k', 'm', 'g' }
14
15 local i = 1
16 while size > 1024 and i < #suffixes do
17 size = size / 1024
18 i = i + 1
19 end
20
21 local format = i == 1 and '%d%s' or '%.1f%s'
22 return string.format(format, size, suffixes[i])
23 end
24
25 return filesize