Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Toggle command #287

Closed
1 task done
ionztorm opened this issue Feb 2, 2025 · 6 comments
Closed
1 task done

feature: Toggle command #287

ionztorm opened this issue Feb 2, 2025 · 6 comments
Labels
enhancement New feature or request
Milestone

Comments

@ionztorm
Copy link

ionztorm commented Feb 2, 2025

Did you check the documentation?

  • I have read all the docs

Is your feature request related to a problem? Please describe.

No problems, just a convenience. This is great by the way - I keep all my work in cloud storage so that I can access between devices without having to work with git when switching device. None of the existing solutions are able to detect changes to my files using this method, but your solution does work.

Describe the solution you'd like

It would be great if there was a command like :LivePreview toggle which turns it on if it's off, and turns it off if it's on so that we can bind it to a single key map.

Describe alternatives you've considered

The alternative is 2 key maps to start and stop.

Additional context

No response

@ionztorm ionztorm added the enhancement New feature or request label Feb 2, 2025
Copy link
Contributor

github-actions bot commented Feb 2, 2025

Thank you for your issue! We appreciate your feedback and will look into it as soon as possible. Please make sure to include all necessary details (including a minimal config that can reproduce the error) to help us assist you better. Pull requests are also welcome!

@ionztorm
Copy link
Author

ionztorm commented Feb 2, 2025

Note, I have attempted to simulate this with my custom util table as follows:

M.live_preview_status = false

M.toggle_live_preview = function()
	if M.live_preview_status then
		vim.cmd("LivePreview close")
		M.live_preview_status = false
		print("LivePreview stopped")
	else
		vim.cmd("execute 'LivePreview start'")
		M.live_preview_status = true
		print("LivePreview started")
	end
end

However it doesn't load the browser immediately using this method. Instead it requires me to hit enter and then change window and then it loads the browser

@brianhuster brianhuster added this to the Need owners milestone Feb 2, 2025
@ionztorm
Copy link
Author

ionztorm commented Feb 2, 2025

update:

This works, but I have no idea how to code this in such a way that I could help you out with a PR

M.live_preview_status = false

M.toggle_live_preview = function()
	if M.live_preview_status then
		vim.cmd("LivePreview close")
		M.live_preview_status = false
		print("LivePreview stopped")
	else

		local filepath = vim.api.nvim_buf_get_name(0)

		if filepath == "" or filepath == nil then
			print("Error: No file detected for LivePreview.")
			return
		end

		local escaped_filepath = vim.fn.fnameescape(filepath)

		vim.api.nvim_feedkeys(":LivePreview start " .. escaped_filepath .. "\n", "n", false)

		vim.defer_fn(function()
			vim.cmd("redraw!")
		end, 100)

		M.live_preview_status = true
		print("LivePreview started for: " .. filepath)
	end
end

@brianhuster
Copy link
Owner

brianhuster commented Feb 2, 2025

I just add a function require('livepreview').is_running() that you can use for it.

However, I'm not sure the use case of such toggle feature. live-preview.nvim would automatically close an existing server before starting a new one (possibly this should be documented)

@ionztorm
Copy link
Author

ionztorm commented Feb 2, 2025

Ahh if it does that automatically then no need thanks though!

@brianhuster
Copy link
Owner

So let me close this for now

@brianhuster brianhuster closed this as not planned Won't fix, can't repro, duplicate, stale Feb 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants