From 978677696e6831f833f08bd49aabbef8cbcce59b Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Thu, 16 Mar 2023 00:03:13 +0800 Subject: [PATCH] feat: add youdao backend file --- lua/Trans/README.md | 1 + lua/Trans/backend/baidu.lua | 8 +--- lua/Trans/backend/youdao.lua | 78 +++++++++++++++++++++++++++++++ lua/Trans/core/translate.lua | 10 ++-- lua/Trans/core/util.lua | 17 +++++++ lua/Trans/core/window.lua | 11 ----- lua/Trans/frontend/hover/init.lua | 55 +++++++++++++--------- lua/Trans/style/theme.lua | 9 ++++ 8 files changed, 146 insertions(+), 43 deletions(-) diff --git a/lua/Trans/README.md b/lua/Trans/README.md index 272100c..d39a800 100644 --- a/lua/Trans/README.md +++ b/lua/Trans/README.md @@ -11,3 +11,4 @@ - [x] waitting animation - [x] init frontend window - [ ] build frontend window format logic +- [ ] Add Query FallBack diff --git a/lua/Trans/backend/baidu.lua b/lua/Trans/backend/baidu.lua index bc8782b..55cc70d 100644 --- a/lua/Trans/backend/baidu.lua +++ b/lua/Trans/backend/baidu.lua @@ -4,6 +4,7 @@ ---@field app_id string ---@field app_passwd string ---@field disable boolean + local M = { uri = 'https://fanyi-api.baidu.com/api/trans/vip/translate', salt = tostring(math.random(bit.lshift(1, 15))), @@ -12,7 +13,6 @@ local M = { local Trans = require('Trans') - ---@class BaiduQuery ---@field q string ---@field from string @@ -50,12 +50,6 @@ end ---Query Using Baidu API ---@param data TransData function M.query(data) - if M.disable then - data.result.baidu = false - return - end - - local handle = function(res) local status, body = pcall(vim.json.decode, res.body) if not status or not body then diff --git a/lua/Trans/backend/youdao.lua b/lua/Trans/backend/youdao.lua index 8141436..97d5160 100644 --- a/lua/Trans/backend/youdao.lua +++ b/lua/Trans/backend/youdao.lua @@ -4,8 +4,86 @@ ---@field app_id string ---@field app_passwd string ---@field disable boolean +local M = { + uri = 'https://openapi.youdao.com/api', + salt = tostring(math.random(bit.lshift(1, 15))), + name = 'youdao', +} + +---@class YoudaoQuery +---@field q string +---@field from string +---@field to string +---@field appid string +---@field salt string +---@field sign string + +---Get content for query +---@param data TransData +---@return YoudaoQuery +function M.get_content(data) + local str = data.str + local app_id = M.app_id + local salt = M.salt + local curtime = tostring(os.time()) + local input = #str > 20 and + str:sub(1, 10) .. #str .. str:sub(-10) or str + + -- sign=sha256(应用ID+input+salt+curtime+应用密钥); + local hash = app_id .. input .. salt .. curtime .. M.app_passwd + local sign = vim.fn.sha256(hash) + return { + q = str, + to = data.from == 'zh' and 'en' or 'zh-CHS', + from = 'auto', + signType = 'v3', + appKey = app_id, + salt = M.salt, + curtime = curtime, + sign = sign, + } +end + +---@overload fun(TransData): TransResult +---Query Using Baidu API +---@param data TransData +function M.query(data) + local handle = function(res) + local status, body = pcall(vim.json.decode, res.body) + if not status or not body then + data.result.youdao = false + data.trace = res + return + end + + if true then + vim.print(body) + return + end + + local result = body.trans_result + if result then + -- TEST :whether multi result + assert(#result == 1) + result = result[1] + data.result.youdao = { + ['title'] = result.src, + [data.from == 'en' and 'translation' or 'definition'] = { result.dst }, + } + end + end + + require('Trans').curl.get(M.uri, { + query = M.get_content(data), + callback = handle, + }) +end + +---@class TransBackend +---@field youdao Youdao +return M -- local GET = require("Trans.util.curl").GET -- return function(word) -- local isEn = word:isEn() diff --git a/lua/Trans/core/translate.lua b/lua/Trans/core/translate.lua index 980c77d..b311120 100644 --- a/lua/Trans/core/translate.lua +++ b/lua/Trans/core/translate.lua @@ -24,14 +24,18 @@ local strategy = { backend.query(data) while result[name] == nil do - update() + if not update() then + break + end end if type(result[name]) == 'table' then return result[name] end end - end + end, + + --- TODO :More Strategys } @@ -58,7 +62,7 @@ local function process(opts) if not result then result = strategy[Trans.conf.query](data, data.frontend:wait()) if not result then - -- data.frontend:fallback() + data.frontend:fallback() return end end diff --git a/lua/Trans/core/util.lua b/lua/Trans/core/util.lua index f412262..6b297b3 100644 --- a/lua/Trans/core/util.lua +++ b/lua/Trans/core/util.lua @@ -115,6 +115,23 @@ function M.display_size(lines, width) return { height = ds_height, width = ds_width } end +---Center node utility function +---@param node string -- TODO :Node +---@param win_width integer window width +---@return string +function M.center(node, win_width) + if type(node) == 'string' then + local space = math.max(0, math.floor((win_width - node:width()) / 2)) + return string.rep(' ', space) .. node + end + + local str = node[1] + win_width = str:width() + local space = math.max(0, math.floor((win_width - str:width()) / 2)) + node[1] = string.rep(' ', space) .. str + return node +end + ---Execute function in main loop ---@param func function function to be executed function M.main_loop(func) diff --git a/lua/Trans/core/window.lua b/lua/Trans/core/window.lua index f51c637..d885dc4 100644 --- a/lua/Trans/core/window.lua +++ b/lua/Trans/core/window.lua @@ -157,17 +157,6 @@ function window:open() end end --- function window:center(node) --- -- TODO : --- print('TODO Center') --- -- local text = node[1] --- -- local width = text:width() --- -- local win_width = self.width --- -- local space = math.max(math.floor((win_width - width) / 2), 0) --- -- node[1] = (' '):rep(space) .. text --- -- return node --- end - window.__index = window diff --git a/lua/Trans/frontend/hover/init.lua b/lua/Trans/frontend/hover/init.lua index beae0a3..5de6b81 100644 --- a/lua/Trans/frontend/hover/init.lua +++ b/lua/Trans/frontend/hover/init.lua @@ -114,18 +114,42 @@ function M:wait() return function() cur = cur + 1 buffer[1] = spinner[cur % size + 1] .. (cell):rep(cur) - buffer:add_highlight(1, 'MoreMsg') + buffer:add_highlight(1, 'TransWaitting') pause(interval) return cur == times end end --- -- FIXME : --- -- vim.api.nvim_buf_set_lines(buffer.bufnr, 1, -1, true, {}) --- -- print('jklajsdk') --- -- print(vim.fn.deletebufline(buffer.bufnr, 1)) --- -- buffer:del() --- buffer[1] = '' +---FallBack window for no result +function M:fallback() + local buffer = self.buffer + local opts = self.opts + buffer:wipe() + local fallback_msg = opts.fallback_message:gsub('{{(%w+)}}', opts.icon) + + -- TODO :Center + buffer[1] = Trans.util.center(fallback_msg, opts.width) + buffer:add_highlight(1, 'TransFailed') + self:defer() +end + +---Defer function when process done +function M:defer() + self.window:set('wrap', true) + self.buffer:set('modifiable', false) + + + local auto_close_events = self.opts.auto_close_events + if auto_close_events then + vim.api.nvim_create_autocmd(auto_close_events, { + once = true, + callback = function() + if self.pin then return end + self:destroy() + end, + }) + end +end ---Display Result in hover window ---@param data TransData @@ -156,6 +180,7 @@ function M:process(data, result) local window = self.window local display_size = Trans.util.display_size(buffer:lines(), opts.width) + if window and window:is_valid() then if opts.auto_resize then display_size.width = math.min(opts.width, display_size.width + opts.padding) @@ -169,21 +194,7 @@ function M:process(data, result) width = math.min(opts.width, display_size.width + opts.padding), } end - - window:set('wrap', true) - buffer:set('modifiable', false) - - - local auto_close_events = opts.auto_close_events - if auto_close_events then - vim.api.nvim_create_autocmd(auto_close_events, { - once = true, - callback = function() - if self.pin then return end - self:destroy() - end, - }) - end + self:defer() end ---Check if hover window and buffer are valid diff --git a/lua/Trans/style/theme.lua b/lua/Trans/style/theme.lua index 737936f..2b8bfdd 100644 --- a/lua/Trans/style/theme.lua +++ b/lua/Trans/style/theme.lua @@ -44,6 +44,9 @@ return { TransFailed = { fg = '#7aa89f', }, + TransWaitting = { + link = 'MoreMsg' + }, }, --- TODO : @@ -91,6 +94,9 @@ return { TransFailed = { fg = '#f4b085', }, + TransWaitting = { + link = 'MoreMsg' + }, }, dracula = { TransWord = { @@ -136,5 +142,8 @@ return { TransFailed = { fg = '#8be9fd', }, + TransWaitting = { + link = 'MoreMsg' + }, }, }