Trans.nvim/lua/Trans/core/frontend.lua

66 lines
2.0 KiB
Lua
Raw Normal View History

local Trans = require 'Trans'
---@class TransFrontendOpts
local default_frontend = {
auto_play = true,
query = 'fallback',
border = 'rounded',
title = vim.fn.has 'nvim-0.9' == 1 and {
2023-09-08 22:44:23 +08:00
{ '', 'TransTitleRound' },
{ ' Trans', 'TransTitle' },
2023-09-08 22:44:23 +08:00
{ '', 'TransTitleRound' },
} or nil, -- need nvim-0.9+
---@type {open: string | boolean, close: string | boolean, interval: integer} Window Animation
animation = {
open = 'slid', -- 'fold', 'slid'
close = 'slid',
interval = 12,
},
2023-09-08 22:44:23 +08:00
timeout = 2000, -- only for online backend query
}
if Trans.conf.frontend.default then
default_frontend = vim.tbl_extend('force', default_frontend, Trans.conf.frontend.default)
end
2023-09-08 22:44:23 +08:00
local function empty_method(method)
return function() error('Method [' .. method .. '] not implemented') end
end
2023-03-14 18:17:07 +08:00
---@class TransFrontend
---@field opts? TransFrontendOpts options which user can set
2023-03-14 18:17:07 +08:00
---@field get_active_instance fun():TransFrontend?
2023-03-26 20:07:04 +08:00
---@field wait fun(self: TransFrontend): fun(backend: TransBackend): boolean Update wait status
2023-03-14 18:17:07 +08:00
---@field execute fun(action: string) @Execute action for frontend instance
---@field setup? fun() @Setup method for frontend [optional] **NOTE: This method will be called when frontend is first used**
2023-09-08 22:44:23 +08:00
local M = {
---@type fun() @Fallback method when no result
fallback = empty_method 'fallback',
---@type fun(self: TransFrontend, data: TransData) @render backend result
process = empty_method 'process',
}
2023-03-14 18:17:07 +08:00
---@class Trans
---@field frontend TransFrontend
2023-09-08 22:44:23 +08:00
return setmetatable(M, {
2023-03-12 21:33:00 +08:00
__index = function(self, name)
2023-03-14 18:17:07 +08:00
---@type TransFrontend
local frontend = require('Trans.frontend.' .. name)
2023-03-12 21:33:00 +08:00
frontend.opts =
vim.tbl_extend('force', frontend.opts or {}, default_frontend, Trans.conf.frontend[name])
2023-03-12 21:33:00 +08:00
if frontend.setup then
frontend.setup()
end
rawset(self, name, frontend)
2023-03-12 21:33:00 +08:00
return frontend
2023-03-24 00:56:36 +08:00
end,
2023-03-12 21:33:00 +08:00
})