Skip to content

Commit 78db456

Browse files
authored
Merge pull request #182 from 3rd/feat/toggle-rendering
feat: toggle rendering
2 parents 500c01c + 2eacd30 commit 78db456

File tree

5 files changed

+70
-3
lines changed

5 files changed

+70
-3
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,21 @@ Check https://github.com/3rd/image.nvim/issues/190#issuecomment-2378156235 for h
398398

399399
## How to ...?
400400

401+
#### General
402+
403+
<details>
404+
<summary>Enable / disable / get plugin status</summary>
405+
406+
You can enable/disable the plugin and check its status on demand.
407+
408+
```lua
409+
require("image").enable() -- enable the plugin
410+
require("image").disable() -- disable the plugin
411+
print(require("image").is_enabled()) -- bool
412+
```
413+
414+
</details>
415+
401416
#### Integrations
402417

403418
<details>
@@ -415,7 +430,7 @@ require("image").setup({
415430
})
416431
```
417432

418-
</details
433+
</details>
419434

420435
## API
421436

lua/image/image.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ end
4545

4646
---@param geometry? ImageGeometry
4747
function Image:render(geometry)
48+
if not self.global_state.enabled then return end
49+
4850
if geometry then self.geometry = vim.tbl_deep_extend("force", self.geometry, geometry) end
4951

5052
-- don't render if we are in the conmmand-line-window, in this case previously rendered images can

lua/image/init.lua

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ local state = {
5353
tmp_dir = vim.fn.tempname(),
5454
disable_decorator_handling = false,
5555
hijacked_win_buf_images = {},
56+
enabled = true,
5657
}
5758

5859
---@type API
@@ -102,6 +103,9 @@ api.setup = function(options)
102103
local window_history = {}
103104
vim.api.nvim_set_decoration_provider(state.extmarks_namespace, {
104105
on_win = function(_, winid, bufnr, topline, botline)
106+
-- bail if not enabled
107+
if not state.enabled then return false end
108+
105109
-- bail if decorator handling is disabled
106110
if state.disable_decorator_handling then return false end
107111

@@ -208,6 +212,9 @@ api.setup = function(options)
208212
vim.api.nvim_create_autocmd({ "BufLeave", "WinClosed", "TabEnter" }, {
209213
group = group,
210214
callback = function() -- auto-clear images when windows and buffers change
215+
-- bail if not enabled
216+
if not state.enabled then return end
217+
211218
vim.schedule(function()
212219
local images = api.get_images()
213220

@@ -251,6 +258,9 @@ api.setup = function(options)
251258
vim.api.nvim_create_autocmd({ "WinScrolled" }, {
252259
group = group,
253260
callback = function(au)
261+
-- bail if not enabled
262+
if not state.enabled then return end
263+
254264
local images = api.get_images({ window = tonumber(au.file) })
255265
for _, current_image in ipairs(images) do
256266
current_image:render()
@@ -262,6 +272,9 @@ api.setup = function(options)
262272
vim.api.nvim_create_autocmd({ "WinResized" }, {
263273
group = group,
264274
callback = function()
275+
-- bail if not enabled
276+
if not state.enabled then return end
277+
265278
local images = api.get_images()
266279
for _, current_image in ipairs(images) do
267280
if current_image.window ~= nil then current_image:render() end
@@ -303,9 +316,11 @@ api.setup = function(options)
303316
vim.api.nvim_create_autocmd("FocusLost", {
304317
group = group,
305318
callback = function() -- auto-clear images when windows and buffers change
319+
-- bail if not enabled
320+
if not state.enabled then return end
321+
306322
vim.schedule(function()
307323
-- utils.debug("FocusLost")
308-
309324
if
310325
state.options.editor_only_render_when_focused
311326
or (utils.tmux.is_tmux and utils.tmux.get_window_id() ~= initial_tmux_window_id)
@@ -328,6 +343,9 @@ api.setup = function(options)
328343
vim.api.nvim_create_autocmd("FocusGained", {
329344
group = group,
330345
callback = function() -- auto-clear images when windows and buffers change
346+
-- bail if not enabled
347+
if not state.enabled then return end
348+
331349
-- utils.debug("FocusGained")
332350

333351
state.disable_decorator_handling = false
@@ -362,6 +380,9 @@ api.setup = function(options)
362380
group = group,
363381
pattern = state.options.hijack_file_patterns,
364382
callback = function(event)
383+
-- bail if not enabled
384+
if not state.enabled then return end
385+
365386
local buf = event.buf
366387
local win = vim.api.nvim_get_current_win()
367388
local path = vim.api.nvim_buf_get_name(buf)
@@ -375,6 +396,9 @@ api.setup = function(options)
375396
vim.api.nvim_create_autocmd({ "BufWritePost", "TextChanged", "TextChangedI", "InsertEnter" }, {
376397
group = group,
377398
callback = function(event)
399+
-- bail if not enabled
400+
if not state.enabled then return end
401+
378402
local images = api.get_images({ buffer = event.buf })
379403
for _, img in ipairs(images) do
380404
local has_moved, extmark_y, extmark_x = img:has_extmark_moved()
@@ -492,4 +516,25 @@ api.create_report = function()
492516
return report.create(state)
493517
end
494518

519+
---@return boolean
520+
api.is_enabled = function()
521+
return state.enabled
522+
end
523+
524+
api.enable = function()
525+
state.enabled = true
526+
local images = api.get_images()
527+
for _, current_image in ipairs(images) do
528+
current_image:render()
529+
end
530+
end
531+
532+
api.disable = function()
533+
state.enabled = false
534+
local images = api.get_images()
535+
for _, current_image in ipairs(images) do
536+
current_image:clear(true)
537+
end
538+
end
539+
495540
return api

lua/image/utils/document.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ local create_document_integration = function(config)
5252
local render = vim.schedule_wrap(
5353
---@param ctx IntegrationContext
5454
function(ctx)
55-
local windows = utils.window.get_windows({ normal = true, floating = ctx.options.floating_windows })
55+
if not ctx.state.enabled then return end
5656

57+
local windows = utils.window.get_windows({ normal = true, floating = ctx.options.floating_windows })
5758
local image_queue = {}
5859

5960
for _, window in ipairs(windows) do

lua/types.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,12 @@
77
---@field clear fun(id?: string)
88
---@field get_images fun(opts?: { window?: number, buffer?: number, namespace?: string }): Image[]
99
---@field hijack_buffer fun(path: string, window?: number, buffer?: number, options?: ImageOptions): Image|nil
10+
---@field is_enabled fun(): boolean
11+
---@field enable fun()
12+
---@field disable fun()
1013

1114
---@class State
15+
---@field enabled boolean
1216
---@field backend Backend
1317
---@field options Options
1418
---@field images { [string]: Image }

0 commit comments

Comments
 (0)