From 526ca19fbf96ea062623bd0b2229a9241df4072e Mon Sep 17 00:00:00 2001
From: JuanZoran <1430359574@qq.com>
Date: Sat, 21 Jan 2023 08:26:07 +0800
Subject: [PATCH 1/4] docs: change TODO style

---
 README.md | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)

diff --git a/README.md b/README.md
index ccbb244..fbe1ba2 100644
--- a/README.md
+++ b/README.md
@@ -264,11 +264,10 @@ vim.keymap.set('n', 'mi', '<Cmd>TranslateInput<CR>')
 - [T.vim](https://github.com/sicong-li/T.vim)           灵感来源
 
 ## 待办 (画大饼)
-- ~~移动光标自动关闭窗口~~
-- 多风格样式
-- 历史查询结果保存
-- 在线多引擎异步查询
-- 快捷键定义
-- 自动读音
-- `句子翻译` | `中翻英` 的支持
-- 重新录制屏幕截图示例
+- [x] 多风格样式查询
+- [x] 重新录制屏幕截图示例 
+- [ ] 历史查询结果保存
+- [ ] 在线多引擎异步查询 
+- [ ] 快捷键定义 
+- [ ] 自动读音 
+- [ ] `句子翻译` | `中翻英` 的支持 

From e058985bf34774cc790bc92a6b3141e50d93c39c Mon Sep 17 00:00:00 2001
From: JuanZoran <1430359574@qq.com>
Date: Sat, 21 Jan 2023 14:02:38 +0800
Subject: [PATCH 2/4] fix: fix wrap height caculate mistake

---
 lua/Trans/content.lua    |  6 ++++--
 lua/Trans/init.lua       |  7 ++++---
 lua/Trans/translate.lua  |  1 +
 lua/Trans/view/hover.lua |  5 +++--
 lua/Trans/window.lua     | 18 ++----------------
 5 files changed, 14 insertions(+), 23 deletions(-)

diff --git a/lua/Trans/content.lua b/lua/Trans/content.lua
index ba2f31f..db57a43 100644
--- a/lua/Trans/content.lua
+++ b/lua/Trans/content.lua
@@ -58,8 +58,9 @@ local content = {
         self.window:bufset('modifiable', false)
     end,
 
-    actual_height = function(self)
-        if self.window:option('wrap') then
+    actual_height = function(self, wrap)
+        wrap = wrap or self.window:option('wrap')
+        if  wrap then
             local height = 0
             local width = self.window.width
             local lines = self.lines
@@ -67,6 +68,7 @@ local content = {
                 height = height + math.max(1, (math.ceil(lines[i]:width() / width)))
             end
             return height
+
         else
             return self.size
         end
diff --git a/lua/Trans/init.lua b/lua/Trans/init.lua
index 8570d4d..3a70bac 100644
--- a/lua/Trans/init.lua
+++ b/lua/Trans/init.lua
@@ -19,9 +19,9 @@ M.conf = {
             -- TODO :
             pageup = '[[',
             pagedown = ']]',
-            pin = '+',
-            close = '_',
-            toggle_entry = '--',
+            pin = '<leader>[',
+            close = '<leader>]',
+            toggle_entry = '<leader>;',
         },
         animation = {
             -- open = 'fold',
@@ -77,6 +77,7 @@ M.conf = {
         -- yes = '',
         -- no = ''
     },
+
     db_path = '$HOME/.vim/dict/ultimate.db',
 
     -- TODO :
diff --git a/lua/Trans/translate.lua b/lua/Trans/translate.lua
index 152c2dd..94e5ee5 100644
--- a/lua/Trans/translate.lua
+++ b/lua/Trans/translate.lua
@@ -31,6 +31,7 @@ local function get_word(mode)
     end
 end
 
+
 local function translate(mode, view)
     vim.validate {
         mode = { mode, 's', true },
diff --git a/lua/Trans/view/hover.lua b/lua/Trans/view/hover.lua
index 60cf74b..07218e0 100644
--- a/lua/Trans/view/hover.lua
+++ b/lua/Trans/view/hover.lua
@@ -283,15 +283,16 @@ return function(word)
         end
     else
         process.failed()
+        m_window:set_width(m_content.lines[1]:width())
     end
 
-    m_window:draw(true)
+    m_window:draw()
+    m_window.height = m_content:actual_height(true)
     m_window:open(function()
         m_window:set('wrap', true)
     end)
 
 
-
     -- Auto Close
     cmd_id = api.nvim_create_autocmd(
         hover.auto_close_events, {
diff --git a/lua/Trans/window.lua b/lua/Trans/window.lua
index 3820d0e..000a478 100644
--- a/lua/Trans/window.lua
+++ b/lua/Trans/window.lua
@@ -83,24 +83,10 @@ local window = {
 
     ---**第一次**绘制窗口的内容
     ---@param self table 窗口的对象
-    ---@param adjust boolean 是否需要调整窗口的高度和宽度 (只有窗口只有一行时才会调整宽度)
-    draw = function(self, adjust)
+    draw = function(self)
         -- TODO :
-        if self.title then
-            self.title:attach()
-        end
+        self.title:attach()
         self.content:attach()
-
-        if adjust then
-            local height = self.content:actual_height() + self.title:actual_height()
-            if self.height > height then
-                self:set_height(height)
-            end
-
-            if self.content.size == 1 and self.title.size == 0 then
-                self:set_width(self.content.lines[1]:width())
-            end
-        end
     end,
 
     open = function(self, callback)

From eb10830eb53841e7b70185462fa3748a4596da41 Mon Sep 17 00:00:00 2001
From: JuanZoran <1430359574@qq.com>
Date: Sat, 21 Jan 2023 14:26:33 +0800
Subject: [PATCH 3/4] =?UTF-8?q?refactor:=20=E5=B0=86title=E5=92=8Ccontent?=
 =?UTF-8?q?=E7=9A=84=E5=AD=97=E5=8F=98=E6=88=90=E4=BA=86contents=E7=9A=84?=
 =?UTF-8?q?=E6=95=B0=E7=BB=84,=E5=90=8C=E6=97=B6=E4=B8=8D=E5=86=8D?=
 =?UTF-8?q?=E8=87=AA=E5=8A=A8=E8=AE=BE=E7=BD=AEmodifiable?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 lua/Trans/content.lua    | 10 ++++----
 lua/Trans/view/float.lua |  8 +++---
 lua/Trans/view/hover.lua |  9 +++++--
 lua/Trans/window.lua     | 53 +++++++++++++++++-----------------------
 4 files changed, 38 insertions(+), 42 deletions(-)

diff --git a/lua/Trans/content.lua b/lua/Trans/content.lua
index db57a43..f76ce9c 100644
--- a/lua/Trans/content.lua
+++ b/lua/Trans/content.lua
@@ -41,15 +41,16 @@ local content = {
         self.size = 0
     end,
 
-    attach = function(self)
+    ---将内容连接上对应的窗口
+    ---@param self table content对象
+    ---@param offset integer 起始行
+    attach = function(self, offset)
         if self.size == 0 then
             return
         end
 
         self.window:bufset('modifiable', true)
         local window = self.window
-        local offset = self.offset
-
         api.nvim_buf_set_lines(window.bufnr, offset, offset + 1, true, self.lines)
 
         for _, hl in ipairs(self.highlights) do
@@ -153,13 +154,12 @@ local content = {
 ---content的构造函数
 ---@param window table 链接的窗口
 ---@return table 构造好的content
-return function(window, offset)
+return function(window)
     vim.validate {
         window = { window, 't' },
     }
     return setmetatable({
         modifiable = true,
-        offset = offset or 0,
         window = window,
         size = 0,
         hl_size = 0,
diff --git a/lua/Trans/view/float.lua b/lua/Trans/view/float.lua
index e43fc4a..b4c582f 100644
--- a/lua/Trans/view/float.lua
+++ b/lua/Trans/view/float.lua
@@ -2,7 +2,7 @@ local m_window
 local m_result
 
 local function set_title()
-    local title = m_window.title
+    local title = m_window.contents[1]
     local github = 'https://github.com/JuanZoran/Trans.nvim'
 
     -- TODO :config this
@@ -10,7 +10,7 @@ local function set_title()
 end
 
 local action = {
-    quit = function ()
+    quit = function()
         m_window:try_close()
     end,
 }
@@ -21,7 +21,7 @@ return function(word)
     local float = require('Trans').conf.float
     m_result    = require('Trans.query.offline')(word)
 
-    local opt = {
+    local opt          = {
         relative = 'editor',
         width    = float.width,
         height   = float.height,
@@ -30,7 +30,7 @@ return function(word)
         row      = math.floor((vim.o.lines - float.height) / 2),
         col      = math.floor((vim.o.columns - float.width) / 2),
     }
-    m_window    = require('Trans.window')(true, opt)
+    m_window           = require('Trans.window')(true, opt)
     m_window.animation = float.animation
 
     set_title()
diff --git a/lua/Trans/view/hover.lua b/lua/Trans/view/hover.lua
index 07218e0..8afac7a 100644
--- a/lua/Trans/view/hover.lua
+++ b/lua/Trans/view/hover.lua
@@ -275,7 +275,7 @@ return function(word)
 
     m_window = require("Trans.window")(false, opt)
     m_window.animation = hover.animation
-    m_content = m_window.content
+    m_content = m_window.contents[1]
 
     if m_result then
         for _, field in ipairs(conf.order) do
@@ -287,7 +287,12 @@ return function(word)
     end
 
     m_window:draw()
-    m_window.height = m_content:actual_height(true)
+
+    local height = m_content:actual_height(true)
+    if height < m_window.height then
+        m_window:set_height(height)
+    end
+
     m_window:open(function()
         m_window:set('wrap', true)
     end)
diff --git a/lua/Trans/window.lua b/lua/Trans/window.lua
index 000a478..e385bca 100644
--- a/lua/Trans/window.lua
+++ b/lua/Trans/window.lua
@@ -85,8 +85,11 @@ local window = {
     ---@param self table 窗口的对象
     draw = function(self)
         -- TODO :
-        self.title:attach()
-        self.content:attach()
+        local offset = 0
+        for _, content in ipairs(self.contents) do
+            content:attach(offset)
+            offset = offset + content.size
+        end
     end,
 
     open = function(self, callback)
@@ -193,20 +196,18 @@ local window = {
 }
 
 ---@class window
----@field title table 窗口不变的title对象,载入后不可修改
 ---@field winid integer 窗口的handle
 ---@field bufnr integer 窗口对应buffer的handle
----@field content table 窗口内容的对象, 和title一样是content类
 ---@field width integer 窗口当前的宽度
 ---@field height integer 窗口当前的高度
 ---@field hl integer 窗口highlight的namespace
-
+---@field contents table[] 窗口内容的对象数组
 
 
 ---窗口对象的构造器
 ---@param entry boolean 光标初始化时是否应该进入窗口
 ---@param option table 需要设置的选项
----@return window
+---@return window win
 ---@nodiscard
 return function(entry, option)
     vim.validate {
@@ -235,34 +236,24 @@ return function(entry, option)
     local bufnr = api.nvim_create_buf(false, true)
     local winid = api.nvim_open_win(bufnr, entry, opt)
 
-    local win = setmetatable({
-        winid   = winid,
-        bufnr   = bufnr,
-        title   = nil,
-        content = nil,
-        width   = opt.width,
-        height  = opt.height,
-        hl      = api.nvim_create_namespace('TransWinHl'),
-    },
-        { __index = function(tbl, key)
-            if key == 'content' then
-                if tbl.title then
-                    tbl.content = require('Trans.content')(tbl, tbl.title.size)
-                    tbl.title.modifiable = false
-                else
-                    tbl.content = require('Trans.content')(tbl)
-                end
-                return tbl.content
 
-            elseif key == 'title' then
-                tbl.title = require('Trans.content')(tbl, 0)
-                return tbl.title
-
-            else
-                return window[key]
+    local win
+    win = {
+        winid    = winid,
+        bufnr    = bufnr,
+        width    = opt.width,
+        height   = opt.height,
+        hl       = api.nvim_create_namespace('TransWinHl'),
+        contents = setmetatable({}, {
+            __index = function(self, key)
+                assert(type(key) == 'number')
+                self[key] = require('Trans.content')(win)
+                return self[key]
             end
-        end })
+        })
+    }
 
+    setmetatable(win, { __index = window })
     win:set('winhl', 'Normal:TransWin,FloatBorder:TransBorder')
     win:bufset('filetype', 'Trans')
     win:bufset('buftype', 'nofile')

From 294ffe1b9f92ee2570e8bad50d2729449dc05011 Mon Sep 17 00:00:00 2001
From: JuanZoran <1430359574@qq.com>
Date: Sat, 21 Jan 2023 21:13:51 +0800
Subject: [PATCH 4/4] feat: add auto_play for hover mode

---
 .gitignore               |  3 ++-
 README.md                | 39 ++++++++++++++++++++++++++++-----------
 install.sh               |  1 +
 lua/Trans/init.lua       | 14 ++++++++------
 lua/Trans/setup.lua      |  1 +
 lua/Trans/view/hover.lua | 24 +++++++++++++++++++-----
 tts/package.json         |  5 +++++
 tts/say.js               |  6 ++++++
 8 files changed, 70 insertions(+), 23 deletions(-)
 create mode 100644 tts/package.json
 create mode 100644 tts/say.js

diff --git a/.gitignore b/.gitignore
index 89f94ce..70d18fb 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,6 +1,7 @@
 lua/Trans/util/
 lua/Trans/test/
 note/
-go/
 demo.mp4
 screenshot.gif
+tts/node_modules/
+tts/package-lock.json
diff --git a/README.md b/README.md
index fbe1ba2..76e15f7 100644
--- a/README.md
+++ b/README.md
@@ -103,6 +103,12 @@ use {
     
   > 后续会增加 `healthcheck` 进行检查
 
+- **`auto_play`** 使用步骤:
+    - 需要确保安装了`nodejs`
+    - 进入插件的`tts`目录运行`npm install`
+        > 如果`install.sh`运行正常则自动安装,如果安装失败,请尝试手动安装
+    - linux 用户需要额外安装以下依赖:
+        > sudo apt-get install festival festvox-kallpc16k
 
 ## 配置
 ```lua
@@ -125,14 +131,24 @@ require'Trans'.setup {
             -- TODO :
             pageup = '[[',
             pagedown = ']]',
-            pin = '_', -- 将窗口固定在右上角, 参考demo
-            close = '+',
+            pin = '<leader>[',
+            close = '<leader>]',
+            toggle_entry = '<leader>;',
+            play = '_',
         },
         animation = {
-            open = 'slid', -- 可选的样式: slid , fold 
+            -- open = 'fold',
+            -- close = 'fold',
+            open = 'slid',
             close = 'slid',
-            interval = 12, -- 动画的帧间隔
-        }
+            interval = 12,
+        },
+        auto_close_events = {
+            'InsertEnter',
+            'CursorMoved',
+            'BufLeave',
+        },
+        auto_play = true, -- WARN : 请阅读说明
     },
     float = {
         width = 0.8,
@@ -167,14 +183,15 @@ require'Trans'.setup {
     },
     icon = {
         star = '',
-        notfound = '❔',
-        yes = '✔️',
-        no = '❌'
+        -- notfound = '❔',
+        notfound = ' ',
+        yes = ' ',
+        no = ''
+        -- yes = '✔️',
+        -- no = '❌'
         -- star = '⭐',
-        -- notfound = '',
-        -- yes = '',
-        -- no = ''
     },
+
     db_path = '$HOME/.vim/dict/ultimate.db',
 
     -- TODO :
diff --git a/install.sh b/install.sh
index a9adb84..991d623 100755
--- a/install.sh
+++ b/install.sh
@@ -15,3 +15,4 @@ wget https://github.com/skywind3000/ECDICT-ultimate/releases/download/1.0.0/ecdi
 unzip /tmp/dict.zip -d $HOME/.vim/dict
 
 rm -rf /tmp/dict.zip
+cd ./tts/ && npm install
diff --git a/lua/Trans/init.lua b/lua/Trans/init.lua
index 3a70bac..e62de37 100644
--- a/lua/Trans/init.lua
+++ b/lua/Trans/init.lua
@@ -22,6 +22,7 @@ M.conf = {
             pin = '<leader>[',
             close = '<leader>]',
             toggle_entry = '<leader>;',
+            play = '_',
         },
         animation = {
             -- open = 'fold',
@@ -35,6 +36,7 @@ M.conf = {
             'CursorMoved',
             'BufLeave',
         },
+        auto_play = true,
     },
     float = {
         width = 0.8,
@@ -69,13 +71,13 @@ M.conf = {
     },
     icon = {
         star = '',
-        notfound = '❔',
-        yes = '✔️',
-        no = '❌'
+        -- notfound = '❔',
+        notfound = ' ',
+        yes = ' ',
+        no = ''
+        -- yes = '✔️',
+        -- no = '❌'
         -- star = '⭐',
-        -- notfound = '',
-        -- yes = '',
-        -- no = ''
     },
 
     db_path = '$HOME/.vim/dict/ultimate.db',
diff --git a/lua/Trans/setup.lua b/lua/Trans/setup.lua
index 647e976..ad49e4d 100644
--- a/lua/Trans/setup.lua
+++ b/lua/Trans/setup.lua
@@ -2,6 +2,7 @@ if vim.fn.executable('sqlite3') ~= 1 then
     error('Please check out sqlite3')
 end
 
+
 vim.api.nvim_create_user_command('Translate', function()
     require("Trans").translate()
 end, { desc = '  单词翻译', })
diff --git a/lua/Trans/view/hover.lua b/lua/Trans/view/hover.lua
index 8afac7a..68f5e48 100644
--- a/lua/Trans/view/hover.lua
+++ b/lua/Trans/view/hover.lua
@@ -186,6 +186,8 @@ end
 
 local action
 local next
+local _word
+
 action = {
     pageup = function()
         m_window:normal('gg')
@@ -251,7 +253,12 @@ action = {
         else
             vim.keymap.del('n', conf.hover.keymap.toggle_entry, { buffer = true })
         end
-    end
+    end,
+
+    play = function()
+        local file = debug.getinfo(1, "S").source:sub(2):match('(.*)lua/') .. 'tts/say.js'
+        vim.fn.jobstart('node ' .. file .. ' ' .. _word)
+    end,
 }
 
 
@@ -259,11 +266,15 @@ return function(word)
     vim.validate {
         word = { word, 's' },
     }
-
+    _word       = word
     -- 目前只处理了本地数据库的查询
     m_result    = require('Trans.query.offline')(word)
     local hover = conf.hover
-    local opt   = {
+    if hover.auto_play then
+        action.play()
+    end
+
+    local opt = {
         relative = 'cursor',
         width    = hover.width,
         height   = hover.height,
@@ -298,6 +309,7 @@ return function(word)
     end)
 
 
+
     -- Auto Close
     cmd_id = api.nvim_create_autocmd(
         hover.auto_close_events, {
@@ -310,7 +322,9 @@ return function(word)
         end,
     })
 
-    for act, key in pairs(hover.keymap) do
-        vim.keymap.set('n', key, action[act], { buffer = true, silent = true })
+    if m_result then
+        for act, key in pairs(hover.keymap) do
+            vim.keymap.set('n', key, action[act], { buffer = true, silent = true })
+        end
     end
 end
diff --git a/tts/package.json b/tts/package.json
new file mode 100644
index 0000000..e5abbcc
--- /dev/null
+++ b/tts/package.json
@@ -0,0 +1,5 @@
+{
+  "dependencies": {
+    "say": "^0.16.0"
+  }
+}
diff --git a/tts/say.js b/tts/say.js
new file mode 100644
index 0000000..34fc28d
--- /dev/null
+++ b/tts/say.js
@@ -0,0 +1,6 @@
+const say = require('say')
+
+word = process.argv
+
+// console.log(word)
+say.speak(word.slice(2))