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-09-25 17:15:42 +00:00
|
|
|
local M = require('lualine.component'):extend()
|
2021-04-11 08:20:41 +00:00
|
|
|
|
2022-02-15 16:54:29 +00:00
|
|
|
local modules = require('lualine_require').lazy_require {
|
|
|
|
utils = 'lualine.utils.utils',
|
|
|
|
}
|
|
|
|
|
2021-09-03 12:04:48 +00:00
|
|
|
local default_options = {
|
2022-08-08 05:59:33 +00:00
|
|
|
symbols = {
|
|
|
|
modified = '[+]',
|
|
|
|
readonly = '[-]',
|
|
|
|
unnamed = '[No Name]',
|
|
|
|
newfile = '[New]',
|
|
|
|
},
|
2021-09-03 12:04:48 +00:00
|
|
|
file_status = true,
|
2022-08-08 05:59:33 +00:00
|
|
|
newfile_status = false,
|
2021-09-03 12:04:48 +00:00
|
|
|
path = 0,
|
|
|
|
shorting_target = 40,
|
|
|
|
}
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---counts how many times pattern occur in base ( used for counting path-sep )
|
|
|
|
---@param base string
|
|
|
|
---@param pattern string
|
|
|
|
---@return number
|
2021-05-11 10:47:09 +00:00
|
|
|
local function count(base, pattern)
|
|
|
|
return select(2, string.gsub(base, pattern, ''))
|
|
|
|
end
|
|
|
|
|
2022-08-08 05:59:33 +00:00
|
|
|
local function is_new_file()
|
|
|
|
local filename = vim.fn.expand('%')
|
2022-08-09 07:07:55 +00:00
|
|
|
return filename ~= '' and vim.bo.buftype == '' and
|
|
|
|
vim.fn.filereadable(filename) == 0
|
2022-08-08 05:59:33 +00:00
|
|
|
end
|
|
|
|
|
2021-10-12 14:04:47 +00:00
|
|
|
---shortens path by turning apple/orange -> a/orange
|
|
|
|
---@param path string
|
|
|
|
---@param sep string path separator
|
|
|
|
---@return string
|
2021-05-11 10:47:09 +00:00
|
|
|
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-09-25 17:15:42 +00:00
|
|
|
M.init = function(self, options)
|
|
|
|
M.super.init(self, options)
|
|
|
|
self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options)
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
M.update_status = function(self)
|
2021-05-11 10:47:09 +00:00
|
|
|
local data
|
|
|
|
if self.options.path == 1 then
|
|
|
|
-- relative path
|
2022-01-02 11:38:39 +00:00
|
|
|
data = vim.fn.expand('%:~:.')
|
2021-05-11 10:47:09 +00:00
|
|
|
elseif self.options.path == 2 then
|
|
|
|
-- absolute path
|
2022-01-02 11:38:39 +00:00
|
|
|
data = vim.fn.expand('%:p')
|
2022-05-13 05:28:03 +00:00
|
|
|
elseif self.options.path == 3 then
|
|
|
|
-- absolute path, with tilde
|
|
|
|
data = vim.fn.expand('%:p:~')
|
2021-05-11 10:47:09 +00:00
|
|
|
else
|
|
|
|
-- just filename
|
2022-01-02 11:38:39 +00:00
|
|
|
data = vim.fn.expand('%:t')
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
|
|
|
|
2022-02-15 16:54:29 +00:00
|
|
|
data = modules.utils.stl_escape(data)
|
|
|
|
|
2021-09-03 18:28:20 +00:00
|
|
|
if data == '' then
|
2021-12-18 12:49:41 +00:00
|
|
|
data = self.options.symbols.unnamed
|
2021-09-03 18:28:20 +00:00
|
|
|
end
|
2021-05-11 10:47:09 +00:00
|
|
|
|
2021-05-28 19:49:44 +00:00
|
|
|
if self.options.shorting_target ~= 0 then
|
2022-03-18 14:08:52 +00:00
|
|
|
local windwidth = self.options.globalstatus and vim.go.columns or vim.fn.winwidth(0)
|
2021-05-28 19:49:44 +00:00
|
|
|
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-12-18 13:04:09 +00:00
|
|
|
end
|
|
|
|
if 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
|
2022-08-08 05:59:33 +00:00
|
|
|
|
|
|
|
if self.options.newfile_status and is_new_file() then
|
|
|
|
data = data .. self.options.symbols.newfile
|
|
|
|
end
|
2021-04-11 08:20:41 +00:00
|
|
|
return data
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
return M
|