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,
|
|
|
|
}
|
|
|
|
|
2022-08-08 05:59:33 +00:00
|
|
|
local function is_new_file()
|
|
|
|
local filename = vim.fn.expand('%')
|
2022-08-09 07:08:23 +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
|
2022-09-10 09:20:37 +00:00
|
|
|
---@param max_len integer maximum length of the full filename string
|
2021-10-12 14:04:47 +00:00
|
|
|
---@return string
|
2022-09-10 09:20:37 +00:00
|
|
|
local function shorten_path(path, sep, max_len)
|
|
|
|
local len = #path
|
|
|
|
if len <= max_len then
|
|
|
|
return path
|
|
|
|
end
|
|
|
|
|
|
|
|
local segments = vim.split(path, sep)
|
|
|
|
for idx = 1, #segments - 1 do
|
|
|
|
if len <= max_len then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
|
|
|
|
local segment = segments[idx]
|
|
|
|
local shortened = segment:sub(1, vim.startswith(segment, '.') and 2 or 1)
|
|
|
|
segments[idx] = shortened
|
|
|
|
len = len - (#segment - #shortened)
|
|
|
|
end
|
|
|
|
|
|
|
|
return table.concat(segments, sep)
|
2021-05-11 10:47:09 +00:00
|
|
|
end
|
|
|
|
|
2023-03-31 08:02:12 +00:00
|
|
|
local function filename_and_parent(path, sep)
|
|
|
|
local segments = vim.split(path, sep)
|
|
|
|
if #segments == 0 then
|
|
|
|
return path
|
|
|
|
elseif #segments == 1 then
|
|
|
|
return segments[#segments]
|
|
|
|
else
|
|
|
|
return table.concat({segments[#segments - 1], segments[#segments]}, sep)
|
|
|
|
end
|
|
|
|
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)
|
2023-03-31 08:02:12 +00:00
|
|
|
local path_separator = package.config:sub(1, 1)
|
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:~')
|
2023-03-31 08:02:12 +00:00
|
|
|
elseif self.options.path == 4 then
|
|
|
|
-- filename and immediate parent
|
|
|
|
data = filename_and_parent(vim.fn.expand('%:p:~'), path_separator)
|
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
|
|
|
|
|
2022-09-10 09:20:37 +00:00
|
|
|
data = shorten_path(data, path_separator, estimated_space_available)
|
2021-04-11 08:20:41 +00:00
|
|
|
end
|
2021-02-15 18:09:12 +00:00
|
|
|
|
2022-09-10 09:39:38 +00:00
|
|
|
local symbols = {}
|
2021-04-11 08:20:41 +00:00
|
|
|
if self.options.file_status then
|
|
|
|
if vim.bo.modified then
|
2022-09-10 09:39:38 +00:00
|
|
|
table.insert(symbols, self.options.symbols.modified)
|
2021-12-18 13:04:09 +00:00
|
|
|
end
|
|
|
|
if vim.bo.modifiable == false or vim.bo.readonly == true then
|
2022-09-10 09:39:38 +00:00
|
|
|
table.insert(symbols, 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
|
2022-09-10 09:39:38 +00:00
|
|
|
table.insert(symbols, self.options.symbols.newfile)
|
2022-08-08 05:59:33 +00:00
|
|
|
end
|
2022-09-10 09:39:38 +00:00
|
|
|
|
|
|
|
return data .. (#symbols > 0 and ' ' .. table.concat(symbols, '') or '')
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
|
2021-09-25 17:15:42 +00:00
|
|
|
return M
|