[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/shewer/librime-lua-script/main/lua/init_processor.lua [Back]  [Original]

-- init_processor
-- engine processorinitconfig
-- 
-- lua global  _G[]    /modules sub_module (name_space  global )
-- init_processor  module tab    _G[]
--   lua_processor@@  init_processor init fini func  sub_module init fini func
--
--
--  lua_processor init & fini    engine/processors

--
--
--
-- librime-lua-script env
require 'tools/_component'
local Env = require 'tools/env_api'
local List = require 'tools/list'

_NR = package.config:sub(1,1):match("/") and "\n" or "\r"
-- init_module only for Processor

local FS={} -- commit_text
function FS.screen_print(env)
  local engine = env.engine
  local page_size = engine.schema.page_size
  local context = engine.context
  local seg = context.composition:back()
  local s_index = seg.selected_index
  local st = s_index - s_index % page_size

  local str_head = context:get_selected_candidate().preedit .. "\t" .. seg.prompt .. _NR
  local strlist= List.Range(st,st+page_size-1):map(function(elm)
    local c=seg:get_candidate_at(elm)
    local head = elm == s_index and '-->' or '   '
    return c and ("%s%s:\t%s%s"):format(head, c.text, c.comment,_NR) or nil
  end):concat()
  return (str_head .. strlist)
end

function FS.modules(env)
  return env.modules:map(
  function(elm) return ("%s: %s%s"):format( elm.name,elm.comp,_NR) end):concat()
end

function FS.comps(env)
  return env:components_str():map(function(elm)
    return elm:gsub('engine/','') end):concat(_NR)
end

function FS.cal(env)
  local cal = require 'tools/cowsay'
  return ('```%s%s%s```%s'):format(_NR,cal(),_NR,_NR)
end

function FS.cowsay(env)
  return ( "```\n" .. io.popen('cal | cowsay -n'):read() .. "\n```\n"):gsub('\n',_NR)
end
function FS.ver(env)
  return rime_api.Ver_info(env)
end
function FS.udir(env)
  return rime_api.USER_DIR
end
function FS.sdir(env)
  return rime_api.SHARED_DIR
end
function FS.mem(env)
  return ("%6.3f"):format(collectgarbage('count'))
end


local function commit_text(env, str)
  if type(str) == 'string' and #str > 0 then
    local fs = env:Get_option('full_shape')
    if fs then env:Unset_option('full_shape') end
    env.engine:commit_text( str )
    if fs then env:Set_option('full_shape') end
    return true
  end
end

--execute function
local F={}
function F.reload(env)
  -- reset package.loaded
  package.loaded={}
  for k,v in next, __PKG_LOADED do package.loaded[k] = v end

  if rime_api.Version() < 139 then
    env.engine:process_key(env.keys.reload)
  else
    env.engine:apply_schema(Schema(env.engine.schema.schema_id))
  end
  return 1
end
function F.pgsize(env,size)
  env.engine.context:clear()
  if size and size > 0  and size ConfigList
local function append_component(env, from_path,dist_path)
  local from = from_path and env:Config_get(from_path)
  if not from then return end
  dist_path = dist_path or "engine"

  for sub_path, f_list in pairs(from) do
    local path = dist_path .. "/" ..sub_path
    local dist_clist = List(env:Config_get(path))
    List(f_list):each(function(elm_str)
      dist_clist:append(
        not dist_clist:find(elm_str) and elm_str or nil )
    end)
    env:Config_set(path,dist_clist)
  end
end

-- init_processor

local M={}
function M.init(env)
  Env(env)

  -- init self --
  --local config=env:Config()
  append_component(env, env.name_space .. "/before_modules")
  env.modules = init_modules(env)
  if T01 and GD then GD() end
  append_component(env, env.name_space .. "/after_modules")

  --auto_load_bak(config:get_map("engine"))
  auto_load(env)

  -- prtscr prtkey keyevent
  env.keys= env:get_keybinds()
  env.keys.reload = env.keys.reload and env.keys.reload or KeyEvent('F9')

  -- use key_binder reload
  if rime_api.Version() < 139 then
    local ckeyb= env:Config_get('key_binder/bindings/@0')
    local reload_keyb = {
      ['when']='always',
      accept= env.keys.reload:repr(),
      select= env.engine.schema.schema_id,
    }
    if ckeyb.select ~= reload_keyb.select or
      ckeyb.when ~= reload_keyb.when or
      ckeyb.accept ~= reload_keyb.accept then
      env:Config_set('key_binder/bindings/@before 0', reload_keyb)
    end
  end

  -- init end
  env:Unset_option("_reload")
  env:Unset_option("_menu_size")
  env.opt_update_notifier=env:Context().option_update_notifier:connect(function(ctx,name)
  end)
  env.commit_notifier=env:Context().commit_notifier:connect(function(ctx)
    --local bf=collectgarbage('count')
    --collectgarbage()
    --local ff=collectgarbage('count')
    --Log(DEBUG,'-----mem',bf,ff,bf-ff)
  end)

  do
    ( List()
    + "---- submodules ----"
    + env.modules:map(function(elm) return string.format("%s: %s",elm.name, elm.comp) end)
    + "---- engine components ----"
    + env:components_str()
    + "---- recognize/patterns  ----"
    + List(env:Config_get_with_path("recognizer/patterns")):map(function(elm)
      return elm.path .. ": " .. elm.value end)
    ):each(function(elm,out)  Log(out,elm) end, INFO)
  end
end


function M.fini(env)
  -- modules fini
  env.modules:each(function(elm) elm=nil end)
  env.opt_update_notifier:disconnect()
  env.commit_notifier:disconnect()

  -- self finit --
end


function M.func(key,env)
  local Rejected,Accepted,Noop=0,1,2
  local context=env.engine.context
  local status=env:get_status()

  --Log(DEBUG,'in process------->', env.name_space,key:repr(),context.input)
  local debug_mode = context:get_option('_debug')
  if debug_mode and key:eq(env.keys.prtkey) then
    env:Toggle_option("prtkey")
    return Accepted
  elseif debug_mode and context:get_option("prtkey") then
    env.engine:commit_text(key:repr().. " ")
    return Accepted
  -- commit_text key:repr()
  elseif status.has_menu and key:eq(env.keys.prtscr) then
    commit_text(env,FS["screen_print"](env))
    return Accepted
  end
  -- self func
  -- /ver /modules /comps /cal /cowsay
  local ascii_char = key.keycode >0x20 and key.keycode = 127) and M or Er

Web Proxy Viewer  |  New URL  |  Original Page