diff --git a/lua/Trans/component/offline/Definition.lua b/lua/Trans/component/offline/Definition.lua index cf04e60..94acd96 100644 --- a/lua/Trans/component/offline/Definition.lua +++ b/lua/Trans/component/offline/Definition.lua @@ -1,6 +1,6 @@ local M = {} -M.to_lines = function (field) +M.to_content = function (field) -- TODO end diff --git a/lua/Trans/component/offline/Exchange.lua b/lua/Trans/component/offline/Exchange.lua index cf04e60..94acd96 100644 --- a/lua/Trans/component/offline/Exchange.lua +++ b/lua/Trans/component/offline/Exchange.lua @@ -1,6 +1,6 @@ local M = {} -M.to_lines = function (field) +M.to_content = function (field) -- TODO end diff --git a/lua/Trans/component/offline/Pos.lua b/lua/Trans/component/offline/Pos.lua index cf04e60..94acd96 100644 --- a/lua/Trans/component/offline/Pos.lua +++ b/lua/Trans/component/offline/Pos.lua @@ -1,6 +1,6 @@ local M = {} -M.to_lines = function (field) +M.to_content = function (field) -- TODO end diff --git a/lua/Trans/component/offline/Tag.lua b/lua/Trans/component/offline/Tag.lua index cf04e60..94acd96 100644 --- a/lua/Trans/component/offline/Tag.lua +++ b/lua/Trans/component/offline/Tag.lua @@ -1,6 +1,6 @@ local M = {} -M.to_lines = function (field) +M.to_content = function (field) -- TODO end diff --git a/lua/Trans/component/offline/Title.lua b/lua/Trans/component/offline/Title.lua index cf04e60..71d3068 100644 --- a/lua/Trans/component/offline/Title.lua +++ b/lua/Trans/component/offline/Title.lua @@ -1,7 +1,31 @@ local M = {} -M.to_lines = function (field) +local display = require("Tran.conf").ui.display +-- Example: +-- local content = { +-- width = 1, +-- height = 1; +-- lines = { +-- Highlight = { +-- 'first line', +-- 'second line', +-- } +-- }, ---@table +-- } + + +-- local function format() +-- +-- end + +M.to_content = function (field) -- TODO + local line = '' + local format = '%s %s %s %s' + local content = { + height = 1, + } + return content end return M diff --git a/lua/Trans/component/offline/Translation.lua b/lua/Trans/component/offline/Translation.lua index cf04e60..94acd96 100644 --- a/lua/Trans/component/offline/Translation.lua +++ b/lua/Trans/component/offline/Translation.lua @@ -1,6 +1,6 @@ local M = {} -M.to_lines = function (field) +M.to_content = function (field) -- TODO end diff --git a/lua/Trans/conf/default.lua b/lua/Trans/conf/default.lua index 7212202..80d8694 100644 --- a/lua/Trans/conf/default.lua +++ b/lua/Trans/conf/default.lua @@ -16,24 +16,12 @@ M.conf = { 'Definition', }, window = { - -- NOTE :可选的风格:['fixed', 'relative', .. TODO] - -- width 和 height说明: - -- 大于1: - -- 如果style为fixed , 则为固定的长宽 - -- 如果style为relative , 则为最大长宽 - -- 小于1: - -- 如果style为fixed , 则为默认 - -- 如果style为relative , 则为无限制 - -- 0 ~ 1: - -- 相对长宽 cursor = { - style = 'fixed', border = 'rounded', width = 30, height = 30, }, float = { - style = 'fixed', border = 'rounded', width = 0.8, height = 0.9, @@ -43,7 +31,7 @@ M.conf = { -- limit = { -- En = 1, -- 只显示第一行,(一般为最广泛的释义) -- }, - limit = nil, + limit = nil, }, }, ui = { @@ -98,6 +86,9 @@ M.conf = { unknown_conf = true, }, }, + map = { + -- TODO + }, -- TODO add online translate engine -- online_search = { -- enable = false, diff --git a/lua/Trans/util/format.lua b/lua/Trans/util/format.lua new file mode 100644 index 0000000..7d9bf80 --- /dev/null +++ b/lua/Trans/util/format.lua @@ -0,0 +1,124 @@ +---@diagnostic disable: undefined-global +local M = {} +local type_check = require("Trans.util.debug").type_check + +-- 各种风格的基础宽度 +local style_width = { + float = require("Trans.conf.window").float.width, -- NOTE : need window parsed conf + cursor = require("Trans.conf.window").cursor.width, +} + +local m_width = nil -- 需要被格式化窗口的高度 +local m_fields = nil -- 待格式化的字段 +local m_indent = nil -- 每行的行首缩进 +local m_length = nil -- 所有字段加起来的长度(不包括缩进和间隔) + +local function get_rows() + -- TODO + return rows +end + +local function do_indent(lines) + for i, v in ipairs(lines) do + lines[i] = (' '):rep(m_indent) .. v + end +end + +local function format_to_line() + local space = math.floor((m_width - m_length) / #m_fields) + return line +end + +local function format_to_multilines() + -- TODO + type_check { + interval = { interval, 'number' }, + rows = { rows, 'number' }, + } +end + +local function get_formatted_lines() + local lines = {} + -- NOTE : 判断能否格式化成一行 + if m_length + (#m_fields * m_indent) > m_width then + lines = format_to_multilines() + else + lines[1] = format_to_line() + end + + if m_indent then + do_indent(lines) + end + return lines +end + +---将组件格式化成相应的vim支持的lines格式 +---@param style string 窗口的风格 +---@param fields string[] 需要格式化的字段 +---@param indent number 缩进的长度 +---@return string[] lines 便于vim.api.nvim_buf_set_lines +M.to_lines = function(style, fields, indent) + if not fields then + return {} + end + type_check { + style = { style, { 'string' } }, + fields = { fields, { 'table' } }, + indent = { indent, { 'number' }, true }, + } + + local length = 0 + for _, v in ipairs(fields) do + length = length + #v + end + + m_width = style_width[style] - indent + m_indent = indent + m_fields = fields + m_length = length + return get_formatted_lines() +end + +-- local function get_lines(win_width, components) +-- local lines = {} +-- local interval = win_width > 40 and 6 or 4 +-- local row = 1 +-- local width = win_width - #components[1] +-- for i in 2, #components do +-- width = width - #components[i] - interval +-- if width < 0 then +-- width = win_width - #components[i] +-- row = row + 1 +-- end +-- end +-- if row == 1 then +-- local format = '%s' .. ((' '):rep(interval) .. '%s') +-- lines[1] = string.format(format, unpack(components)) +-- else +-- table.sort(components, function (a, b) +-- return #a > #b +-- end) +-- -- FIXME +-- local res, rem = #components / (row + 1), #components % (row + 1) +-- row = math.ceil(res) +-- local rol = row - rem - 1 +-- end +-- +-- return lines +-- end +-- +-- M.format = function(style, components, indent) +-- local lines = {} +-- if #components > 1 then +-- indent = indent or 0 +-- type_check { +-- style = { style, 'string' }, +-- components = { components, 'table' }, ---@string[] +-- -- max_items = { max_items, { 'nil', 'number' } }, ---@string[] +-- } +-- local win_width = (style == 'float' and float_win_width or cursor_win_width) - indent +-- local res = get_lines(win_width, components) +-- end +-- return lines +-- end +return M diff --git a/lua/Trans/util/test/a.lua b/lua/Trans/util/test/a.lua new file mode 120000 index 0000000..9b39cbe --- /dev/null +++ b/lua/Trans/util/test/a.lua @@ -0,0 +1 @@ +query_youdao.lua \ No newline at end of file diff --git a/lua/Trans/util/test/query_youdao.lua b/lua/Trans/util/test/query_youdao.lua new file mode 100644 index 0000000..1d064f8 --- /dev/null +++ b/lua/Trans/util/test/query_youdao.lua @@ -0,0 +1,66 @@ +local M = {} +-- local type_check = require("Trans.util.debug").type_check +local salt = '96836db9-1e28-4789-b5a6-fb7bb67e1259' +local appKey = '1858465a8708c121' +local appPasswd = 'fG0sitfk16nJOlIlycnLPYZn1optxUxL' + +local curtime +local word + +local function caculate_input() + local input + local len = #word + if len > 20 then + input = word:sub(1, 10) .. len .. word:sub(-10) + else + input = word + end + return input +end + +local function caculate_sign() + -- sign=sha256(应用ID+input+salt+curtime+应用密钥); + local hash = appKey .. caculate_input() .. salt .. curtime .. appPasswd + + return vim.fn.sha256(hash) +end + +local function test() + local query = { + q = word, + from = 'auto', + to = 'zh-CHS', + -- dicts = 'ec', + signType = 'v3', + appKey = appKey, + salt = salt, + curtime = curtime, + sign = caculate_sign(), + } + return query +end + +-- curl --data {{'{"name":"bob"}'}} --header {{'Content-Type: application/json'}} {{http://example.com/users/1234}} + +local function query_word(q) + local field = ( + [[curl -s --header 'Content-Type: application/x-www-form-urlencoded' https://openapi.youdao.com/api]]) + for k, v in pairs(q) do + field = field .. ([[ -d '%s=%s']]):format(k, v) + end + -- vim.pretty_print(field) + local output = vim.fn.system(field) + local tb = vim.fn.json_decode(output) + -- print(type(output)) + -- vim.pretty_print(tb.basic) +end + +M.test = function(query) + curtime = tostring(os.time()) -- 更新一下time + word = query or 'as' + -- local json = vim.fn.json_encode(test()) + query_word(test()) + -- vim.pretty_print(vim.fn.json_encode(json)) +end + +return M