Skip to content

Commit

Permalink
feat(bar): support swapping and restoring nil values in dropbar_symbol_t
Browse files Browse the repository at this point in the history
  • Loading branch information
bekaboo committed Jul 3, 2023
1 parent 4d0dd3d commit 31b6fe0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1432,8 +1432,8 @@ basic element of [`dropbar_t`](#dropbar_t) and [`dropbar_menu_entry_t`](#dropbar
| `dropbar_symbol_t:preview()` | preview the symbol in the source window |
| `dropbar_symbol_t:preview_restore_hl()` | clear the preview highlights in the source window |
| `dropbar_symbol_t:preview_restore_view()` | restore the view in the source window after previewing the symbol |
| `dropbar_symbol_t:swap_field(field: string, new_val: any)` | temporarily change the content of a dropbar symbol <br> *does not support replacing nil values |
| `dropbar_symbol_t:restore()` | restore the content of a dropbar symbol after `dropbar_symbol_t:swap_field()` is called <br> *does not support restoring nil values |
| `dropbar_symbol_t:swap_field(field: string, new_val: any)` | temporarily change the content of a dropbar symbol |
| `dropbar_symbol_t:restore()` | restore the original value of the fields of a dropbar symbol changed in `dropbar_symbol_t:swap_field()` |
#### `dropbar_menu_t`
Expand Down
6 changes: 2 additions & 4 deletions doc/dropbar.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1401,17 +1401,15 @@ dropbar_symbol_t:preview_restore_view()
dropbar_symbol_t:swap_field({field}, {new_val})

Temporarily change the content of a dropbar symbol
Currently does not support replacing `nil` values

Parameters ~
{field} (string): The field to change
• {new_val} (any): The new value of the field

dropbar_symbol_t:restore() *dropbar_symbol_t:restore()*

Restore the content of a dropbar symbol after
`dropbar_symbol_t:swap_field()` is called
Currently does not support restoring `nil` values
Restore the values of the fields of a dropbar symbol changed
in `dropbar_symbol_t:swap_field()`

..............................................................................
DROPBAR_MENU_T *dropbar-developers-classes-dropbar_menu_t*
Expand Down
12 changes: 8 additions & 4 deletions lua/dropbar/bar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -306,26 +306,29 @@ function dropbar_symbol_t:preview_restore_view()
end

---Temporarily change the content of a dropbar symbol
---Does not support replacing nil values
---@param field string
---@param new_val any
---@return nil
function dropbar_symbol_t:swap_field(field, new_val)
self.data = self.data or {}
self.data.swap = self.data.swap or {}
self.data.swapped = self.data.swapped or {}
self.data.swap[field] = self.data.swap[field] or self[field]
table.insert(self.data.swapped, field)
self[field] = new_val
end

---Restore the content of a dropbar symbol
---Does not support restoring nil values
---@return nil
function dropbar_symbol_t:restore()
if not self.data or not self.data.swap then
return
end
for field, val in pairs(self.data.swap) do
self[field] = val
for _, field in ipairs(self.data.swapped) do
self[field] = self.data.swap[field]
end
self.data.swap = nil
self.data.swapped = nil
end

---@class dropbar_opts_t
Expand All @@ -346,6 +349,7 @@ end
---@field components dropbar_symbol_t[]
---@field string_cache string
---@field in_pick_mode boolean?
---@field symbol_on_hover dropbar_symbol_t?
---@field last_update_request_time float? timestamp of the last update request in ms, see :h uv.hrtime()
local dropbar_t = {}
dropbar_t.__index = dropbar_t
Expand Down

0 comments on commit 31b6fe0

Please sign in to comment.