diff --git a/.github/workflows/panvimdoc.yml b/.github/workflows/panvimdoc.yml new file mode 100644 index 0000000..f5ea271 --- /dev/null +++ b/.github/workflows/panvimdoc.yml @@ -0,0 +1,18 @@ +name: panvimdoc + +on: [push] + +jobs: + docs: + runs-on: ubuntu-latest + name: pandoc to vimdoc + steps: + - uses: actions/checkout@v2 + - name: panvimdoc + uses: kdheepak/panvimdoc@main + with: + vimdoc: Trans + - uses: stefanzweifel/git-auto-commit-action@v4 + with: + commit_message: "Auto generate docs" + branch: dev diff --git a/doc/Trans.txt b/doc/Trans.txt new file mode 100644 index 0000000..e69de29 diff --git a/lua/Trans/backend/baidu.lua b/lua/Trans/backend/baidu.lua index d6d8b9f..b521b23 100644 --- a/lua/Trans/backend/baidu.lua +++ b/lua/Trans/backend/baidu.lua @@ -1,63 +1,74 @@ -local baidu = require('Trans').conf.engine.baidu -local appid = baidu.appid -local appPasswd = baidu.appPasswd -local salt = tostring(math.random(bit.lshift(1, 15))) -local uri = 'https://fanyi-api.baidu.com/api/trans/vip/translate' +local M = {} --- error('请查看README, 实现在线翻译或者设置将在线翻译设置为false') +local baidu = require('Trans').conf.engine.baidu +local appid = baidu.appid +local app_passwd = baidu.app_passwd +local salt = tostring(math.random(bit.lshift(1, 15))) +local uri = 'https://fanyi-api.baidu.com/api/trans/vip/translate' -local post = require('Trans.util.curl').POST -local function get_field(word, isEn) - local to = isEn and 'zh' or 'en' - local tmp = appid .. word .. salt .. appPasswd +M.request_headers = function(data) + local tmp = appid .. data.str .. salt .. app_passwd local sign = require('Trans.util.md5').sumhexa(tmp) - return { - q = word, - from = 'auto', - to = to, + q = data.str, + from = data.from, + to = data.to, appid = appid, salt = salt, sign = sign, } end ----返回一个channel ----@param word string ----@return table -return function(word) - local isEn = word:isEn() - local query = get_field(word, isEn) - local result = {} - post(uri, { - data = query, - headers = { - content_type = "application/x-www-form-urlencoded", - }, - callback = function(str) - local ok, res = pcall(vim.json.decode, str) - if ok and res and res.trans_result then - result[1] = { - title = { word = word }, - [isEn and 'translation' or 'definition'] = res.trans_result[1].dst, - } +M.query = function(data) + data.engine = 'baidu' - if result.callback then - result.callback(result[1]) - end - else - result[1] = false - end - end, - }) + require('Trans.wrapper.curl').POST { - return result + } end +return M --- NOTE :free tts: --- https://zj.v.api.aa1.cn/api/baidu-01/?msg=我爱你&choose=0&su=100&yd=5 --- 选择转音频的人物,女生1 输入0 | 女生2输入:5|男生1 输入:1|男生2 输入:2|男生3 输入:3 +-- local post = require('Trans.util.curl').POST + +-- ---返回一个channel +-- ---@param word string +-- ---@return table +-- return function(word) +-- local isEn = word:isEn() +-- local query = get_field(word, isEn) +-- local result = {} + +-- post(uri, { +-- data = query, +-- headers = { +-- content_type = "application/x-www-form-urlencoded", +-- }, +-- callback = function(str) +-- local ok, res = pcall(vim.json.decode, str) +-- if ok and res and res.trans_result then +-- result[1] = { +-- title = { word = word }, +-- [isEn and 'translation' or 'definition'] = res.trans_result[1].dst, +-- } + +-- if result.callback then +-- result.callback(result[1]) +-- end +-- else +-- result[1] = false +-- end +-- end, +-- }) + +-- return result +-- end + + + +-- -- NOTE :free tts: +-- -- https://zj.v.api.aa1.cn/api/baidu-01/?msg=我爱你&choose=0&su=100&yd=5 +-- -- 选择转音频的人物,女生1 输入0 | 女生2输入:5|男生1 输入:1|男生2 输入:2|男生3 输入:3 diff --git a/lua/Trans/backend/init.lua b/lua/Trans/backend/init.lua index ece2093..038b634 100644 --- a/lua/Trans/backend/init.lua +++ b/lua/Trans/backend/init.lua @@ -7,4 +7,7 @@ M.__index = function(t, k) return engine end + + + return setmetatable(M, M) diff --git a/lua/Trans/backend/offline.lua b/lua/Trans/backend/offline.lua index 9343dc7..14fdf24 100644 --- a/lua/Trans/backend/offline.lua +++ b/lua/Trans/backend/offline.lua @@ -1,7 +1,6 @@ -local M = {} +local M = { no_wait = true } local db = require 'sqlite.db' -local util = require("Trans.backend.util") vim.api.nvim_create_autocmd('VimLeavePre', { once = true, callback = function() @@ -11,45 +10,32 @@ vim.api.nvim_create_autocmd('VimLeavePre', { end }) -M.query = function(opts) - opts = type(opts) == 'string' and { str = opts } or opts - if opts.is_word == false then return end +M.query = function(data) + if data.is_word == false or data.from == 'zh' then return end - opts.engine = 'offline' - - opts.field = opts.field or M.field - opts.path = vim.fn.expand(opts.path or require('Trans').conf.db_path) - opts.formatter = opts.formatter or M.formatter + data.path = vim.fn.expand(data.path or require('Trans').conf.db_path) + data.engine = 'offline' + data.formatter = data.formatter or M.formatter + data.query_field = data.query_field or M.query_field - local dict = db:open(opts.path) - local db_name = opts.db_name or 'stardict' + local dict = db:open(data.path) + local db_name = data.db_name or 'stardict' local res = dict:select(db_name, { - where = { word = opts.str, }, - keys = opts.field, + where = { word = data.str, }, + keys = data.query_field, limit = 1, })[1] - - if util.is_English(opts.str) then - opts.from = 'en' - opts.to = 'zh' - else - opts.from = 'zh' - opts.to = 'en' - end - if res then - opts.result = opts.formatter(res) + data.result = data.formatter(res) end - return opts + return data end -M.nowait = true - -M.field = { +M.query_field = { 'word', 'phonetic', 'definition', @@ -61,10 +47,13 @@ M.field = { 'exchange', } +local exist = function(str) + return str and str ~= '' +end local formatter = { title = function(res) - res.title = { + local title = { word = res.word, oxford = res.oxford, collins = res.collins, @@ -75,8 +64,10 @@ local formatter = { res.oxford = nil res.collins = nil res.phonetic = nil + return title end, tag = function(res) + if not exist(res.tag) then return end local tag_map = { zk = '中考', gk = '高考', @@ -96,6 +87,7 @@ local formatter = { return tag end, exchange = function(res) + if not exist(res.exchange) then return end local exchange_map = { ['p'] = '过去式 ', ['d'] = '过去分词 ', @@ -110,13 +102,14 @@ local formatter = { } local exchange = {} - for i, _exchange in ipairs(vim.split(res.exchange, ' ', { plain = true })) do - exchange[i] = exchange_map[_exchange] + for _, _exchange in ipairs(vim.split(res.exchange, '/', { plain = true })) do + exchange[exchange_map[_exchange:sub(1, 1)]] = _exchange:sub(3) end return exchange end, pos = function(res) + if not exist(res.pos) then return end local pos_map = { a = '代词pron ', c = '连接词conj ', @@ -134,13 +127,14 @@ local formatter = { } local pos = {} - for i, _pos in ipairs(vim.split(res.pos, '/', { plain = true })) do - pos[i] = pos_map[_pos] + for _, _pos in ipairs(vim.split(res.pos, '/', { plain = true })) do + pos[pos_map[_pos:sub(1, 1)]] = _pos:sub(3) end return pos end, translation = function(res) + if not exist(res.translation) then return end local translation = {} for i, _translation in ipairs(vim.split(res.translation, '\n', { plain = true })) do translation[i] = _translation @@ -149,6 +143,7 @@ local formatter = { return translation end, definition = function(res) + if not exist(res.definition) then return end local definition = {} for i, _definition in ipairs(vim.split(res.definition, '\n', { plain = true })) do -- -- TODO :判断是否需要分割空格 diff --git a/lua/Trans/backend/util.lua b/lua/Trans/backend/util.lua deleted file mode 100644 index ef03e2f..0000000 --- a/lua/Trans/backend/util.lua +++ /dev/null @@ -1,14 +0,0 @@ -local M = {} - -M.is_English = function(str) - local char = { str:byte(1, -1) } - for i = 1, #str do - if char[i] > 128 then - return false - end - end - return true -end - - -return M diff --git a/lua/Trans/core/conf.lua b/lua/Trans/core/conf.lua new file mode 100644 index 0000000..819401e --- /dev/null +++ b/lua/Trans/core/conf.lua @@ -0,0 +1,105 @@ +return { + view = { + i = 'float', + n = 'hover', + v = 'hover', + }, + hover = { + width = 37, + height = 27, + border = 'rounded', + title = vim.fn.has('nvim-0.9') == 1 and { + { '', 'TransTitleRound' }, + { ' Trans', 'TransTitle' }, + { '', 'TransTitleRound' }, + } or nil, + keymap = { + pageup = '[[', + pagedown = ']]', + pin = '[', + close = ']', + toggle_entry = ';', + play = '_', + }, + animation = { + -- open = 'fold', + -- close = 'fold', + open = 'slid', + close = 'slid', + interval = 12, + }, + auto_close_events = { + 'InsertEnter', + 'CursorMoved', + 'BufLeave', + }, + auto_play = true, + timeout = 2000, + spinner = 'dots', -- 查看所有样式: /lua/Trans/util/spinner + -- spinner = 'moon' + }, + order = { -- only work on hover mode + 'title', + 'tag', + 'pos', + 'exchange', + 'translation', + 'definition', + }, + icon = { + star = '', + notfound = ' ', + yes = '✔', + no = '', + -- --- char: ■ | □ | ▇ | ▏ ▎ ▍ ▌ ▋ ▊ ▉ █ + -- --- ◖■■■■■■■◗▫◻ ▆ ▆ ▇⃞ ▉⃞ + cell = '■', + -- star = '⭐', + -- notfound = '❔', + -- yes = '✔️', + -- no = '❌' + }, + theme = 'default', + db_path = '$HOME/.vim/dict/ultimate.db', + -- float = { + -- width = 0.8, + -- height = 0.8, + -- border = 'rounded', + -- keymap = { + -- quit = 'q', + -- }, + -- animation = { + -- open = 'fold', + -- close = 'fold', + -- interval = 10, + -- }, + -- tag = { + -- wait = '#519aba', + -- fail = '#e46876', + -- success = '#10b981', + -- }, + -- }, +} + + +-- ---Pasue Handler for {ms} milliseconds +-- ---@param ms number @milliseconds +-- M.pause = function(ms) +-- local co = coroutine.running() +-- vim.defer_fn(function() +-- coroutine.resume(co) +-- end, ms) +-- coroutine.yield() +-- end + + +-- local title = { +-- "████████╗██████╗ █████╗ ███╗ ██╗███████╗", +-- "╚══██╔══╝██╔══██╗██╔══██╗████╗ ██║██╔════╝", +-- " ██║ ██████╔╝███████║██╔██╗ ██║███████╗", +-- " ██║ ██╔══██╗██╔══██║██║╚██╗██║╚════██║", +-- " ██║ ██║ ██║██║ ██║██║ ╚████║███████║", +-- " ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝", +--} + +-- string.width = api.nvim_strwidth diff --git a/lua/Trans/core/setup.lua b/lua/Trans/core/setup.lua new file mode 100644 index 0000000..5af4802 --- /dev/null +++ b/lua/Trans/core/setup.lua @@ -0,0 +1,24 @@ +return function(opts) + local M = require('Trans') + 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] + 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 diff --git a/lua/Trans/core/translate.lua b/lua/Trans/core/translate.lua new file mode 100644 index 0000000..625b278 --- /dev/null +++ b/lua/Trans/core/translate.lua @@ -0,0 +1,33 @@ +local M = require('Trans') +local util = M.util + + +local process +process = function(opts) + opts = opts or {} + local mode = opts.mode or vim.api.nvim_get_mode().mode + local str = util.get_str(mode) + if str == '' then return end + + local data = { + str = str, + view = opts.view or M.conf.view[mode], + mode = mode, + } + + if util.is_English(str) then + data.from = 'en' + data.to = 'zh' + else + data.from = 'zh' + data.to = 'en' + end + + + local res = require('Trans.backend').offline.query(data) + -- vim.pretty_print(res) + + M.translate = coroutine.wrap(process) +end + +return process diff --git a/lua/Trans/core/util.lua b/lua/Trans/core/util.lua new file mode 100644 index 0000000..44c24d0 --- /dev/null +++ b/lua/Trans/core/util.lua @@ -0,0 +1,63 @@ +local M = {} + +local fn, api = vim.fn, vim.api + +M.get_select = function() + local _start = fn.getpos("v") + local _end = fn.getpos('.') + + if _start[2] > _end[2] or (_start[3] > _end[3] and _start[2] == _end[2]) then + _start, _end = _end, _start + end + local s_row, s_col = _start[2], _start[3] + local e_row, e_col = _end[2], _end[3] + + -- print(s_row, e_row, s_col, e_col) + ---@type string + ---@diagnostic disable-next-line: assign-type-mismatch + local line = fn.getline(e_row) + local uidx = vim.str_utfindex(line, math.min(#line, e_col)) + ---@diagnostic disable-next-line: param-type-mismatch + e_col = vim.str_byteindex(line, uidx) + + + if s_row == e_row then + return line:sub(s_col, e_col) + else + local lines = fn.getline(s_row, e_row) + local e = #lines + lines[1] = lines[1]:sub(s_col) + lines[e] = line:sub(1, e_col) + return table.concat(lines) + end +end + +---Get Text which need to be translated +---@param mode string 'n' | 'v' | 'i' +---@return string +M.get_str = function(mode) + if mode == 'n' then + return fn.expand('') + elseif mode == 'v' then + api.nvim_input('') + return M.get_select() + elseif mode == 'i' then + -- TODO Use Telescope with fuzzy finder + ---@diagnostic disable-next-line: param-type-mismatch + return fn.input('请输入需要查询的单词:') + else + error('invalid mode: ' .. mode) + end +end + +M.is_English = function(str) + local char = { str:byte(1, -1) } + for i = 1, #str do + if char[i] > 128 then + return false + end + end + return true +end + +return M diff --git a/lua/Trans/health.lua b/lua/Trans/health.lua index 90b569d..eac5f3d 100644 --- a/lua/Trans/health.lua +++ b/lua/Trans/health.lua @@ -13,11 +13,11 @@ M.check = function() -- INFO :Check neovim version if has('nvim-0.9') == 1 then ok [[ - [PASS]: you have Trans.nvim with full features in neovim-nightly + you have Trans.nvim with full features in neovim-nightly ]] else warn [[ - [WARN]: Trans Title requires Neovim 0.9 or newer + Trans Title requires Neovim 0.9 or newer See neovim-nightly: https://github.com/neovim/neovim/releases/tag/nightly ]] end @@ -26,22 +26,22 @@ M.check = function() local has_sqlite = pcall(require, 'sqlite') if has_sqlite then ok [[ - [PASS]: Dependency sqlite.lua is installed + Dependency sqlite.lua is installed ]] else error [[ - [ERROR]: Dependency sqlite.lua can't work correctly + Dependency sqlite.lua can't work correctly Please Read the doc in github carefully ]] end if executable('sqlite3') then ok [[ - [PASS]: Dependency sqlite3 found + Dependency sqlite3 found ]] else error [[ - [ERROR]: Dependency sqlite3 not found + Dependency sqlite3 not found ]] end @@ -50,12 +50,28 @@ M.check = function() local db_path = vim.fn.expand(require('Trans').conf.db_path) if vim.fn.filereadable(db_path) == 1 then ok [[ - [PASS]: Stardict database found + Stardict database found ]] else error [[ - [PASS]: Stardict database not found - Please check the doc in github + Stardict database not found + Please check the doc in github: https://github.com/JuanZoran/Trans.nvim + ]] + end + + + -- INFO :Check Engine configuration file + local path = vim.fn.expand("$HOME/.vim/dict/Trans.json") + local file = io.open(path, "r") + local valid = vim.json.decode(file:read("*a")) + if valid then + ok [[ + Engine configuration file found and valid + ]] + else + error [[ + Engine configuration file not found or invalid + Please check the doc in github: https://github.com/JuanZoran/Trans.nvim ]] end end diff --git a/lua/Trans/init.lua b/lua/Trans/init.lua index bce7ff5..620d571 100644 --- a/lua/Trans/init.lua +++ b/lua/Trans/init.lua @@ -1,208 +1,11 @@ -local M = {} -local api, fn = vim.api, vim.fn - -local win_title = fn.has('nvim-0.9') == 1 and { - { '', 'TransTitleRound' }, - { ' Trans', 'TransTitle' }, - { '', 'TransTitleRound' }, - } or nil - -M.conf = { - view = { - i = 'float', - n = 'hover', - v = 'hover', - }, - hover = { - width = 37, - height = 27, - border = 'rounded', - title = win_title, - keymap = { - pageup = '[[', - pagedown = ']]', - pin = '[', - close = ']', - toggle_entry = ';', - play = '_', - }, - animation = { - -- open = 'fold', - -- close = 'fold', - open = 'slid', - close = 'slid', - interval = 12, - }, - auto_close_events = { - 'InsertEnter', - 'CursorMoved', - 'BufLeave', - }, - auto_play = true, - timeout = 2000, - spinner = 'dots', -- 查看所有样式: /lua/Trans/util/spinner - -- spinner = 'moon' - }, - float = { - width = 0.8, - height = 0.8, - border = 'rounded', - title = win_title, - keymap = { - quit = 'q', - }, - animation = { - open = 'fold', - close = 'fold', - interval = 10, - }, - tag = { - wait = '#519aba', - fail = '#e46876', - success = '#10b981', - }, - }, - order = { -- only work on hover mode - 'title', - 'tag', - 'pos', - 'exchange', - 'translation', - 'definition', - }, - icon = { - star = '', - notfound = ' ', - yes = '✔', - no = '', - -- --- char: ■ | □ | ▇ | ▏ ▎ ▍ ▌ ▋ ▊ ▉ █ - -- --- ◖■■■■■■■◗▫◻ ▆ ▆ ▇⃞ ▉⃞ - cell = '■', - -- star = '⭐', - -- notfound = '❔', - -- yes = '✔️', - -- no = '❌' - }, - theme = 'default', - db_path = '$HOME/.vim/dict/ultimate.db', -} - -M.setup = function(opts) - if opts then - M.conf = vim.tbl_deep_extend('force', M.conf, opts) +local M = setmetatable({}, { + __index = function(tbl, key) + local status, field = pcall(require, 'Trans.core.' .. key) + assert(status, 'Unknown field: ' .. key) + + tbl[key] = field + return field end - local conf = M.conf - - local set_hl = api.nvim_set_hl - local hls = require('Trans.style.theme')[conf.theme] - for hl, opt in pairs(hls) do - set_hl(0, hl, opt) - end -end - -local function get_select() - local _start = fn.getpos("v") - local _end = fn.getpos('.') - - if _start[2] > _end[2] or (_start[3] > _end[3] and _start[2] == _end[2]) then - _start, _end = _end, _start - end - local s_row, s_col = _start[2], _start[3] - local e_row, e_col = _end[2], _end[3] - - -- print(s_row, e_row, s_col, e_col) - ---@type string - ---@diagnostic disable-next-line: assign-type-mismatch - local line = fn.getline(e_row) - local uidx = vim.str_utfindex(line, math.min(#line, e_col)) - e_col = vim.str_byteindex(line, uidx) - - - if s_row == e_row then - return line:sub(s_col, e_col) - else - local lines = fn.getline(s_row, e_row) - local e = #lines - lines[1] = lines[1]:sub(s_col) - lines[e] = line:sub(1, e_col) - return table.concat(lines) - end -end - -M.get_str = function(mode) - local word - if mode == 'n' then - word = fn.expand('') - elseif mode == 'v' then - api.nvim_input('') - word = get_select() - elseif mode == 'i' then - -- TODO Use Telescope with fuzzy finder - ---@diagnostic disable-next-line: param-type-mismatch - word = fn.input('请输入需要查询的单词:') - else - error('invalid mode: ' .. mode) - end - return word -end - - -local process -process = function(opts) - opts = opts or {} - local mode = opts.mode or vim.api.nvim_get_mode().mode - local str = M.get_str(mode) - if str == '' then return end - - local view = opts.view or M.conf.view[mode] - local res = require('Trans.backend').offline.query(str) - vim.pretty_print(res) - - M.translate = coroutine.wrap(process) -end - -M.translate = coroutine.wrap(process) - - ----Pasue Handler for {ms} milliseconds ----@param ms number @milliseconds -M.pause = function(ms) - local co = coroutine.running() - vim.defer_fn(function() - coroutine.resume(co) - end, ms) - coroutine.yield() -end - - --- local title = { --- "████████╗██████╗ █████╗ ███╗ ██╗███████╗", --- "╚══██╔══╝██╔══██╗██╔══██╗████╗ ██║██╔════╝", --- " ██║ ██████╔╝███████║██╔██╗ ██║███████╗", --- " ██║ ██╔══██╗██╔══██║██║╚██╗██║╚════██║", --- " ██║ ██║ ██║██║ ██║██║ ╚████║███████║", --- " ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝", ---} - --- string.width = api.nvim_strwidth --- string.isEn = function(self) --- local char = { self:byte(1, -1) } --- for i = 1, #self do --- if char[i] > 128 then --- return false --- end --- end --- return true --- end - --- string.play = fn.has('linux') == 1 and function(self) --- local cmd = ([[echo "%s" | festival --tts]]):format(self) --- fn.jobstart(cmd) --- end or function(self) --- local seperator = fn.has('unix') and '/' or '\\' --- local file = debug.getinfo(1, "S").source:sub(2):match('(.*)lua') .. seperator .. 'tts' .. seperator .. 'say.js' --- fn.jobstart('node ' .. file .. ' ' .. self) --- end -M.ns = api.nvim_create_namespace('Trans') +}) return M diff --git a/lua/Trans/bak_init.lua b/lua/Trans/util/bak_init.lua similarity index 100% rename from lua/Trans/bak_init.lua rename to lua/Trans/util/bak_init.lua diff --git a/lua/Trans/util/curl.lua b/lua/Trans/util/curl.lua index 0b54edf..b0309f7 100644 --- a/lua/Trans/util/curl.lua +++ b/lua/Trans/util/curl.lua @@ -1,44 +1,65 @@ --- TODO :wrapper for curl local curl = {} --- local example = { --- data = {}, --- headers = { --- k = 'v', --- }, --- callback = function(output) --- end, --- } - -curl.GET = function(uri, opts) - --- TODO : - vim.validate { - uri = { uri, 's' }, - opts = { opts, 't' } - } - local cmd = {'curl', '-s', ('"%s"'):format(uri)} +---Send a GET request +---@param opts table +curl.GET = function(opts) + local uri = opts.uri + local headers = opts.headers local callback = opts.callback - local output = '' - local option = { + -- 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 + cmd[#cmd + 1] = ([[-H '%s: %s']]):format(k, v) + end + + -- INFO :Add arguments + local info = {} + for k, v in pairs(opts.arguments) do + info[#info + 1] = ('%s=%s'):format(k, v) + end + cmd[#cmd + 1] = ([['%s?%s']]):format(uri, table.concat(info, '&')) + + + -- write a function to get the output + local outpus = {} + vim.fn.jobstart(table.concat(cmd, ' '), { stdin = 'null', on_stdout = function(_, stdout) local str = table.concat(stdout) if str ~= '' then - output = output .. str + end end, on_exit = function() callback(output) end, - } + }) - vim.fn.jobstart(table.concat(cmd, ' '), option) + -- local output = '' + -- local option = { + -- stdin = 'null', + -- on_stdout = function(_, stdout) + -- local str = table.concat(stdout) + -- if str ~= '' then + -- output = output .. str + -- end + -- end, + -- on_exit = function() + -- callback(output) + -- end, + -- } + + -- vim.fn.jobstart(table.concat(cmd, ' '), option) end -curl.POST = function(uri, opts) +curl.POST = function(opts) vim.validate { uri = { uri, 's' }, opts = { opts, 't' } diff --git a/plugin/Trans.lua b/plugin/Trans.lua index 5f6540a..ed568e3 100644 --- a/plugin/Trans.lua +++ b/plugin/Trans.lua @@ -1,13 +1,32 @@ -local api = vim.api +local api, fn = vim.api, vim.fn + +if fn.has('linux') == 1 then + string.play = function(self) + local cmd = ([[echo "%s" | festival --tts]]):format(self) + fn.jobstart(cmd) + end +elseif fn.has('mac') == 1 then + string.play = function(self) + local cmd = ([[say "%s"]]):format(self) + fn.jobstart(cmd) + end +else + string.play = function(self) + local seperator = fn.has('unix') and '/' or '\\' + local file = debug.getinfo(1, "S").source:sub(2):match('(.*)lua') .. seperator .. 'tts' .. seperator .. 'say.js' + fn.jobstart('node ' .. file .. ' ' .. self) + end +end local M = require('Trans') -local new_command = api.nvim_create_user_command -new_command('Translate', function() M.translate() end, { desc = ' 单词翻译', }) +local command = api.nvim_create_user_command + +command('Translate', function() M.translate() end, { desc = ' 单词翻译', }) +command('TransPlay', function() + local str = M.util.get_str(api.nvim_get_mode().mode) + if str and str ~= '' and M.util.is_English(str) then + str:play() + end +end, { desc = ' 自动发音' }) -- new_command('TranslateInput', function() M.translate { mode = 'i' } end, { desc = ' 搜索翻译', }) --- new_command('TransPlay', function() --- local word = M.get_word(api.nvim_get_mode().mode) --- if word ~= '' and word:isEn() then --- word:play() --- end --- end, { desc = ' 自动发音' })