StatusView: add spaces as separate items instead of pre-pending them to items.

This commit is contained in:
jgmdev 2022-02-24 02:57:39 -04:00
parent 131bdf9fb2
commit 128e10ba47
1 changed files with 40 additions and 34 deletions

View File

@ -63,7 +63,6 @@ StatusView.separator2 = " | "
---@field private active boolean ---@field private active boolean
---@field private x number ---@field private x number
---@field private w number ---@field private w number
---@field private deprecated boolean
---@field private cached_item StatusView.styledtext ---@field private cached_item StatusView.styledtext
StatusView.Item = Object:extend() StatusView.Item = Object:extend()
@ -193,8 +192,7 @@ function StatusView:register_docview_items()
local dv = core.active_view local dv = core.active_view
local line, col = dv.doc:get_selection() local line, col = dv.doc:get_selection()
return { return {
line, style.text, line, ":",
":",
col > config.line_limit and style.accent or style.text, col, col > config.line_limit and style.accent or style.text, col,
style.text, style.text,
self.separator, self.separator,
@ -234,8 +232,8 @@ function StatusView:register_docview_items()
return { return {
style.text, style.text,
style.icon_font, "g", style.icon_font, "g",
style.font, style.dim, self.separator2, style.text, style.font, style.dim, self.separator2,
#dv.doc.lines, " lines", style.text, #dv.doc.lines, " lines",
} }
end end
).separator = self.separator2 ).separator = self.separator2
@ -247,7 +245,7 @@ function StatusView:register_docview_items()
function() function()
local dv = core.active_view local dv = core.active_view
return { return {
dv.doc.crlf and "CRLF" or "LF" style.text, dv.doc.crlf and "CRLF" or "LF"
} }
end, end,
"doc:toggle-line-ending" "doc:toggle-line-ending"
@ -267,7 +265,7 @@ function StatusView:register_command_items()
return { return {
style.icon_font, "g", style.icon_font, "g",
style.font, style.dim, self.separator2, style.font, style.dim, self.separator2,
#core.docs, style.text, " / ", style.text, #core.docs, style.text, " / ",
#core.project_files, " files" #core.project_files, " files"
} }
end end
@ -311,7 +309,7 @@ function StatusView:add_item(predicate, name, alignment, getitem, command, pos,
end end
---Get an item associated object or nil if not found. ---Get an item object associated to a name or nil if not found.
---@param name string ---@param name string
---@return StatusView.Item | nil ---@return StatusView.Item | nil
function StatusView:get_item(name) function StatusView:get_item(name)
@ -450,7 +448,8 @@ end
---Draws a table of styled text on the status bar starting on the left or right. ---Draws a table of styled text on the status bar starting on the left or right.
---@param items StatusView.styledtext ---@param items StatusView.styledtext
---@param right_align boolean ---@param right_align boolean
---@param yoffset number ---@param xoffset? number
---@param yoffset? number
function StatusView:draw_items(items, right_align, xoffset, yoffset) function StatusView:draw_items(items, right_align, xoffset, yoffset)
local x, y = self:get_content_offset() local x, y = self:get_content_offset()
x = x + (xoffset or 0) x = x + (xoffset or 0)
@ -567,29 +566,30 @@ local function merge_deprecated_items(destination, items, alignment)
end end
---Append a space item into the given items list.
---@param self StatusView ---@param self StatusView
---@param styled_text StatusView.styledtext ---@param destination StatusView.Item[]
---@param separator string ---@param separator string
local function add_spacing(self, styled_text, separator) ---@param alignment StatusView.Item.alignment
if ---@return StatusView.Item
Object.is(styled_text[1], renderer.font) local function add_spacing(self, destination, separator, alignment, x)
or ---@type StatusView.Item
( local space = StatusView.Item(nil, "space", alignment)
styled_text[2] ~= self.separator space.cached_item = separator == self.separator and {
or style.text, separator
styled_text[2] ~= self.separator2 } or {
) style.dim, separator
then }
if separator == self.separator2 then space.x = x
table.insert(styled_text, 1, style.dim) space.w = draw_items(self, space.cached_item, 0, 0, text_width)
else
table.insert(styled_text, 1, style.text) table.insert(destination, space)
end
table.insert(styled_text, 2, separator) return space
end
end end
---Remove starting and ending separators.
---@param self StatusView ---@param self StatusView
---@param styled_text StatusView.styledtext ---@param styled_text StatusView.styledtext
local function remove_spacing(self, styled_text) local function remove_spacing(self, styled_text)
@ -625,11 +625,9 @@ local function remove_spacing(self, styled_text)
end end
---Get the styled text that will be displayed on the left side or ---Set the active items that will be displayed on the left or right side
---right side of the status bar checking their predicates and performing ---of the status bar checking their predicates and performing positioning
---positioning calculations for proper functioning of tooltips and clicks. ---calculations for proper functioning of tooltips and clicks.
---@return StatusView.styledtext left
---@return StatusView.styledtext right
function StatusView:update_active_items() function StatusView:update_active_items()
local left, right = {}, {} local left, right = {}, {}
@ -667,7 +665,11 @@ function StatusView:update_active_items()
item.active = true item.active = true
if item.alignment == StatusView.Item.LEFT then if item.alignment == StatusView.Item.LEFT then
if not lfirst then if not lfirst then
add_spacing(self, styled_text, item.separator, true) local space = add_spacing(
self, self.active_items, item.separator, item.alignment, lx
)
lw = lw + space.w
lx = lx + space.w
else else
lfirst = false lfirst = false
end end
@ -680,7 +682,11 @@ function StatusView:update_active_items()
lx = lx + item.w lx = lx + item.w
else else
if not rfirst then if not rfirst then
add_spacing(self, styled_text, item.separator, true) local space = add_spacing(
self, self.active_items, item.separator, item.alignment, rx
)
rw = rw + space.w
rx = rx + space.w
else else
rfirst = false rfirst = false
end end