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,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