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: use vim.system, add option passed to vim.system #65

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

## ⚡️ Requirements

* Neovim >= 0.5.0
* Neovim >= 0.10.0
* macOS (`open`), Linux (`xdg-open`) or Windows (`powershell.exe start explorer.exe`)

## 📦 Installation
Expand All @@ -38,14 +38,14 @@ require("lazy").setup({
init = function ()
vim.g.netrw_nogx = 1 -- disable netrw gx
end,
dependencies = { "nvim-lua/plenary.nvim" },
config = true, -- default settings
submodules = false, -- not needed, submodules are required only for tests

-- you can specify also another config if you want
config = function() require("gx").setup {
open_browser_app = "os_specific", -- specify your browser app; default for macOS is "open", Linux "xdg-open" and Windows "powershell.exe"
open_browser_args = { "--background" }, -- specify any arguments, such as --background for macOS' "open".
open_browser_optioins = {}, -- specify options passed to `vim.system`, see `vim.SystemOpts`
handlers = {
plugin = true, -- open plugin links in lua (e.g. packer, lazy, ..)
github = true, -- open github issues
Expand Down
7 changes: 4 additions & 3 deletions lua/gx/git.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
M = {}

local function parse_git_output(result)
if not result or #result < 1 then
if not result or #result < 0 then
return
end

local domain, repository = result[1]:gsub("%.git%s*$", ""):match("@(.*%..*):(.*)$")
result = vim.trim(result)
local domain, repository = result:gsub("%.git%s*$", ""):match("@(.*%..*):(.*)$")
if domain and repository then
return "https://" .. domain .. "/" .. repository
end

local url = result[1]:gsub("%.git%s*$", ""):match("^https?://.+")
local url = result:gsub("%.git%s*$", ""):match("^https?://.+")
if url then
return url
end
Expand Down
4 changes: 4 additions & 0 deletions lua/gx/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ local M = {}
---@class GxOptions
---@field open_browser_app string
---@field open_browser_args string[]
---@field open_browser_options table
---@field handlers (boolean | GxHandler)[]
---@field handler_options GxHandlerOptions | nil

Expand All @@ -43,6 +44,7 @@ function M.open(mode, line)
return require("gx.shell").execute_with_error(
M.options.open_browser_app,
M.options.open_browser_args,
M.options.open_browser_options,
urls[1].url
)
else
Expand All @@ -59,6 +61,7 @@ function M.open(mode, line)
return require("gx.shell").execute_with_error(
M.options.open_browser_app,
M.options.open_browser_args,
M.options.open_browser_options,
selected.url
)
end)
Expand Down Expand Up @@ -94,6 +97,7 @@ local function with_defaults(options)
return {
open_browser_app = options.open_browser_app or get_open_browser_app(),
open_browser_args = options.open_browser_args or get_open_browser_args(),
open_browser_options = {},
handlers = options.handlers or {},
handler_options = {
search_engine = options.handler_options.search_engine or "google",
Expand Down
18 changes: 8 additions & 10 deletions lua/gx/shell.lua
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
local shell = {}

function shell.execute(command, args)
-- TODO: This could use vim.system() in 0.10+
local Job = require("plenary.job")
function shell.execute(command, args, options)
local cmd = { command, unpack(args) }
options = options or {}
local opts = vim.tbl_extend("force", {}, options)

local result, return_val = Job:new({
command = command,
args = args,
}):sync()
local obj = vim.system(cmd, opts):wait()

return return_val, result
return obj.code, (obj.stdout or "")
end

function shell.execute_with_error(command, args, url)
function shell.execute_with_error(command, args, options, url)
local shell_args = {}
for _, v in ipairs(args) do
table.insert(shell_args, v)
end
table.insert(shell_args, url)

local return_val, _ = shell.execute(command, shell_args)
local return_val, _ = shell.execute(command, shell_args, options)

if return_val ~= 0 then
local ret = {}
Expand Down