diff options
author | marshmallow <marshycity@gmail.com> | 2023-04-30 15:53:02 +1000 |
---|---|---|
committer | Justin M. Keyes <justinkz@gmail.com> | 2023-07-04 23:45:00 +0200 |
commit | af6e6ccf3dee815850639ec5613dda3442caa7d6 (patch) | |
tree | 8425b2b0dcf6592dd5f8a25d63187f337a047a2e /runtime/lua/vim/ui.lua | |
parent | 3ecd45ded044c47efa76b74e9e3b720fbe27adc7 (diff) | |
download | rneovim-af6e6ccf3dee815850639ec5613dda3442caa7d6.tar.gz rneovim-af6e6ccf3dee815850639ec5613dda3442caa7d6.tar.bz2 rneovim-af6e6ccf3dee815850639ec5613dda3442caa7d6.zip |
feat(vim.ui): vim.ui.open, "gx" without netrw
Co-authored-by: Mathias Fußenegger <mfussenegger@users.noreply.github.com>
Co-authored-by: Justin M. Keyes <justinkz@gmail.com>
Co-authored-by: ii14 <59243201+ii14@users.noreply.github.com>
Diffstat (limited to 'runtime/lua/vim/ui.lua')
-rw-r--r-- | runtime/lua/vim/ui.lua | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/runtime/lua/vim/ui.lua b/runtime/lua/vim/ui.lua index aaee175f3a..2200ee7bc3 100644 --- a/runtime/lua/vim/ui.lua +++ b/runtime/lua/vim/ui.lua @@ -104,4 +104,69 @@ function M.input(opts, on_confirm) end end +--- Opens a path in the system's default handler. +--- This function utilizes `xdg-open`, `wslview`, `explorer`, or `open` commands +--- depending on the system to open the provided path. +--- +--- Notifies the user if unsuccessful +--- +---@param path string Path to be opened +--- +---@return SystemCompleted|nil result Result of command, if an appropriate one +---could be found. +--- +---@see |vim.system| +--- +--- Example: +--- <pre>lua +--- vim.ui.open("https://neovim.io/") +--- +--- vim.ui.open("/path/to/file") +--- </pre> +function M.open(path) + if not path or path == '' then + vim.notify('os_open: No path provided', vim.log.levels.ERROR) + return nil + end + + local cmd + + if vim.fn.has('macunix') == 1 then + cmd = { 'open', path } + elseif vim.fn.has('win32') == 1 then + cmd = { 'explorer', path } + else + if vim.fn.executable('wslview') == 1 then + cmd = { 'wslview', path } + elseif vim.fn.executable('xdg-open') == 1 then + cmd = { 'xdg-open', path } + else + vim.notify( + 'os_open: Could not find an appropriate command to use (Is xdg-open installed?)', + vim.log.levels.ERROR + ) + + return nil + end + end + + local ret = vim + .system(cmd, { + text = true, + detach = true, + }) + :wait() + + if ret.code ~= 0 then + local msg = { + 'Failed to open path', + ret, + vim.inspect(cmd), + } + vim.notify(table.concat(msg, '\n'), vim.log.levels.ERROR) + end + + return ret +end + return M |