61ac665774
- includes modified class implementation from https://github.com/rxi/classic/blob/master/classic.lua - now base component class is created from classic. - change to how component classes are created. - Don't overwrite new method to initialize a component. Overwrite the init method. new is responsible for creating class object and calling init on it. Unlike previous new overwrite you don't need to create the class (table) and return it. Instead you will recive the object as self and do required manipulation on that just like any most other oop langs. Also don't need to return anything from init. init's job is to initialize. remember to call classes init before running your operations unfortunately lua isn't full fledged oop lang and I don't how to automate this. - changes how super classes are accesed. - rename Component._parent -> Component.super - methods on super classes now ran through super class instead of objects _parent self._parent as that can lead to recursive inf loop. See branch, diff, tabs, buffer classes call to init for example on pattern. - All components updated to reflect current logic - component loader updated to use new initialization procedure. - updated tests - updated BREAKING_CHANGES.md - plus quite a bit of formatting changes in the components - comp.method = function(self, ...) -> function M:method(...) BREAKING_CHANGE
66 lines
1.7 KiB
Lua
66 lines
1.7 KiB
Lua
-- Copyright (c) 2020-2021 shadmansaleh
|
|
-- MIT license, see LICENSE for more details.
|
|
local M = require('lualine.component'):extend()
|
|
|
|
local default_options = {
|
|
symbols = { modified = '[+]', readonly = '[-]' },
|
|
file_status = true,
|
|
path = 0,
|
|
shorting_target = 40,
|
|
}
|
|
|
|
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
|
|
|
|
M.init = function(self, options)
|
|
M.super.init(self, options)
|
|
self.options = vim.tbl_deep_extend('keep', self.options or {}, default_options)
|
|
end
|
|
|
|
M.update_status = function(self)
|
|
local data
|
|
if self.options.path == 1 then
|
|
-- relative path
|
|
data = vim.fn.expand '%:~:.'
|
|
elseif self.options.path == 2 then
|
|
-- absolute path
|
|
data = vim.fn.expand '%:p'
|
|
else
|
|
-- just filename
|
|
data = vim.fn.expand '%:t'
|
|
end
|
|
|
|
if data == '' then
|
|
data = '[No Name]'
|
|
end
|
|
|
|
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
|
|
end
|
|
end
|
|
|
|
if self.options.file_status then
|
|
if vim.bo.modified then
|
|
data = data .. self.options.symbols.modified
|
|
elseif vim.bo.modifiable == false or vim.bo.readonly == true then
|
|
data = data .. self.options.symbols.readonly
|
|
end
|
|
end
|
|
return data
|
|
end
|
|
|
|
return M
|