Skip to content

Commit

Permalink
feat: make programs to open URL's configurable (open_programs) (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
saecki authored Apr 23, 2023
1 parent 1d4bb1e commit 5a529df
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 28 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ require('crates').setup {
thousands_separator = ".",
notification_title = "Crates",
curl_args = { "-sL", "--retry", "1" },
open_programs = { "xdg-open", "open" },
disable_invalid_feature_diagnostic = false,
text = {
loading = "  Loading",
Expand Down
7 changes: 7 additions & 0 deletions doc/crates.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ For more information about individual config options see |crates-config|.
thousands_separator = ".",
notification_title = "Crates",
curl_args = { "-sL", "--retry", "1" },
open_programs = { "xdg-open", "open" },
disable_invalid_feature_diagnostic = false,
text = {
loading = "  Loading",
Expand Down Expand Up @@ -513,6 +514,12 @@ curl_args *crates-config-curl_args*
The title displayed in notifications.


open_programs *crates-config-open_programs*
Type: `table`, Default: `{ "xdg-open", "open" }`

A list of programs that used to open urls.


*crates-config-disable_invalid_feature_diagnostic*
disable_invalid_feature_diagnostic
Type: `boolean`, Default: `false`
Expand Down
8 changes: 8 additions & 0 deletions lua/crates/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ local M = {Config = {TextConfig = {}, HighlightConfig = {}, DiagnosticConfig = {






local Config = M.Config
Expand Down Expand Up @@ -325,6 +326,13 @@ entry(M.schema, "curl_args", {
The title displayed in notifications.
]],
})
entry(M.schema, "open_programs", {
type = "table",
default = { "xdg-open", "open" },
description = [[
A list of programs that used to open urls.
]],
})

entry(M.schema, "disable_invalid_feature_diagnostic", {
type = "boolean",
Expand Down
22 changes: 14 additions & 8 deletions lua/crates/health.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local M = {}

local state = require("crates.state")
local util = require("crates.util")

local health_start = vim.fn["health#report_start"]
Expand All @@ -8,7 +9,7 @@ local health_warn = vim.fn["health#report_warn"]
local health_error = vim.fn["health#report_error"]

function M.check()
health_start("Checking for required plugins")
health_start("Checking required plugins")
if util.lualib_installed("plenary") then
health_ok("plenary.nvim installed")
else
Expand All @@ -20,19 +21,24 @@ function M.check()
health_warn("null-ls.nvim not found")
end

health_start("Checking for external dependencies")
health_start("Checking external dependencies")
if util.binary_installed("curl") then
health_ok("curl installed")
else
health_error("curl not found")
end

if util.binary_installed("xdg-open") then
health_ok("xdg-open installed")
elseif util.binary_installed("open") then
health_ok("open installed")
else
health_warn("xdg-open or open not found")
local num = 0
for _, prg in ipairs(state.cfg.open_programs) do
if util.binary_installed(prg) then
health_ok(string.format("%s installed", prg))
num = num + 1
end
end

if num == 0 then
local programs = table.concat(state.cfg.open_programs, " ")
health_warn("none of the following are installed " .. programs)
end
end

Expand Down
13 changes: 7 additions & 6 deletions lua/crates/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,14 @@ function M.crates_io_url(name)
end

function M.open_url(url)
if M.binary_installed("xdg-open") then
vim.cmd("silent !xdg-open " .. url)
elseif M.binary_installed("open") then
vim.cmd("silent !open " .. url)
else
M.notify(vim.log.levels.WARN, "Couldn't open url")
for _, prg in ipairs(state.cfg.open_programs) do
if M.binary_installed(prg) then
vim.cmd(string.format("silent !%s %s", prg, url))
return
end
end

M.notify(vim.log.levels.WARN, "Couldn't open url")
end

return M
8 changes: 8 additions & 0 deletions teal/crates/config.tl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local record M
thousands_separator: string
notification_title: string
curl_args: {string}
open_programs: {string}
disable_invalid_feature_diagnostic: boolean
text: TextConfig
highlight: HighlightConfig
Expand Down Expand Up @@ -325,6 +326,13 @@ entry(M.schema, "curl_args", {
The title displayed in notifications.
]],
})
entry(M.schema, "open_programs", {
type = "table",
default = { "xdg-open", "open" },
description = [[
A list of programs that used to open urls.
]],
})
-- TODO: use cargo metadata to get feature renames
entry(M.schema, "disable_invalid_feature_diagnostic", {
type = "boolean",
Expand Down
22 changes: 14 additions & 8 deletions teal/crates/health.tl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local record M end

local state = require("crates.state")
local util = require("crates.util")

local health_start = vim.fn["health#report_start"]
Expand All @@ -8,7 +9,7 @@ local health_warn = vim.fn["health#report_warn"]
local health_error = vim.fn["health#report_error"]

function M.check()
health_start("Checking for required plugins")
health_start("Checking required plugins")
if util.lualib_installed("plenary") then
health_ok("plenary.nvim installed")
else
Expand All @@ -20,19 +21,24 @@ function M.check()
health_warn("null-ls.nvim not found")
end

health_start("Checking for external dependencies")
health_start("Checking external dependencies")
if util.binary_installed("curl") then
health_ok("curl installed")
else
health_error("curl not found")
end

if util.binary_installed("xdg-open") then
health_ok("xdg-open installed")
elseif util.binary_installed("open") then
health_ok("open installed")
else
health_warn("xdg-open or open not found")
local num = 0
for _, prg in ipairs(state.cfg.open_programs) do
if util.binary_installed(prg) then
health_ok(string.format("%s installed", prg))
num = num + 1
end
end

if num == 0 then
local programs = table.concat(state.cfg.open_programs, " ")
health_warn("none of the following are installed " .. programs)
end
end

Expand Down
13 changes: 7 additions & 6 deletions teal/crates/util.tl
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,14 @@ function M.crates_io_url(name: string): string
end

function M.open_url(url: string)
if M.binary_installed("xdg-open") then
vim.cmd("silent !xdg-open " .. url)
elseif M.binary_installed("open") then
vim.cmd("silent !open " .. url)
else
M.notify(vim.log.levels.WARN, "Couldn't open url")
for _, prg in ipairs(state.cfg.open_programs) do
if M.binary_installed(prg) then
vim.cmd(string.format("silent !%s %s", prg, url))
return
end
end

M.notify(vim.log.levels.WARN, "Couldn't open url")
end

return M

0 comments on commit 5a529df

Please sign in to comment.