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

feat: add ya.clipboard() Lua API #980

Merged
merged 3 commits into from
May 1, 2024

Conversation

mikavilpas
Copy link
Contributor

Fixes #964

It allows accessing the clipboard content as well as setting it.

-- get contents of the clipboard
local content = ya.clipboard()

-- set contents of the clipboard
ya.clipboard("new clipboard content")

Note: I moved the clipboard to a different crate because of circular dependency issues. Here's the commit message:

refactor: move CLIPBOARD from yazi-core -> yazi-shared

This needs to be done because we want to make it possible to use the
clipboard in lua plugins.

Before this change, the clipboard was only available in the yazi-core
crate, which depends on yazi-plugin.

The lua plugins are configured in the yazi-plugin crate, but it can't
use anything from yazi-core, because it would create a circular
dependency.

I should still look into testing this with the lua plugin api, so I'll keep this as a draft for now.

@mikavilpas mikavilpas marked this pull request as draft April 30, 2024 15:11
@mikavilpas mikavilpas force-pushed the add-ya-clipboard-lua-api branch from 102f614 to bd57bfc Compare April 30, 2024 16:19
@sxyazi
Copy link
Owner

sxyazi commented Apr 30, 2024

Thank you!

Please move the clipboard.rs to the yazi-plugin crate, as only yazi-core and yazi-plugin need it and yazi-plugin is already a dependency of yazi-core.

mikavilpas added 2 commits May 1, 2024 08:02
This needs to be done because we want to make it possible to use the
clipboard in lua plugins.

Before this change, the clipboard was only available in the yazi-core
crate, which depends on yazi-plugin.

The lua plugins are configured in the yazi-plugin crate, but it can't
use anything from yazi-core, because it would create a circular
dependency.
It allows accessing the clipboard content as well as setting it.

```lua
-- get contents of the clipboard
local content = ya.clipboard()

-- set contents of the clipboard
ya.clipboard("new clipboard content")
```
@mikavilpas mikavilpas force-pushed the add-ya-clipboard-lua-api branch from bd57bfc to c606e7f Compare May 1, 2024 05:03
@mikavilpas
Copy link
Contributor Author

Ok, that is now done. I'll try to write a test plugin to verify this works in lua, next.

@mikavilpas
Copy link
Contributor Author

I was able to test setting the clipboard and that works fine. I had trouble testing getting the clipboard contents, however.

I think I don't have a deep enough understanding of yazi plugins to solve this yet. Would you have the time to take a look?

-- /Users/mikavilpas/.config/yazi/plugins/clipboard-test.yazi/init.lua
return {
	entry = function(_, args)
		-- test setting the clipboard
		--
		local input = ya.input({
			title = "give me some input",
			position = { "top-center", y = 3, w = 40 },
		})

		ya.clipboard(input)

		-- test getting the clipboard
		--
		-- TODO this does not work as expected
		ya.dbg("testing clipboard")
		local contents = ya.clipboard()
		ya.dbg("The clipboard contents are: " .. contents)
	end,
}

@mikavilpas mikavilpas marked this pull request as ready for review May 1, 2024 06:14
@sxyazi
Copy link
Owner

sxyazi commented May 1, 2024

Just tested it, and it works great! 👍🏻

(Since it's an async function, so it needs to be ran in an async context, i.e. plugin command without --sync flag).

I think we can merge it now.

@sxyazi sxyazi merged commit 51c7f03 into sxyazi:main May 1, 2024
5 checks passed
@sxyazi sxyazi changed the title Add ya.clipboard() lua api faet: add ya.clipboard() lua api May 1, 2024
@sxyazi sxyazi changed the title faet: add ya.clipboard() lua api faet: add ya.clipboard() Lua API May 1, 2024
sxyazi added a commit that referenced this pull request May 1, 2024
@sxyazi sxyazi changed the title faet: add ya.clipboard() Lua API feat: add ya.clipboard() Lua API May 1, 2024
@mikavilpas
Copy link
Contributor Author

Oh, I see. Do you think this case could be detected, and a warning could be logged, for example?

@sxyazi
Copy link
Owner

sxyazi commented May 2, 2024

Do you mean detecting when async functions are called in a sync context? If so it would throw an error that shows up in the log file.

@mikavilpas
Copy link
Contributor Author

Ok, gotcha! I don't remember if I saw it in the log file or not, but I will keep my eyes on that in the future 🙂

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 2, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a new ya.clipboard() Lua API
2 participants