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

Custom XDG Handler Support #17

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
52 changes: 52 additions & 0 deletions lua/git-dev/xdg.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
--- @class XDG_Handler
--- @field name string used for the filename
--- @field scheme string the actuall schem like: nvim-gitdev
--- @field wrapper string a wrapper script

local M = {}

M.desktop_file_location = "~/.local/share/applications
M.generated_comment = "#generated by git-dev.nvim "

---@param handler XDG_Handler
---@return boolean was_sucessfull
M.generate_xdg_handler_desktop_entry = function(handler)
local file_name = string.format("%s/%s.desktop", M.desktop_file_location, handler.name)
local file_content = string.format([[
%s
[Desktop Entry]
NoDisplay=true
Exec=%snvim -c "GitDevOpen %%u"
Terminal=true
Type=Application
MimeType=x-scheme-handler/%s
]],
M.generated_comment
handler.wrapper,
handler.scheme
)

local file = io.open(vim.fn.expand(file_name), "w")
if file then
file:write(file_content)
file:close()
end
return file ~= nil
end

---@param handler XDG_Handler
---@param callback function(job_id: number, exit_code: number):void
---@return number job_id
M.register_xdg_handler = function(handler, callback)
local cmd = {
"xdg-mime",
"default",
string.format("%s.desktop", handler.name),
string.format("x-scheme-handler/%s", handler.scheme),
}

local job_id = vim.fn.jobstart(cmd, { on_exit = callback })
return job_id
end

return M