From b3c5c4403cf6761e23161b884f3f995bc7849444 Mon Sep 17 00:00:00 2001 From: shadmansaleh <13149513+shadmansaleh@users.noreply.github.com> Date: Fri, 3 Sep 2021 16:16:10 +0600 Subject: [PATCH] feat: add new filesize component --- README.md | 1 + doc/lualine.txt | 1 + examples/evil_lualine.lua | 17 +---------------- lua/lualine/components/filesize.lua | 23 +++++++++++++++++++++++ 4 files changed, 26 insertions(+), 16 deletions(-) create mode 100644 lua/lualine/components/filesize.lua diff --git a/README.md b/README.md index 4814ba2..4104d8a 100644 --- a/README.md +++ b/README.md @@ -186,6 +186,7 @@ sections = {lualine_a = {'mode'}} * `encoding` (file encoding) * `fileformat` (file format) * `filename` +* `filesize` * `filetype` * `hostname` * `location` (location in file in line:column format) diff --git a/doc/lualine.txt b/doc/lualine.txt index a1d2ebc..22b07ac 100644 --- a/doc/lualine.txt +++ b/doc/lualine.txt @@ -128,6 +128,7 @@ Available components~ * |encoding| (file encoding) * |fileformat| (file format) * |filename| + * |filesize| * |filetype| * |hostname| * |location| (location in file in line:column format) diff --git a/examples/evil_lualine.lua b/examples/evil_lualine.lua index 98c18a0..190dc83 100644 --- a/examples/evil_lualine.lua +++ b/examples/evil_lualine.lua @@ -116,22 +116,7 @@ ins_left { ins_left { -- filesize component - function() - 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, + 'filesize', condition = conditions.buffer_not_empty } diff --git a/lua/lualine/components/filesize.lua b/lua/lualine/components/filesize.lua new file mode 100644 index 0000000..cc87905 --- /dev/null +++ b/lua/lualine/components/filesize.lua @@ -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