diff --git a/README.md b/README.md index f294f75..783b830 100644 --- a/README.md +++ b/README.md @@ -96,36 +96,113 @@ use { ## 配置 ```lua -require'Trans'.setup{ - display = { - style = 'minimal', - max_height = 50, -- 小于0代表无限制 - max_width = 50, - collins_star = true, -- 是否显示柯林斯星级 - oxford = true, -- 是否显示为牛津3000词汇 - wrap = true, -- 是否折叠超出width的部分 - border_style = 'rounded', -- 边框属性 - view = 'cursor', -- TODO, 目前还未测试, 请不要更改 - offset_x = 2, -- 弹窗相对光标的偏移 - offset_y = 2, +require'Trans'.setu{ + style = { + ui = { + input = 'float', + n = 'cursor', + v = 'cursor' + }, + window = { + cursor = { + border = 'rounded', + width = 50, -- 最大宽高 + height = 50, + }, + float = { + border = 'rounded', + width = 0.9, -- 相对宽高 + height = 0.8, + }, + limit = nil, + }, }, - order = { -- 排版的顺序 - 'title', -- 如果你不想显示某一部分, 可以直接删除该部分 - 'tag', - 'pos', - 'exchange', - 'zh', - 'en', + order = { + offline = { + 'Title', + 'Tag', + 'Pos', + 'Exchange', + 'Translation', + -- NOTE :如果你想限制某个组件的行数,可以设置max_size + -- { 'Definition', max_size = 4 }, -- TODO : better format + }, + -- online = { + -- -- TODO + -- }, }, + ui = { + highlight = { + TransWord = { + fg = '#7ee787', + bold = true, + }, + TransPhonetic = { + link = 'Linenr' + }, + TransRef = { + fg = '#75beff', + bold = true, + }, + TransTag = { + fg = '#e5c07b', + }, + TransExchange = { + link = 'TransTag', + }, + TransPos = { + link = 'TransTag', + }, + TransTranslation = { + link = 'TransWord', + }, + TransDefinition = { + -- fg = '#bc8cff', + link = 'Moremsg', + }, + TransCursorWin = { + link = 'Normal', + }, - db_path = '$HOME/.vim/dict/ultimate.db', -- 词典的数据库位置 - -- 如果你是手动安装, 可以自定义 - icon = { - star = '⭐', -- 柯林斯星级图标 - isOxford = '✔', -- 牛津3000词汇的标志 - notOxford = '' + TransCursorBorder = { + link = 'FloatBorder', + } + }, + icon = { + star = '⭐', + isOxford = '✔', + notOxford = '' + }, + display = { + phnoetic = true, + collins = true, + oxford = true, + -- TODO + -- history = false, + }, }, - auto_close = true, -- 移动光标关闭弹窗 + base = { + db_path = '$HOME/.vim/dict/ultimate.db', + auto_close = true, + engine = { + -- TODO + 'offline', + } + }, + -- map = { + -- -- TODO + -- }, + -- history = { + -- -- TOOD + -- } + + -- TODO add online translate engine + -- online_search = { + -- enable = false, + -- engine = {}, + -- } + + -- TODO register word } ``` @@ -134,25 +211,14 @@ require'Trans'.setup{ > 示例中展示, 将`mm`映射成快捷键 ```lua -- normal-mode -vim.keymap.set('n', 'mm', 'TranslateCursorWord') - --- visual-mode -vim.keymap.set('v', 'mm', 'TranslateSelectWord') +vim.keymap.set({'n', 'v'}, 'mm', 'Translate') +vim.keymap.set('n', 'mi', 'TranslateInput') ``` ## 高亮组 ```lua -hlgroup = { - word = 'TransWord', - phonetic = 'TransPhonetic', - ref = 'TransRef', -- 如: 标签: | 中文翻译: 之类的前导词 - tag = 'TransTag', - exchange = 'TransExchange', - pos = 'TransPos', - zh = 'TransZh', - en = 'TransEn', -} +-- TODO : add explanation ``` ## 声明 diff --git a/lua/Trans/component/offline/Translation.lua b/lua/Trans/component/offline/Translation.lua index de61ffb..bdfe0e0 100644 --- a/lua/Trans/component/offline/Translation.lua +++ b/lua/Trans/component/offline/Translation.lua @@ -1,6 +1,6 @@ local M = {} -M.component = function(field) +M.component = function(field, max_size) if field.translation then local ref = { { '中文翻译', 'TransRef' } @@ -12,8 +12,13 @@ M.component = function(field) emptyline = true, needformat = true, } + local size = 0 for trans in vim.gsplit(field.translation, '\n', true) do + size = size + 1 table.insert(translations, trans) + if size == max_size then + break + end end return { ref, translations } diff --git a/lua/Trans/conf/default.lua b/lua/Trans/conf/default.lua index 57a60b9..1554085 100644 --- a/lua/Trans/conf/default.lua +++ b/lua/Trans/conf/default.lua @@ -4,13 +4,13 @@ M.conf = { style = { ui = { input = 'float', - normal = 'cursor', - select = 'cursor' + n = 'cursor', + v = 'cursor', }, window = { cursor = { border = 'rounded', - width = 50, + width = 40, height = 50, }, float = { @@ -18,14 +18,9 @@ M.conf = { width = 0.9, height = 0.8, }, - -- NOTE :如果你想限制某个组件的行数,可以设置 (名称与order相同) - -- Example: - -- limit = { - -- En = 1, -- 只显示第一行,(一般为最广泛的释义) - -- }, - limit = nil, }, }, + order = { offline = { 'Title', @@ -33,6 +28,7 @@ M.conf = { 'Pos', 'Exchange', 'Translation', + -- NOTE :如果你想限制某个组件的行数,可以设置max_size -- { 'Definition', max_size = 4 }, }, -- online = { diff --git a/lua/Trans/core/translate.lua b/lua/Trans/core/translate.lua index 4ce2295..26177a4 100644 --- a/lua/Trans/core/translate.lua +++ b/lua/Trans/core/translate.lua @@ -4,14 +4,13 @@ local core = require("Trans.core") local function get_opts(opts) + + local mode = vim.api.nvim_get_mode().mode local default_conf = { - method = vim.api.nvim_get_mode().mode, - engine = { - 'offline', - -- TODO : other engine - }, + method = mode, + engine = conf.base.engine, win = { - style = 'cursor', + style = conf.style.ui[opts.method or mode], width = conf.style.window.cursor.width, height = conf.style.window.cursor.height }, @@ -36,7 +35,6 @@ local function get_opts(opts) return vim.tbl_extend('force', default_conf, opts) end - -- EXAMPLE : -- require('Trans').translate({ -- method = 'input', -- 不填则自动判断mode获取查询的单词 @@ -85,7 +83,6 @@ local function create_win(win) return bufnr, winid end - local function translate(opts) vim.validate { opts = { opts, 'table', true }