From 73a6c8fadfd1fccdda92208253570e54dea64826 Mon Sep 17 00:00:00 2001 From: Hubert Pelczarski <41551030+hoob3rt@users.noreply.github.com> Date: Tue, 11 May 2021 12:47:09 +0200 Subject: [PATCH] feat: dynamic shortening for filetype (#224) * feat: dynamic shortening for filetype * feat: redraw statusline on VimResized Co-authored-by: NullVoxPopuli Co-authored-by: shadmansaleh --- README.md | 3 +- doc/lualine.txt | 10 ++----- lua/lualine/components/filename.lua | 45 ++++++++++++++++++++--------- lua/lualine/init.lua | 1 + 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index ea82e45..a1ec6d7 100644 --- a/README.md +++ b/README.md @@ -301,8 +301,7 @@ symbols | `{error = ' ', warn = ' ', info = ' '}` or `{error = 'E:', wa Option | Default | Behaviour :------: | :------: | :----: file_status | true | Displays file status (readonly status, modified status) -full_path | false | Displays relative path if set to `true`, absolute path if set to `false` -shorten | true | if `full_path` is true and `shorten` is `false` it shortens absolute path `aaa/bbb/ccc/file` to `a/b/c/file` +path | 0 | filename `path` option: 0 = just filename, 1 = relative path, 2 = absolute path symbols | `{modified = '[+]', readonly = '[-]'}` | changes status symbols | table containing one or more symbols | * `diff` component options diff --git a/doc/lualine.txt b/doc/lualine.txt index 5669462..1d2fe29 100644 --- a/doc/lualine.txt +++ b/doc/lualine.txt @@ -403,15 +403,11 @@ In addition, some components have unique options. < • filename~ - • file_status (true) + • file_status (true) Displays file status (readonly status, modified status) - • full_path (false) - Displays relative path if set to `true`, absolute path if set to `false` - - • shorten (true) - if `full_path` is `true` and `shorten` is `false` it shortens - absolute path `aaa/bbb/ccc/file` to `a/b/c/file` + • path (0) + filename `path` option: 0 = just filename, 1 = relative path, 2 = absolute path • symbols (`{modified = '[+]', readonly = '[-]'}`) changes status symbols diff --git a/lua/lualine/components/filename.lua b/lua/lualine/components/filename.lua index 15f05be..18752b2 100644 --- a/lua/lualine/components/filename.lua +++ b/lua/lualine/components/filename.lua @@ -2,6 +2,16 @@ -- MIT license, see LICENSE for more details. local FileName = require('lualine.component'):new() +local function count(base, pattern) + return select(2, string.gsub(base, pattern, '')) +end + +local function shorten_path(path, sep) + -- ('([^/])[^/]+%/', '%1/', 1) + return path:gsub( + string.format('([^%s])[^%s]+%%%s', sep, sep, sep), '%1' .. sep, 1) +end + FileName.new = function(self, options, child) local new_instance = self._parent:new(options, child or FileName) local default_symbols = {modified = '[+]', readonly = '[-]'} @@ -13,28 +23,37 @@ FileName.new = function(self, options, child) if new_instance.options.file_status == nil then new_instance.options.file_status = true end - if new_instance.options.shorten == nil then - new_instance.options.shorten = true - end - if new_instance.options.full_path == nil then - new_instance.options.full_path = false + if new_instance.options.path == nil then new_instance.options.path = 0 end + if new_instance.options.full_path or new_instance.options.shorten then + vim.api.nvim_err_writeln( + [[ filetype component configuration changed, see :h lualine_custom_options ]]) end return new_instance end FileName.update_status = function(self) - local data = vim.fn.expand('%:p') - if not self.options.full_path then - data = vim.fn.expand('%:t') - elseif self.options.shorten then + local data + if self.options.path == 1 then + -- relative path data = vim.fn.expand('%:~:.') + elseif self.options.path == 2 then + -- absolute path + data = vim.fn.expand('%:p') + else + -- just filename + data = vim.fn.expand('%:t') end - if data == '' then - data = '[No Name]' - elseif vim.fn.winwidth(0) <= 84 or #data > 40 then - data = vim.fn.pathshorten(data) + if data == '' then data = '[No Name]' end + + local windwidth = vim.fn.winwidth(0) + local estimated_space_available = 40 + local path_separator = package.config:sub(1, 1) + for _ = 0, count(data, path_separator) do + if windwidth <= 84 or #data > estimated_space_available then + data = shorten_path(data, path_separator) + end end if self.options.file_status then diff --git a/lua/lualine/init.lua b/lua/lualine/init.lua index be24320..e542e44 100644 --- a/lua/lualine/init.lua +++ b/lua/lualine/init.lua @@ -174,6 +174,7 @@ local function set_statusline() autocmd! autocmd WinLeave,BufLeave * lua vim.wo.statusline=require'lualine'.statusline() autocmd WinEnter,BufEnter * set statusline< + autocmd VimResized * redrawstatus augroup END ]], false) end