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-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)
|
|
|
|
return path:gsub(
|
|
|
|
string.format('([^%s])[^%s]+%%%s', sep, sep, sep), '%1' .. sep, 1)
|
|
|
|
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-04-20 16:25:25 +00:00
|
|
|
local default_symbols = {modified = '[+]', readonly = '[-]'}
|
2021-05-09 21:11:18 +00:00
|
|
|
new_instance.options.symbols = vim.tbl_extend('force', default_symbols,
|
|
|
|
new_instance.options.symbols or
|
|
|
|
{})
|
2021-04-11 08:20:41 +00:00
|
|
|
|
2021-02-15 18:09:12 +00:00
|
|
|
-- setting defaults
|
2021-04-20 03:02:21 +00:00
|
|
|
if new_instance.options.file_status == nil then
|
|
|
|
new_instance.options.file_status = true
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
2021-05-11 10:47:09 +00:00
|
|
|
if new_instance.options.path == nil then new_instance.options.path = 0 end
|
2021-05-28 19:49:44 +00:00
|
|
|
if new_instance.options.shorting_target == nil then
|
|
|
|
new_instance.options.shorting_target = 40
|
|
|
|
end
|
2021-02-15 18:09:12 +00:00
|
|
|
|
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-04-11 08:20:41 +00:00
|
|
|
data = vim.fn.expand('%:~:.')
|
2021-05-11 10:47:09 +00:00
|
|
|
elseif self.options.path == 2 then
|
|
|
|
-- absolute path
|
|
|
|
data = vim.fn.expand('%:p')
|
|
|
|
else
|
|
|
|
-- just filename
|
|
|
|
data = vim.fn.expand('%:t')
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
|
2021-05-11 10:47:09 +00:00
|
|
|
if data == '' then data = '[No Name]' end
|
|
|
|
|
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
|