fix: when download abort then continue last time
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
---@class TransConf
|
||||
return {
|
||||
---@type string the directory for database file and password file
|
||||
dir = require 'Trans'.relative_path({ 'extra' }, true),
|
||||
dir = require 'Trans'.plugin_dir,
|
||||
debug = true,
|
||||
---@type 'default' | 'dracula' | 'tokyonight' global Trans theme [see lua/Trans/style/theme.lua]
|
||||
theme = 'default', -- default | tokyonight | dracula
|
||||
|
@@ -18,18 +18,19 @@ local curl = {}
|
||||
---Send a GET request use curl
|
||||
---@param uri string uri for request
|
||||
---@param opts
|
||||
---| { query?: table<string, string>, output?: string, headers?: table<string, string>, callback: fun(result: RequestResult) }
|
||||
---| { query?: table<string, string>, output?: string, headers?: table<string, string>, callback: fun(result: RequestResult), extra?: string[] }
|
||||
function curl.get(uri, opts)
|
||||
local query = opts.query
|
||||
local output = opts.output
|
||||
local headers = opts.headers
|
||||
local callback = opts.callback
|
||||
local extra = opts.extra
|
||||
|
||||
|
||||
-- INFO :Init Curl command with {s}ilent and {G}et
|
||||
local cmd = { 'curl', '-GLs', uri }
|
||||
local cmd = vim.list_extend({ 'curl', '-GLs', uri }, extra or {})
|
||||
local size = #cmd
|
||||
local function insert(value)
|
||||
local function add(value)
|
||||
size = size + 1
|
||||
cmd[size] = value
|
||||
end
|
||||
@@ -37,20 +38,20 @@ function curl.get(uri, opts)
|
||||
-- INFO :Add headers
|
||||
if headers then
|
||||
for k, v in pairs(headers) do
|
||||
insert(('-H %q: %q'):format(k, v))
|
||||
add(('-H %q: %q'):format(k, v))
|
||||
end
|
||||
end
|
||||
|
||||
-- INFO :Add arguments
|
||||
if query then
|
||||
for k, v in pairs(query) do
|
||||
insert(('--data-urlencode %q=%q'):format(k, v))
|
||||
add(('--data-urlencode %q=%q'):format(k, v))
|
||||
end
|
||||
end
|
||||
|
||||
-- INFO :Store output to file
|
||||
if output then
|
||||
insert(('-o %q'):format(output))
|
||||
add(('-o %q'):format(output))
|
||||
end
|
||||
|
||||
-- INFO : Start a job
|
||||
|
@@ -2,11 +2,11 @@
|
||||
---@field install fun() Download database and tts dependencies
|
||||
return function()
|
||||
local Trans = require 'Trans'
|
||||
local fn = vim.fn
|
||||
-- INFO :Check ultimate.db exists
|
||||
local dir = Trans.conf.dir
|
||||
local path = dir .. 'ultimate.db'
|
||||
|
||||
local fn = vim.fn
|
||||
if fn.isdirectory(dir) == 0 then
|
||||
fn.mkdir(dir, 'p')
|
||||
end
|
||||
@@ -16,14 +16,10 @@ return function()
|
||||
return
|
||||
end
|
||||
|
||||
vim.notify('Trying to download database', vim.log.INFO)
|
||||
|
||||
-- INFO :Download ultimate.db
|
||||
local uri = 'https://github.com/skywind3000/ECDICT-ultimate/releases/download/1.0.0/ecdict-ultimate-sqlite.zip'
|
||||
local zip = dir .. 'ultimate.zip'
|
||||
|
||||
if fn.filereadable(zip) then os.remove(zip) end
|
||||
|
||||
local continue = fn.filereadable(zip) == 1
|
||||
local handle = function(output)
|
||||
if output.exit == 0 and fn.filereadable(zip) then
|
||||
if fn.executable 'unzip' == 0 then
|
||||
@@ -44,11 +40,16 @@ return function()
|
||||
vim.notify(debug_message, vim.log.ERROR)
|
||||
end
|
||||
|
||||
|
||||
Trans.curl.get(uri, {
|
||||
output = zip,
|
||||
callback = handle,
|
||||
output = zip,
|
||||
callback = handle,
|
||||
extra = continue and { '-C', '-' } or nil,
|
||||
})
|
||||
|
||||
local message = continue and 'Continue download database' or 'Begin to download database'
|
||||
vim.notify(message, vim.log.levels.INFO)
|
||||
|
||||
-- INFO : Install tts dependencies
|
||||
if fn.has 'linux' == 0 and fn.has 'mac' == 0 then
|
||||
os.execute 'cd ./tts && npm install'
|
||||
|
Reference in New Issue
Block a user