feat: dynamic shortening for filetype (#224)

* feat: dynamic shortening for filetype

* feat: redraw statusline on VimResized

Co-authored-by: NullVoxPopuli <LPSego3+dev@gmail.com>
Co-authored-by: shadmansaleh <shadmansaleh3@gmail.com>
This commit is contained in:
Hubert Pelczarski 2021-05-11 12:47:09 +02:00 committed by GitHub
parent 11280b44f2
commit 73a6c8fadf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 37 additions and 22 deletions

View File

@ -301,8 +301,7 @@ symbols | `{error = ' ', warn = ' ', info = ' '}` or `{error = 'E:', wa
Option | Default | Behaviour Option | Default | Behaviour
:------: | :------: | :----: :------: | :------: | :----:
file_status | true | Displays file status (readonly status, modified status) 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` path | 0 | filename `path` option: 0 = just filename, 1 = relative path, 2 = absolute path
shorten | true | if `full_path` is true and `shorten` is `false` it shortens absolute path `aaa/bbb/ccc/file` to `a/b/c/file`
symbols | `{modified = '[+]', readonly = '[-]'}` | changes status symbols | table containing one or more symbols | symbols | `{modified = '[+]', readonly = '[-]'}` | changes status symbols | table containing one or more symbols |
* `diff` component options * `diff` component options

View File

@ -406,12 +406,8 @@ In addition, some components have unique options.
• file_status (true) • file_status (true)
Displays file status (readonly status, modified status) Displays file status (readonly status, modified status)
• full_path (false) • path (0)
Displays relative path if set to `true`, absolute path if set to `false` filename `path` option: 0 = just filename, 1 = relative path, 2 = absolute path
• shorten (true)
if `full_path` is `true` and `shorten` is `false` it shortens
absolute path `aaa/bbb/ccc/file` to `a/b/c/file`
• symbols (`{modified = '[+]', readonly = '[-]'}`) • symbols (`{modified = '[+]', readonly = '[-]'}`)
changes status symbols changes status symbols

View File

@ -2,6 +2,16 @@
-- MIT license, see LICENSE for more details. -- MIT license, see LICENSE for more details.
local FileName = require('lualine.component'):new() 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) FileName.new = function(self, options, child)
local new_instance = self._parent:new(options, child or FileName) local new_instance = self._parent:new(options, child or FileName)
local default_symbols = {modified = '[+]', readonly = '[-]'} local default_symbols = {modified = '[+]', readonly = '[-]'}
@ -13,28 +23,37 @@ FileName.new = function(self, options, child)
if new_instance.options.file_status == nil then if new_instance.options.file_status == nil then
new_instance.options.file_status = true new_instance.options.file_status = true
end end
if new_instance.options.shorten == nil then if new_instance.options.path == nil then new_instance.options.path = 0 end
new_instance.options.shorten = true if new_instance.options.full_path or new_instance.options.shorten then
end vim.api.nvim_err_writeln(
if new_instance.options.full_path == nil then [[ filetype component configuration changed, see :h lualine_custom_options ]])
new_instance.options.full_path = false
end end
return new_instance return new_instance
end end
FileName.update_status = function(self) FileName.update_status = function(self)
local data = vim.fn.expand('%:p') local data
if not self.options.full_path then if self.options.path == 1 then
data = vim.fn.expand('%:t') -- relative path
elseif self.options.shorten then
data = vim.fn.expand('%:~:.') 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 end
if data == '' then if data == '' then data = '[No Name]' end
data = '[No Name]'
elseif vim.fn.winwidth(0) <= 84 or #data > 40 then local windwidth = vim.fn.winwidth(0)
data = vim.fn.pathshorten(data) 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 end
if self.options.file_status then if self.options.file_status then

View File

@ -174,6 +174,7 @@ local function set_statusline()
autocmd! autocmd!
autocmd WinLeave,BufLeave * lua vim.wo.statusline=require'lualine'.statusline() autocmd WinLeave,BufLeave * lua vim.wo.statusline=require'lualine'.statusline()
autocmd WinEnter,BufEnter * set statusline< autocmd WinEnter,BufEnter * set statusline<
autocmd VimResized * redrawstatus
augroup END augroup END
]], false) ]], false)
end end