aboutsummaryrefslogtreecommitdiff
path: root/plugins/gui_filepicker.lua
diff options
context:
space:
mode:
authorkmafeni04 <88457139+kmafeni04@users.noreply.github.com>2023-10-09 15:12:26 +0100
committerGitHub <noreply@github.com>2023-10-09 16:12:26 +0200
commit9ee4822331ac98b8d1f2f51cd88daff0e7d1a4f3 (patch)
treee318e7553497af3303e32b0d1cee1d4d6b40caa5 /plugins/gui_filepicker.lua
parent1de82578d2125a4e6f71b5522152aeea7ab487fb (diff)
downloadlite-xl-plugins-9ee4822331ac98b8d1f2f51cd88daff0e7d1a4f3.tar.gz
lite-xl-plugins-9ee4822331ac98b8d1f2f51cd88daff0e7d1a4f3.zip
Graphical file picker using Zenity (#312)
* Add files via upload * Update manifest.json * Update manifest.json * Update and rename zenity.lua to gui_filepicker.lua
Diffstat (limited to 'plugins/gui_filepicker.lua')
-rw-r--r--plugins/gui_filepicker.lua210
1 files changed, 210 insertions, 0 deletions
diff --git a/plugins/gui_filepicker.lua b/plugins/gui_filepicker.lua
new file mode 100644
index 0000000..60a663f
--- /dev/null
+++ b/plugins/gui_filepicker.lua
@@ -0,0 +1,210 @@
+-- mod-version:3
+
+local core = require("core")
+local command = require("core.command")
+local common = require("core.common")
+local keymap = require("core.keymap")
+
+command.add(nil, {
+
+ ["gui-filepicker:open-file"] = function()
+ local zen, proc_err = process.start({
+ "zenity",
+ "--file-selection",
+ }, { stdout = process.REDIRECT_PIPE })
+ if not zen then
+ core.error("Unable to run zenity: %s", proc_err)
+ return
+ end
+
+ -- Run this in a coroutine, so we don't block the UI
+ core.add_thread(function()
+ local buffer = {}
+ while zen:running() do
+ local buf = zen:read_stdout()
+ if buf and #buf > 0 then
+ table.insert(buffer, buf)
+ end
+ coroutine.yield()
+ end
+ local abs_path = table.concat(buffer) or ""
+
+ -- Remove final newline zenity adds
+ abs_path = string.match(abs_path, "^[^\n]+") or ""
+ if #abs_path == 0 then
+ return
+ end
+ core.root_view:open_doc(core.open_doc(common.home_expand(abs_path)))
+ end)
+ end,
+
+ ["gui-filepicker:open-project-folder"] = function()
+ local zen, proc_err = process.start({
+ "zenity",
+ "--file-selection",
+ "--directory",
+ }, { stdout = process.REDIRECT_PIPE })
+ if not zen then
+ core.error("Unable to run zenity: %s", proc_err)
+ return
+ end
+
+ -- Run this in a coroutine, so we don't block the UI
+ core.add_thread(function()
+ local buffer = {}
+ while zen:running() do
+ local buf = zen:read_stdout()
+ if buf and #buf > 0 then
+ table.insert(buffer, buf)
+ end
+ coroutine.yield()
+ end
+ local abs_path = table.concat(buffer) or ""
+
+ -- Remove final newline zenity adds
+ abs_path = string.match(abs_path, "^[^\n]+") or ""
+ if #abs_path == 0 then
+ return
+ end
+
+ if abs_path == core.project_dir then
+ return
+ end
+ os.execute(string.format("%q %q", EXEFILE, abs_path))
+ end)
+ end,
+
+ ["gui-filepicker:change-project-folder"] = function()
+ local zen, proc_err = process.start({
+ "zenity",
+ "--file-selection",
+ "--directory",
+ }, { stdout = process.REDIRECT_PIPE })
+ if not zen then
+ core.error("Unable to run zenity: %s", proc_err)
+ return
+ end
+
+ -- Run this in a coroutine, so we don't block the UI
+ core.add_thread(function()
+ local buffer = {}
+ while zen:running() do
+ local buf = zen:read_stdout()
+ if buf and #buf > 0 then
+ table.insert(buffer, buf)
+ end
+ coroutine.yield()
+ end
+ local abs_path = table.concat(buffer) or ""
+
+ -- Remove final newline zenity adds
+ abs_path = string.match(abs_path, "^[^\n]+") or ""
+ if #abs_path == 0 then
+ return
+ end
+
+ if abs_path == core.project_dir then
+ return
+ end
+ core.confirm_close_docs(core.docs, function(dirpath)
+ core.open_folder_project(dirpath)
+ end, abs_path)
+ end)
+ end,
+
+ ["gui-filepicker:add-directory"] = function()
+ local zen, proc_err = process.start({
+ "zenity",
+ "--file-selection",
+ "--directory",
+ }, { stdout = process.REDIRECT_PIPE })
+ if not zen then
+ core.error("Unable to run zenity: %s", proc_err)
+ return
+ end
+
+ -- Run this in a coroutine, so we don't block the UI
+ core.add_thread(function()
+ local buffer = {}
+ while zen:running() do
+ local buf = zen:read_stdout()
+ if buf and #buf > 0 then
+ table.insert(buffer, buf)
+ end
+ coroutine.yield()
+ end
+ local abs_path = table.concat(buffer) or ""
+
+ -- Remove final newline zenity adds
+ abs_path = string.match(abs_path, "^[^\n]+") or ""
+ if #abs_path == 0 then
+ return
+ end
+
+ if abs_path == core.project_dir then
+ return
+ end
+ core.add_project_directory(system.absolute_path(abs_path))
+ end)
+ end,
+})
+
+command.add("core.docview", {
+
+ ["gui-filepicker:save-as"] = function(dv)
+ local text
+ text = text or "new_file"
+ local doc = dv.doc
+ if dv.doc.filename then
+ text = dv.doc.filename
+ end
+ local zen, proc_err = process.start({
+ "zenity",
+ "--file-selection",
+ "--save",
+ "--filename",
+ text,
+ }, { stdout = process.REDIRECT_PIPE })
+ if not zen then
+ core.error("Unable to run zenity: %s", proc_err)
+ return
+ end
+
+ -- Run this in a coroutine, so we don't block the UI
+ core.add_thread(function()
+ local buffer = {}
+ while zen:running() do
+ local buf = zen:read_stdout()
+ if buf and #buf > 0 then
+ table.insert(buffer, buf)
+ end
+ coroutine.yield()
+ end
+ local abs_path = table.concat(buffer) or ""
+
+ -- Remove final newline zenity adds
+ abs_path = string.match(abs_path, "^[^\n]+") or ""
+ if #abs_path == 0 then
+ return
+ end
+ doc:save(abs_path, abs_path)
+ core.log('Saved as "%s"', dv.doc.filename)
+ end)
+ end,
+
+ ["gui-filepicker:save"] = function(dv)
+ if dv.doc.filename then
+ command.perform("doc:save")
+ else
+ command.perform("gui-filepicker:save-as")
+ end
+ end,
+})
+
+keymap.add({
+ ["ctrl+s"] = "gui-filepicker:save",
+ ["ctrl+shift+s"] = "gui-filepicker:save-as",
+ ["ctrl+shift+c"] = "gui-filepicker:change-project-folder",
+ ["ctrl+o"] = "gui-filepicker:open-file",
+ ["ctrl+shift+o"] = "gui-filepicker:open-project-folder",
+})