2021-02-15 18:09:12 +00:00
|
|
|
-- Copyright (c) 2020-2021 shadmansaleh
|
|
|
|
-- MIT license, see LICENSE for more details.
|
2021-03-15 23:37:46 +00:00
|
|
|
local M = {}
|
2021-02-15 18:09:12 +00:00
|
|
|
|
2021-03-15 23:37:46 +00:00
|
|
|
local highlight = require 'lualine.highlight'
|
2021-02-15 18:09:12 +00:00
|
|
|
|
|
|
|
-- set upper or lower case
|
2021-03-18 00:57:59 +00:00
|
|
|
function M.apply_case(status, options)
|
2021-02-15 18:09:12 +00:00
|
|
|
-- Donn't work on components that emit vim statusline escaped chars
|
|
|
|
if status:find('%%') and not status:find('%%%%') then return status end
|
|
|
|
if options.upper == true then
|
|
|
|
return status:upper()
|
|
|
|
elseif options.lower == true then
|
|
|
|
return status:lower()
|
|
|
|
end
|
|
|
|
return status
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Adds spaces to left and right of a component
|
2021-03-18 00:57:59 +00:00
|
|
|
function M.apply_padding(status, options)
|
2021-03-15 23:37:46 +00:00
|
|
|
local l_padding = (options.left_padding or options.padding or 1)
|
2021-02-15 18:09:12 +00:00
|
|
|
local r_padding = (options.right_padding or options.padding or 1)
|
2021-02-21 23:07:44 +00:00
|
|
|
if l_padding then
|
|
|
|
if status:find('%%#.*#') == 1 then
|
|
|
|
-- When component has changed the highlight at begining
|
|
|
|
-- we will add the padding after the highlight
|
|
|
|
local pre_highlight = vim.fn.matchlist(status, [[\(%#.\{-\}#\)]])[2]
|
2021-03-15 23:37:46 +00:00
|
|
|
status = pre_highlight .. string.rep(' ', l_padding) ..
|
|
|
|
status:sub(#pre_highlight + 1, #status)
|
2021-02-21 23:07:44 +00:00
|
|
|
else
|
2021-03-15 23:37:46 +00:00
|
|
|
status = string.rep(' ', l_padding) .. status
|
2021-02-21 23:07:44 +00:00
|
|
|
end
|
|
|
|
end
|
2021-03-15 23:37:46 +00:00
|
|
|
if r_padding then status = status .. string.rep(' ', r_padding) end
|
2021-02-15 18:09:12 +00:00
|
|
|
return status
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Applies custom highlights for component
|
2021-03-18 00:57:59 +00:00
|
|
|
function M.apply_highlights(status, options, default_hl)
|
2021-02-15 18:09:12 +00:00
|
|
|
if options.color_highlight then
|
2021-03-15 23:37:46 +00:00
|
|
|
status = highlight.component_format_highlight(options.color_highlight) ..
|
|
|
|
status
|
2021-02-15 18:09:12 +00:00
|
|
|
end
|
2021-03-14 13:38:19 +00:00
|
|
|
return status .. default_hl
|
2021-02-15 18:09:12 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Apply icon in front of component
|
2021-03-18 00:57:59 +00:00
|
|
|
function M.apply_icon(status, options)
|
2021-02-15 18:09:12 +00:00
|
|
|
if options.icons_enabled and options.icon then
|
|
|
|
status = options.icon .. ' ' .. status
|
|
|
|
end
|
|
|
|
return status
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Apply separator at end of component only when
|
|
|
|
-- custom highlights haven't affected background
|
2021-03-18 00:57:59 +00:00
|
|
|
function M.apply_spearator(status, options)
|
2021-03-15 23:37:46 +00:00
|
|
|
local separator
|
|
|
|
if options.separator and #options.separator > 0 then
|
|
|
|
separator = options.separator
|
|
|
|
elseif options.component_separators then
|
|
|
|
if options.self.section < 'lualine_x' then
|
|
|
|
separator = options.component_separators[1]
|
|
|
|
else
|
|
|
|
separator = options.component_separators[2]
|
2021-02-20 03:21:05 +00:00
|
|
|
end
|
2021-03-15 23:37:46 +00:00
|
|
|
options.separator = separator
|
|
|
|
end
|
|
|
|
if separator then status = status .. separator end
|
|
|
|
options.separator_applied = separator
|
2021-02-20 03:21:05 +00:00
|
|
|
return status
|
|
|
|
end
|
|
|
|
|
2021-03-18 00:57:59 +00:00
|
|
|
function M.strip_separator(status, options)
|
2021-02-20 03:21:05 +00:00
|
|
|
if options.separator_applied then
|
|
|
|
status = status:sub(1, #status - #options.separator_applied)
|
|
|
|
options.separator_applied = nil
|
2021-02-15 18:09:12 +00:00
|
|
|
end
|
|
|
|
return status
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|