2021-02-15 18:09:12 +00:00
|
|
|
-- Copyright (c) 2020-2021 shadmansaleh
|
2021-02-13 00:03:57 +00:00
|
|
|
-- MIT license, see LICENSE for more details.
|
2021-04-11 08:20:41 +00:00
|
|
|
local FileName = require('lualine.component'):new()
|
|
|
|
|
2021-09-03 12:04:48 +00:00
|
|
|
local default_options = {
|
2021-09-03 18:28:20 +00:00
|
|
|
symbols = { modified = '[+]', readonly = '[-]' },
|
2021-09-03 12:04:48 +00:00
|
|
|
file_status = true,
|
|
|
|
path = 0,
|
|
|
|
shorting_target = 40,
|
|
|
|
}
|
|
|
|
|
2021-05-11 10:47:09 +00:00
|
|
|
local function count(base, pattern)
|
|
|
|
return select(2, string.gsub(base, pattern, ''))
|
|
|
|
end
|
|
|
|
|
|
|
|
local function shorten_path(path, sep)
|
|
|
|
-- ('([^/])[^/]+%/', '%1/', 1)
|
2021-09-03 18:28:20 +00:00
|
|
|
return path:gsub(string.format('([^%s])[^%s]+%%%s', sep, sep, sep), '%1' .. sep, 1)
|
2021-05-11 10:47:09 +00:00
|
|
|
end
|
|
|
|
|
2021-04-11 08:20:41 +00:00
|
|
|
FileName.new = function(self, options, child)
|
2021-04-20 03:02:21 +00:00
|
|
|
local new_instance = self._parent:new(options, child or FileName)
|
2021-09-03 18:28:20 +00:00
|
|
|
new_instance.options = vim.tbl_deep_extend('keep', new_instance.options or {}, default_options)
|
2021-04-20 03:02:21 +00:00
|
|
|
return new_instance
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
FileName.update_status = function(self)
|
2021-05-11 10:47:09 +00:00
|
|
|
local data
|
|
|
|
if self.options.path == 1 then
|
|
|
|
-- relative path
|
2021-09-03 18:28:20 +00:00
|
|
|
data = vim.fn.expand '%:~:.'
|
2021-05-11 10:47:09 +00:00
|
|
|
elseif self.options.path == 2 then
|
|
|
|
-- absolute path
|
2021-09-03 18:28:20 +00:00
|
|
|
data = vim.fn.expand '%:p'
|
2021-05-11 10:47:09 +00:00
|
|
|
else
|
|
|
|
-- just filename
|
2021-09-03 18:28:20 +00:00
|
|
|
data = vim.fn.expand '%:t'
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
|
2021-09-03 18:28:20 +00:00
|
|
|
if data == '' then
|
|
|
|
data = '[No Name]'
|
|
|
|
end
|
2021-05-11 10:47:09 +00:00
|
|
|
|
2021-05-28 19:49:44 +00:00
|
|
|
if self.options.shorting_target ~= 0 then
|
|
|
|
local windwidth = vim.fn.winwidth(0)
|
|
|
|
local estimated_space_available = windwidth - self.options.shorting_target
|
|
|
|
|
|
|
|
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
|
2021-05-11 10:47:09 +00:00
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
2021-02-15 18:09:12 +00:00
|
|
|
|
2021-04-11 08:20:41 +00:00
|
|
|
if self.options.file_status then
|
|
|
|
if vim.bo.modified then
|
2021-04-20 16:25:25 +00:00
|
|
|
data = data .. self.options.symbols.modified
|
2021-04-11 08:20:41 +00:00
|
|
|
elseif vim.bo.modifiable == false or vim.bo.readonly == true then
|
2021-04-20 16:25:25 +00:00
|
|
|
data = data .. self.options.symbols.readonly
|
2021-02-15 18:09:12 +00:00
|
|
|
end
|
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
return data
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
|
2021-04-11 08:20:41 +00:00
|
|
|
return FileName
|