refactor: try to remove dependency for plenary.curl

This commit is contained in:
JuanZoran 2023-03-10 11:23:22 +08:00
parent a5164bf052
commit 8dd538ba60
7 changed files with 136 additions and 43 deletions

View File

@ -1,6 +1,7 @@
local M = {}
local M = {}
local baidu = require('Trans').conf.engine.baidu
local baidu = require('Trans').conf.engines.baidu
local app_id = baidu.app_id
local app_passwd = baidu.app_passwd
local salt = tostring(math.random(bit.lshift(1, 15)))
@ -47,10 +48,10 @@ M.query = function(data)
end
data.result = false
data.error = res
data.trace = res
end
require('plenary.curl').get(uri, {
require('Trans.wrapper.curl').get(uri, {
query = M.get_content(data),
callback = handle,
})

View File

@ -1,5 +1,5 @@
return function()
-- INFO :Chceck ultimate.db exists
-- INFO :Check ultimate.db exists
local dir = require('Trans').conf.dir
local path = dir .. '/ultimate.db'
if vim.fn.filereadable(path) == 1 then
@ -13,29 +13,32 @@ return function()
-- INFO :Download ultimate.db
local uri = 'https://github.com/skywind3000/ECDICT-ultimate/releases/download/1.0.0/ecdict-ultimate-sqlite.zip'
local loc = dir .. '/ultimate.zip'
require('plenary.curl').get(uri, {
output = loc,
callback = function(output)
if output.exsit == 0 and output.status == 200 then
if vim.fn.executable('unzip') == 0 then
vim.notify('unzip not found, Please unzip ' .. loc .. 'manually', vim.log.ERROR)
return
end
local cmd = string.format('unzip %s -d %s', path, dir)
os.execute(cmd)
os.remove(path)
vim.notify('Download database successfully', vim.log.INFO)
local handle = function(output)
if output.exit == 0 and vim.fn.filereadable(loc) then
if vim.fn.executable('unzip') == 0 then
vim.notify('unzip not found, Please unzip ' .. loc .. 'manually', vim.log.ERROR)
return
end
local debug_message = 'Download database failed:' .. vim.inspect(output)
vim.notify(debug_message, vim.log.ERROR)
end,
local cmd = string.format('unzip %s -d %s', path, dir)
local status = os.execute(cmd)
os.remove(path)
if status == 0 then
vim.notify('Download database successfully', vim.log.INFO)
return
end
end
local debug_message = 'Download database failed:' .. vim.inspect(output)
vim.notify(debug_message, vim.log.ERROR)
end
require('Trans.wrapper.curl').get(uri, {
output = loc,
callback = handle,
})
-- INFO : tts dependencies
-- INFO : Install tts dependencies
if vim.fn.has('linux') == 0 and vim.fn.has('mac') == 0 then
os.execute('cd ./tts/ && npm install')
end

View File

@ -3,22 +3,10 @@ return function(opts)
if opts then
M.conf = vim.tbl_deep_extend('force', M.conf, opts)
end
local conf = M.conf
local set_hl = vim.api.nvim_set_hl
local hls = require('Trans.style.theme')[conf.theme]
local hls = require('Trans.style.theme')[M.conf.theme]
for hl, opt in pairs(hls) do
set_hl(0, hl, opt)
end
local path = vim.fn.expand("$HOME/.vim/dict/Trans.json")
local file = io.open(path, "r")
if file then
local content = file:read("*a")
file:close()
local status, engine = pcall(vim.json.decode, content)
assert(status, 'Unable to parse json file: ' .. path)
conf.engine = engine
end
end

View File

@ -59,7 +59,8 @@ local check = function()
ok [[ultimate database found ]]
else
error [[Stardict database not found
Please check the doc in github: [https://github.com/JuanZoran/Trans.nvim]
[Manually]: Please check the doc in github: [https://github.com/JuanZoran/Trans.nvim]
[Automatically]: Try to run `:lua require "Trans".install()`
]]
end
@ -69,11 +70,11 @@ local check = function()
local file = io.open(path, "r")
local valid = file and pcall(vim.json.decode, file:read("*a"))
if valid then
ok [[Engine configuration file found and valid ]]
ok(string.format([[Engine configuration file[%s] found and valid ]], path))
else
error [[Engine configuration file not found or invalid
error(string.format([[Engine configuration file not found[%s] or invalid
Please check the doc in github: [https://github.com/JuanZoran/Trans.nvim]
]]
]], path))
end
end

View File

@ -5,12 +5,11 @@ local curl = {}
---@param opts table
curl.GET = function(opts)
local uri = opts.uri
local headers = opts.headers
local headers = opts.headers or {}
local callback = opts.callback
-- INFO :Init Curl command with {s}ilent and {G}et
local cmd = { 'curl', '-Gs' }
local callback = opts.callback
-- INFO :Add headers
for k, v in pairs(headers) do

View File

@ -0,0 +1,75 @@
local curl = {}
curl.get = function(uri, opts)
local query = opts.query
local headers = opts.headers
local callback = opts.callback
local output = opts.output
-- INFO :Init Curl command with {s}ilent and {G}et
local cmd = { 'curl', '-Gs' }
-- INFO :Add headers
if headers then
for k, v in pairs(headers) do
cmd[#cmd + 1] = ([[-H '%s: %s']]):format(k, v)
end
end
-- INFO :Store output to file
if output then
cmd[#cmd + 1] = [[-o ]] .. output
end
-- INFO :Add arguments
if query then
local info = {}
for k, v in pairs(query) do
info[#info + 1] = ('%s=%s'):format(k, v)
end
cmd[#cmd + 1] = ([['%s?%s']]):format(uri, table.concat(info, '&'))
else
cmd[#cmd + 1] = uri
end
-- INFO : Start a job
local outputs = {}
local on_stdout = function(_, stdout)
local str = table.concat(stdout)
if str ~= '' then
outputs[#outputs + 1] = str
end
end
local error = {}
local on_stderr = function(_, stderr)
error[#error + 1] = table.concat(stderr)
end
local on_exit = function(_, exit)
if callback then
callback {
exit = exit,
body = table.concat(outputs),
error = error
}
end
end
vim.pretty_print(table.concat(cmd, ' '))
vim.fn.jobstart(table.concat(cmd, ' '), {
stdin = 'null',
on_stdout = on_stdout,
on_stderr = on_stderr,
on_exit = on_exit,
})
end
---- TODO :
-- curl.post = function ()
--
-- end
return curl

View File

@ -1,5 +1,6 @@
local api, fn = vim.api, vim.fn
--- INFO :Define string play method
if fn.has('linux') == 1 then
string.play = function(self)
local cmd = ([[echo "%s" | festival --tts]]):format(self)
@ -18,6 +19,8 @@ else
end
end
--- INFO :Define plugin command
local M = require('Trans')
local command = api.nvim_create_user_command
@ -29,4 +32,27 @@ command('TransPlay', function()
end
end, { desc = ' 自动发音' })
--- INFO :Parse online engines config file
local path = vim.fn.expand("$HOME/.vim/dict/Trans.json")
local file = io.open(path, "r")
if file then
local content = file:read("*a")
file:close()
local status, engines = pcall(vim.json.decode, content)
assert(status, 'Unable to parse json file: ' .. path)
engines = engines or {}
for k, v in pairs(engines) do
if not v.enable then
engines[k] = nil
end
end
M.conf.engines = engines
else
M.conf.engines = {}
end
-- new_command('TranslateInput', function() M.translate { mode = 'i' } end, { desc = ' 搜索翻译', })