diff --git a/README.md b/README.md index 02138e13..447d2042 100644 --- a/README.md +++ b/README.md @@ -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
*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
*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` diff --git a/doc/dropbar.txt b/doc/dropbar.txt index b6f70a51..7c1c4b05 100644 --- a/doc/dropbar.txt +++ b/doc/dropbar.txt @@ -1401,7 +1401,6 @@ 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 @@ -1409,9 +1408,8 @@ dropbar_symbol_t:swap_field({field}, {new_val}) 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* diff --git a/lua/dropbar/bar.lua b/lua/dropbar/bar.lua index f23a63f5..2f866fde 100644 --- a/lua/dropbar/bar.lua +++ b/lua/dropbar/bar.lua @@ -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 @@ -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