From 38b8e20729a128d11c80291fa7a07595d8a4b409 Mon Sep 17 00:00:00 2001 From: JuanZoran <1430359574@qq.com> Date: Fri, 7 Apr 2023 19:05:24 +0800 Subject: [PATCH] chore: close window and buffer more safely --- lua/Trans/core/buffer.lua | 2 +- lua/Trans/core/data.lua | 2 +- lua/Trans/core/translate.lua | 4 +--- lua/Trans/core/util.lua | 2 +- lua/Trans/core/window.lua | 3 ++- lua/Trans/health.lua | 6 +++--- plugin/Trans.lua | 18 ++++++++---------- 7 files changed, 17 insertions(+), 20 deletions(-) diff --git a/lua/Trans/core/buffer.lua b/lua/Trans/core/buffer.lua index 0294352..1ed62cb 100644 --- a/lua/Trans/core/buffer.lua +++ b/lua/Trans/core/buffer.lua @@ -37,7 +37,7 @@ end ---Destory buffer function buffer:destroy() - api.nvim_buf_delete(self.bufnr, { force = true }) + pcall(api.nvim_buf_delete, self.bufnr, { force = true }) end ---Set buffer load keymap diff --git a/lua/Trans/core/data.lua b/lua/Trans/core/data.lua index 35f4ea6..d911534 100644 --- a/lua/Trans/core/data.lua +++ b/lua/Trans/core/data.lua @@ -37,7 +37,7 @@ function M.new(opts) data.backends[i] = Trans.backend[name] end - if Trans.util.is_English(str) then + if Trans.util.is_english(str) then data.from = 'en' data.to = 'zh' else diff --git a/lua/Trans/core/translate.lua b/lua/Trans/core/translate.lua index bf64453..f9263ce 100644 --- a/lua/Trans/core/translate.lua +++ b/lua/Trans/core/translate.lua @@ -59,8 +59,7 @@ local strategy = { local name = backend.name ---@cast backend TransBackend - while result[name] == nil do - if not update(backend) then break end + while result[name] == nil and update(backend) do end if result[name] then return true end @@ -72,7 +71,6 @@ local strategy = { } - -- HACK : Core process logic local function process(opts) opts = init_opts(opts) diff --git a/lua/Trans/core/util.lua b/lua/Trans/core/util.lua index 33f605d..d1aefa6 100644 --- a/lua/Trans/core/util.lua +++ b/lua/Trans/core/util.lua @@ -72,7 +72,7 @@ end ---Detect whether the string is English ---@param str string ---@return boolean -function M.is_English(str) +function M.is_english(str) local char = { str:byte(1, -1) } for i = 1, #str do if char[i] > 128 then diff --git a/lua/Trans/core/window.lua b/lua/Trans/core/window.lua index 082171e..5fa2fdc 100644 --- a/lua/Trans/core/window.lua +++ b/lua/Trans/core/window.lua @@ -124,7 +124,8 @@ function window:try_close() } end - api.nvim_win_close(self.winid, true) + + pcall(api.nvim_win_close, self.winid, true) end ---Set window local highlight group diff --git a/lua/Trans/health.lua b/lua/Trans/health.lua index 4f3d6f2..2d07e0f 100644 --- a/lua/Trans/health.lua +++ b/lua/Trans/health.lua @@ -39,9 +39,9 @@ local function check_binary_dependencies() } binary_dependencies[3] = ({ - win = 'node', - mac = 'say', - linux = 'festival', + win = 'node', + mac = 'say', + linux = 'festival', termux = 'termux-api-speak', })[Trans.system] diff --git a/plugin/Trans.lua b/plugin/Trans.lua index a7f8a73..2148c38 100644 --- a/plugin/Trans.lua +++ b/plugin/Trans.lua @@ -4,19 +4,17 @@ local api, fn = vim.api, vim.fn local Trans = require 'Trans' local command = api.nvim_create_user_command -command('Translate', function() - Trans.translate() -end, { desc = ' Translate cursor word' }) +command('Translate', function() Trans.translate() end, + { desc = ' Translate cursor word' }) -command('TranslateInput', function() - Trans.translate { mode = 'i' } -end, { desc = ' Translate input word' }) +command('TranslateInput', function() Trans.translate { mode = 'i' } end, + { desc = ' Translate input word' }) command('TransPlay', function() local util = Trans.util local str = util.get_str(vim.fn.mode()) - if str and str ~= '' and util.is_English(str) then + if str and str ~= '' and util.is_english(str) then str:play() end end, { desc = ' Auto play' }) @@ -30,9 +28,9 @@ local f = system == 'termux' and 'termux-tts-speak %q' or system == 'linux' and 'echo %q | festival --tts' or 'node' .. Trans.relative_path { 'tts', 'say.js' } .. ' %q' - -- 'powershell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak([Console]::In.ReadToEnd())" | Out-File -Encoding ASCII %q' - -- or 'node' .. Trans.relative_path { 'tts', 'say.js' } .. ' %q' - -- system == 'win' and 'powershell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak([Console]::In.ReadToEnd())" | Out-File -Encoding ASCII %q' +-- 'powershell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak([Console]::In.ReadToEnd())" | Out-File -Encoding ASCII %q' +-- or 'node' .. Trans.relative_path { 'tts', 'say.js' } .. ' %q' +-- system == 'win' and 'powershell -Command "Add-Type –AssemblyName System.Speech; (New-Object System.Speech.Synthesis.SpeechSynthesizer).Speak([Console]::In.ReadToEnd())" | Out-File -Encoding ASCII %q' string.play = function(self) fn.jobstart(f:format(self))