2021-02-16 00:09:12 +06:00
|
|
|
-- Copyright (c) 2020-2021 shadmansaleh
|
2021-02-13 01:03:57 +01:00
|
|
|
-- MIT license, see LICENSE for more details.
|
2021-09-25 23:15:42 +06:00
|
|
|
local M = require('lualine.component'):extend()
|
2021-04-11 14:20:41 +06:00
|
|
|
|
2022-02-15 22:54:29 +06:00
|
|
|
local modules = require('lualine_require').lazy_require {
|
|
|
|
utils = 'lualine.utils.utils',
|
|
|
|
}
|
|
|
|
|
2021-09-03 18:04:48 +06:00
|
|
|
local default_options = {
|
2022-08-08 13:59:33 +08:00
|
|
|
symbols = {
|
|
|
|
modified = '[+]',
|
|
|
|
readonly = '[-]',
|
|
|
|
unnamed = '[No Name]',
|
|
|
|
newfile = '[New]',
|
|
|
|
},
|
2021-09-03 18:04:48 +06:00
|
|
|
file_status = true,
|
2022-08-08 13:59:33 +08:00
|
|
|
newfile_status = false,
|
2021-09-03 18:04:48 +06:00
|
|
|
path = 0,
|
|
|
|
shorting_target = 40,
|
|
|
|
}
|
|
|
|
|
2022-08-08 13:59:33 +08: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 13:59:33 +08:00
|
|
|
end
|
|
|
|
|
2021-10-12 20:04:47 +06:00
|
|
|
---shortens path by turning apple/orange -> a/orange
|
|
|
|
---@param path string
|
|
|
|
---@param sep string path separator
|
2022-09-10 11:20:37 +02:00
|
|
|
---@param max_len integer maximum length of the full filename string
|
2021-10-12 20:04:47 +06:00
|
|
|
---@return string
|
2022-09-10 11:20:37 +02: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 12:47:09 +02:00
|
|
|
end
|
|
|
|
|
2023-03-31 11:02:12 +03: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
|
2023-03-31 08:02:48 +00:00
|
|
|
return table.concat({ segments[#segments - 1], segments[#segments] }, sep)
|
2023-03-31 11:02:12 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-25 23:15:42 +06: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 14:20:41 +06:00
|
|
|
end
|
|
|
|
|
2021-09-25 23:15:42 +06:00
|
|
|
M.update_status = function(self)
|
2023-03-31 11:02:12 +03:00
|
|
|
local path_separator = package.config:sub(1, 1)
|
2021-05-11 12:47:09 +02:00
|
|
|
local data
|
|
|
|
if self.options.path == 1 then
|
|
|
|
-- relative path
|
2022-01-02 17:38:39 +06:00
|
|
|
data = vim.fn.expand('%:~:.')
|
2021-05-11 12:47:09 +02:00
|
|
|
elseif self.options.path == 2 then
|
|
|
|
-- absolute path
|
2022-01-02 17:38:39 +06:00
|
|
|
data = vim.fn.expand('%:p')
|
2022-05-13 15:28:03 +10:00
|
|
|
elseif self.options.path == 3 then
|
|
|
|
-- absolute path, with tilde
|
|
|
|
data = vim.fn.expand('%:p:~')
|
2023-03-31 11:02:12 +03:00
|
|
|
elseif self.options.path == 4 then
|
|
|
|
-- filename and immediate parent
|
|
|
|
data = filename_and_parent(vim.fn.expand('%:p:~'), path_separator)
|
2021-05-11 12:47:09 +02:00
|
|
|
else
|
|
|
|
-- just filename
|
2022-01-02 17:38:39 +06:00
|
|
|
data = vim.fn.expand('%:t')
|
2021-04-11 14:20:41 +06:00
|
|
|
end
|
|
|
|
|
2022-02-15 22:54:29 +06:00
|
|
|
data = modules.utils.stl_escape(data)
|
|
|
|
|
2021-09-04 00:28:20 +06:00
|
|
|
if data == '' then
|
2021-12-18 13:49:41 +01:00
|
|
|
data = self.options.symbols.unnamed
|
2021-09-04 00:28:20 +06:00
|
|
|
end
|
2021-05-11 12:47:09 +02:00
|
|
|
|
2021-05-28 15:49:44 -04:00
|
|
|
if self.options.shorting_target ~= 0 then
|
2022-03-18 22:08:52 +08:00
|
|
|
local windwidth = self.options.globalstatus and vim.go.columns or vim.fn.winwidth(0)
|
2021-05-28 15:49:44 -04:00
|
|
|
local estimated_space_available = windwidth - self.options.shorting_target
|
|
|
|
|
2022-09-10 11:20:37 +02:00
|
|
|
data = shorten_path(data, path_separator, estimated_space_available)
|
2021-04-11 14:20:41 +06:00
|
|
|
end
|
2021-02-16 00:09:12 +06:00
|
|
|
|
2022-09-10 11:39:38 +02:00
|
|
|
local symbols = {}
|
2021-04-11 14:20:41 +06:00
|
|
|
if self.options.file_status then
|
|
|
|
if vim.bo.modified then
|
2022-09-10 11:39:38 +02:00
|
|
|
table.insert(symbols, self.options.symbols.modified)
|
2021-12-18 14:04:09 +01:00
|
|
|
end
|
|
|
|
if vim.bo.modifiable == false or vim.bo.readonly == true then
|
2022-09-10 11:39:38 +02:00
|
|
|
table.insert(symbols, self.options.symbols.readonly)
|
2021-02-16 00:09:12 +06:00
|
|
|
end
|
|
|
|
end
|
2022-08-08 13:59:33 +08:00
|
|
|
|
|
|
|
if self.options.newfile_status and is_new_file() then
|
2022-09-10 11:39:38 +02:00
|
|
|
table.insert(symbols, self.options.symbols.newfile)
|
2022-08-08 13:59:33 +08:00
|
|
|
end
|
2022-09-10 11:39:38 +02:00
|
|
|
|
|
|
|
return data .. (#symbols > 0 and ' ' .. table.concat(symbols, '') or '')
|
2020-12-30 15:48:51 +01:00
|
|
|
end
|
|
|
|
|
2021-09-25 23:15:42 +06:00
|
|
|
return M
|