feat: implement database query, TODO: use neovim popup api

This commit is contained in:
JuanZoran 2022-12-17 23:33:21 +08:00
parent 5cf91a5a17
commit 999d7f5d12
6 changed files with 121 additions and 55 deletions

39
lua/Trans/conf.lua Normal file
View File

@ -0,0 +1,39 @@
return {
display = {
style = 'default',
phnoetic = true,
collins_star = true,
tag = true,
oxford = true,
history = false,
},
default_keymap = true,
map = {
},
view = {
-- TODO: style: buffer | cursor | window
-- style = 'buffer',
-- buffer_pos = 'bottom', -- only works when view.style == 'buffer'
},
db_path = '/home/zoran/project/neovim/ecdict-sqlite-28/stardict.db', -- FIXME: change the path
-- TODO: async process
-- async = false,
-- TODO: add online translate engine
-- online_search = {
-- enable = false,
-- engine = {},
-- }
-- TODO: precise match or return closest match result
-- precise_match = true,
-- TODO: leamma search
-- leamma = false,
-- TODO: register word
}

22
lua/Trans/database.lua Normal file
View File

@ -0,0 +1,22 @@
local M = {}
-- local db = require('Trans').db
local dict = require("Trans").dict
function M.query(arg)
-- TODO: return type: a result table:
local res = {}
if type(arg) == 'string' then
res = dict:select('stardict', {
where = { word = arg },
})
elseif type(arg) == 'table' then
res = dict:select('stardict', arg)
else
vim.notify('query argument error!')
end
vim.pretty_print(res)
return res[1]
end
return M

28
lua/Trans/display.lua Normal file
View File

@ -0,0 +1,28 @@
-- local conf = require("Trans").conf
local M = {}
-- {
-- audio = "",
-- bnc = 5222,
-- definition = "n. slang for sexual intercourse",
-- exchange = "d:fucked/p:fucked/i:fucking/3:fucks/s:fucks",
-- frq = 5040,
-- id = 1180286,
-- phonetic = "fʌk",
-- pos = "n:37/v:63",
-- sw = "fuck",
-- tag = "",
-- translation = "vt. 与...性交, 欺骗, 诅咒\nvi. 性交\nn. 性交, 些微, 杂种\ninterj. 他妈的, 混帐",
-- word = "fuck"
-- }
--
function M.query_cursor()
-- TODO:
end
function M.query()
-- TODO:
end
return M

View File

@ -1,6 +1,5 @@
local M = {}
-- NOTE:
-- Style:
-- [minimal]: one line with '/'
@ -8,54 +7,16 @@ local M = {}
-- [full]: show all description in different lines
-- TODO: other style
local default_conf =
{
display = {
style = 'default',
phnoetic = true,
collins_star = true,
tag = true,
oxford = true,
-- TODO: frequency
-- frequency = false,
history = false,
},
default_keymap = true,
map = {
},
view = {
-- TODO: style: buffer | cursor | window
-- style = 'buffer',
-- buffer_pos = 'bottom', -- only works when view.style == 'buffer'
},
db_path = '/home/zoran/project/neovim/ecdict-sqlite-28/stardict.db', -- FIXME: change the path
-- TODO: async process
async = false,
-- TODO: add online translate engine
-- online_search = {
-- enable = false,
-- engine = {},
-- }
-- TODO: precise match or return closest match result
-- precise_match = true,
-- TODO: leamma search
-- leamma = false,
-- TODO: register word
}
M.conf = default_conf
function M:setup(conf)
self.config = vim.tbl_extend('force', default_conf, conf)
M.conf = require("Trans.conf")
function M.setup(conf)
M.conf = vim.tbl_extend('force', M.conf, conf)
require("Trans.setup")
end
M.db = require("sqlite.db")
M.dict = M.db:open(M.conf.db_path)
M.query = require("Trans.display").query
M.query_cursor = require("Trans.display").query_cursor
return M

View File

@ -1,6 +0,0 @@
local sqlite = require("sqlite")
local conf = require('Trans').conf
local path = conf.db_path
local db = sqlite.new(conf.db_path)

22
lua/Trans/setup.lua Normal file
View File

@ -0,0 +1,22 @@
local db = require("Trans").db
local conf = require("Trans").conf
local group = vim.api.nvim_create_augroup('closedb', { clear = true })
vim.api.nvim_create_autocmd('VimLeave', {
group = group,
pattern = '*',
callback = function()
if db:isopen() then
db:close()
end
end,
})
vim.api.nvim_create_user_command('TranslateCurosorWord', require("Trans").query_cursor, {
})
if conf.default_keymap then
--- TODO: keymap
end