feat: add new filesize component

This commit is contained in:
shadmansaleh 2021-09-03 16:16:10 +06:00
parent 8b4f70817c
commit b3c5c4403c
4 changed files with 26 additions and 16 deletions

View File

@ -186,6 +186,7 @@ sections = {lualine_a = {'mode'}}
* `encoding` (file encoding) * `encoding` (file encoding)
* `fileformat` (file format) * `fileformat` (file format)
* `filename` * `filename`
* `filesize`
* `filetype` * `filetype`
* `hostname` * `hostname`
* `location` (location in file in line:column format) * `location` (location in file in line:column format)

View File

@ -128,6 +128,7 @@ Available components~
* |encoding| (file encoding) * |encoding| (file encoding)
* |fileformat| (file format) * |fileformat| (file format)
* |filename| * |filename|
* |filesize|
* |filetype| * |filetype|
* |hostname| * |hostname|
* |location| (location in file in line:column format) * |location| (location in file in line:column format)

View File

@ -116,22 +116,7 @@ ins_left {
ins_left { ins_left {
-- filesize component -- filesize component
function() 'filesize',
local function format_file_size(file)
local size = vim.fn.getfsize(file)
if size <= 0 then return '' end
local sufixes = {'b', 'k', 'm', 'g'}
local i = 1
while size > 1024 do
size = size / 1024
i = i + 1
end
return string.format('%.1f%s', size, sufixes[i])
end
local file = vim.fn.expand('%:p')
if string.len(file) == 0 then return '' end
return format_file_size(file)
end,
condition = conditions.buffer_not_empty condition = conditions.buffer_not_empty
} }

View File

@ -0,0 +1,23 @@
-- Copyright (c) 2020-2021 shadmansaleh
-- MIT license, see LICENSE for more details.
local M = require('lualine.component'):new()
M.update_status = function()
local file = vim.fn.expand('%:p')
if file == nil or #file == 0 then return '' end
local size = vim.fn.getfsize(file)
if size <= 0 then return '' end
local sufixes = {'b', 'k', 'm', 'g'}
local i = 1
while size > 1024 and i < #sufixes do
size = size / 1024
i = i + 1
end
return string.format('%.1f%s', size, sufixes[i])
end
return M