feat: add more bugs

This commit is contained in:
JuanZoran
2023-01-09 21:30:16 +08:00
parent fef956e36d
commit 67c6ffa989
20 changed files with 149 additions and 144 deletions

View File

@ -53,7 +53,7 @@
- `history`
- 查询引擎(engine): `string | table`
- `local` 本地的数据库
- `offline` 离线的数据库
- `youcao` 有道api
- `baidu` 百度api
- `google` 谷歌api
@ -81,7 +81,7 @@ vim.keymap.set('n', 'mi', function ()
require('Trans').translate({
method = 'input', -- 不填则自动判断mode获取查询的单词
engine = { -- 异步查询所有的引擎, 按照列表
'local',
'offline',
'youdao',
'baidu'
},

View File

@ -1,19 +0,0 @@
local M = {}
local type_check = require("Trans.util.debug").type_check
local offline_dir = debug.getinfo(1, "S").source:sub(2):match('.*Trans') .. '/component/offline'
M.to_content = function(query_res)
type_check {
query_res = { query_res, 'table' }
}
local content = {}
for file in vim.fs.dir(offline_dir) do
local res = require("Trans.component.offline." .. file:gsub('.lua', '')).to_content(query_res)
assert(res)
table.insert(content, res)
end
return content
end
return M

7
lua/Trans/core/init.lua Normal file
View File

@ -0,0 +1,7 @@
local M = {}
M.process = require('Trans.core.process')
M.query = require('Trans.core.query')
M.show_win = require('Trans.core.show_win')
return M

View File

@ -0,0 +1,22 @@
local type_check = require("Trans.util.debug").type_check
local format = require("Trans.util.format")
-- NOTE : 将请求得到的字段进行处理
-- local offline_dir = debug.getinfo(1, "S").source:sub(2):match('.*Trans') .. '/component/offline'
local function process (opts)
type_check {
opts = { opts, 'table' }
}
local content = require('Trans.component.content'):new()
for _, v in ipairs(opts.order) do
local component = format.format(opts.win_style, require("Trans.component" .. opts.engine .. v))
content:insert(component)
end
return content:lines()
end
return process

View File

@ -0,0 +1,53 @@
local type_check = require("Trans.util.debug").type_check
local buf_opts = {
filetype = 'Trans',
modifiable = false,
}
-- local win_opts = {
-- winhl = 'Normal:TransWinNormal, FloatBorder:TransWinBorder'
-- }
local function caculate_format(height, width)
local col = math.floor((vim.o.lines - height - vim.o.cmdheight) / 2)
local row = math.floor((vim.o.columns - width) / 2)
return row, col
end
local function show_win(opts)
type_check {
opts = { opts, 'table' },
win = { opts.win, 'table' },
highlight = { opts.highlight, 'table', true },
}
local bufnr = vim.api.nvim_create_buf(false, true)
for k, v in pairs(buf_opts) do
vim.api.nvim_buf_set_option(bufnr, k, v)
end
local is_float = opts.style == 'float'
local win_opts = {
relative = opts.style == 'float' and 'editor' or 'cursor',
width = opts.width,
height = opts.height,
style = 'minimal',
border = 'rounded',
title = 'Trans',
title_pos = 'center',
focusable = true,
zindex = 100,
}
if is_float then
win_opts.row, win_opts.col = caculate_format(win_opts.height, win_opts.width)
else
win_opts.row = 2
win_opts.col = 2
end
local winid = vim.api.nvim_open_win(bufnr, is_float, win_opts)
return bufnr, winid
end
return show_win