2022-05-25 04:36:34 +00:00
|
|
|
local utils = require("go.utils")
|
|
|
|
local log = utils.log
|
2022-06-28 04:36:30 +00:00
|
|
|
local runner = require("go.runner")
|
2022-05-25 04:36:34 +00:00
|
|
|
local curl = "curl"
|
|
|
|
local run = function(...)
|
|
|
|
local query = select(1, ...)
|
2022-05-27 01:39:29 +00:00
|
|
|
if query == nil then
|
|
|
|
query = vim.fn.expand("<cword>")
|
|
|
|
end
|
|
|
|
local cmd = string.format("%s cht.sh/go/%s?T", curl, query)
|
2022-06-21 09:04:30 +00:00
|
|
|
cmd = vim.split(cmd, " ")
|
2022-05-27 01:39:29 +00:00
|
|
|
log(cmd)
|
2022-06-28 04:36:30 +00:00
|
|
|
local opts = {
|
|
|
|
on_exit = function(code, signal, output)
|
|
|
|
if code ~= 0 or signal ~= 0 then
|
|
|
|
log(string.format("command exited with code: %s, signal: %s", code, signal))
|
|
|
|
return
|
|
|
|
end
|
|
|
|
vim.schedule(function()
|
|
|
|
local data = vim.split(output, "\n")
|
|
|
|
data = utils.handle_job_data(data)
|
2022-05-25 04:36:34 +00:00
|
|
|
|
2022-06-28 04:36:30 +00:00
|
|
|
if #data > 4 then
|
|
|
|
data = vim.list_slice(data, 4, #data)
|
|
|
|
local TextView = utils.load_plugin("guihua.lua", "guihua.textview")
|
|
|
|
if TextView then
|
|
|
|
local win = TextView:new({
|
|
|
|
loc = "top_center",
|
|
|
|
syntax = "go",
|
|
|
|
rect = { height = #data, pos_x = 0, pos_y = 4 },
|
|
|
|
data = data,
|
|
|
|
enter = true,
|
|
|
|
})
|
|
|
|
log("draw data", data)
|
|
|
|
win:on_draw(data)
|
|
|
|
else
|
|
|
|
local name = vim.fn.tempname() .. ".go"
|
|
|
|
vim.fn.writefile(data, name)
|
|
|
|
cmd = " silent exe 'e " .. name .. "'"
|
|
|
|
vim.cmd(cmd)
|
|
|
|
vim.cmd("e")
|
|
|
|
end
|
|
|
|
else
|
|
|
|
vim.notify("No result " .. vim.inspect(data))
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end,
|
|
|
|
}
|
2022-05-25 04:36:34 +00:00
|
|
|
|
2022-06-28 04:36:30 +00:00
|
|
|
runner.run(cmd, opts)
|
2022-05-25 04:36:34 +00:00
|
|
|
end
|
2022-05-27 01:39:29 +00:00
|
|
|
return { run = run }
|