aboutsummaryrefslogtreecommitdiff
path: root/plugins
diff options
context:
space:
mode:
authorGuldoman <giulio.lettieri@gmail.com>2023-10-22 13:09:31 +0200
committerGitHub <noreply@github.com>2023-10-22 13:09:31 +0200
commite0f0abb5d24af77f0afe057cd8e17fb120ca971c (patch)
treece2143bc6a3e771e1d9afbab587f319cd1699395 /plugins
parent674860ca09fd989b18409aa8021dece06418c5dd (diff)
downloadlite-xl-plugins-e0f0abb5d24af77f0afe057cd8e17fb120ca971c.tar.gz
lite-xl-plugins-e0f0abb5d24af77f0afe057cd8e17fb120ca971c.zip
`gui_filepicker`: simplify code and replace `ToolbarView` commands (#317)
* `gui_filepicker`: Simplify code * `gui_filepicker`: Replace `ToolbarView` commands
Diffstat (limited to 'plugins')
-rw-r--r--plugins/gui_filepicker.lua251
1 files changed, 92 insertions, 159 deletions
diff --git a/plugins/gui_filepicker.lua b/plugins/gui_filepicker.lua
index 60a663f..edceb8f 100644
--- a/plugins/gui_filepicker.lua
+++ b/plugins/gui_filepicker.lua
@@ -4,192 +4,114 @@ local core = require("core")
local command = require("core.command")
local common = require("core.common")
local keymap = require("core.keymap")
+local TreeView = require("plugins.treeview")
+
+local function run_zenity(options, callback)
+ local zen, proc_err = process.start({
+ "zenity",
+ table.unpack(options)
+ })
+ 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 = {}
+ repeat
+ local buf = zen:read_stdout()
+ if buf and #buf > 0 then
+ table.insert(buffer, buf)
+ end
+ coroutine.yield()
+ until not buf
+ local abs_path = table.concat(buffer) or ""
-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)
+ -- Remove final newline zenity adds
+ abs_path = string.match(abs_path, "^[^\n]+") or ""
+ if #abs_path == 0 then
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 ""
+ callback(abs_path)
+ end)
+end
- -- Remove final newline zenity adds
- abs_path = string.match(abs_path, "^[^\n]+") or ""
- if #abs_path == 0 then
- return
+command.add(nil, {
+ ["gui-filepicker:open-file"] = function()
+ run_zenity(
+ {
+ "--file-selection"
+ },
+ function(abs_path)
+ core.root_view:open_doc(core.open_doc(common.home_expand(abs_path)))
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)
+ run_zenity(
+ {
+ "--file-selection",
+ "--directory",
+ },
+ function(abs_path)
+ if abs_path == core.project_dir then
+ return
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
+ os.execute(string.format("%q %q", EXEFILE, abs_path))
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)
+ run_zenity(
+ {
+ "--file-selection",
+ "--directory",
+ },
+ function(abs_path)
+ if abs_path == core.project_dir then
+ return
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
+ core.confirm_close_docs(core.docs, function(dirpath)
+ core.open_folder_project(dirpath)
+ end, abs_path)
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)
+ run_zenity(
+ {
+ "--file-selection",
+ "--directory",
+ },
+ function(abs_path)
+ if abs_path == core.project_dir then
+ return
end
- coroutine.yield()
+ core.add_project_directory(system.absolute_path(abs_path))
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
+ run_zenity(
+ {
+ "--file-selection",
+ "--save",
+ "--filename",
+ dv.doc.filename or "new_file",
+ },
+ function(abs_path)
+ dv.doc:save(abs_path, abs_path)
+ core.log('Saved as "%s"', dv.doc.filename)
end
- doc:save(abs_path, abs_path)
- core.log('Saved as "%s"', dv.doc.filename)
- end)
+ )
end,
["gui-filepicker:save"] = function(dv)
@@ -208,3 +130,14 @@ keymap.add({
["ctrl+o"] = "gui-filepicker:open-file",
["ctrl+shift+o"] = "gui-filepicker:open-project-folder",
})
+
+local replacements = {
+ ["core:open-file"] = "gui-filepicker:open-file",
+ ["doc:save"] = "gui-filepicker:save",
+}
+for _, v in ipairs(TreeView.toolbar.toolbar_commands) do
+ if replacements[v.command] then
+ v.command = replacements[v.command]
+ end
+end
+