This repository was archived by the owner on Oct 31, 2025. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathrime_api_console.lua
More file actions
116 lines (111 loc) · 3.56 KB
/
Copy pathrime_api_console.lua
File metadata and controls
116 lines (111 loc) · 3.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
-- bit width
local BIT = math.maxinteger > 2^31 and 16 or 8
-- session_id format
local PFORMAT = "%0" .. BIT .. "X"
function on_message(obj, session_id, msg_type, msg_value)
local msg = "lua > message: ["..PFORMAT.."] [%s] %s"
print(msg:format(session_id, msg_type, msg_value))
local state_label = rimeac.get_state_label(msg_type, msg_value)
if msg_type == "option" then
local option_name = msg_value:sub(1, 1) == "!" and msg_value:sub(2) or msg_value
local state = rimeac.get_option(option_name) and 1 or 0
if state_label ~= '' and state_label ~= nil then
print(string.format("lua > update option: %s = %d // %s",
option_name, state, state_label))
end
end
end
local function execute_special_command(line)
if line:match("^select schema ") then
print("select schema: ", line:sub(15))
if not rimeac.select_schema(line:sub(15)) then
print("cannot select schema: " .. line:sub(15))
end
return true
elseif line:match("^set option ") then
local value = line:sub(12)[1]~='!'
print("set option ", line:sub(12), value and "on" or "off")
rimeac.set_option(line:sub(12), value)
return true
elseif line:match("^select candidate ") then
print("select candidate ", line:sub(18))
local index = tonumber(line:sub(18))
if not rimeac.select_candidate(index) then
print("cannot select candidate " .. tostring(index))
end
rimeac.print_session()
return true
elseif line:match("^print candidate list") then
local cands = rimeac.get_candidates()
if not cands or #cands == 0 then
print("no candidates")
end
rimeac.print_session()
return true
elseif line:match("^delete on current page ") then
local index = tonumber(line:sub(24))
print("delete on current page", index)
if not rimeac.delete_candidate_on_current_page(index) then
print("failed to delete index " .. line:sub(8) .. " on current page")
end
rimeac.print_session()
return true
elseif line:match("^delete ") then
local index = tonumber(line:sub(8))
print("delete ", index)
if not rimeac.delete_candidate(index) then
print("failed to delete index " .. line:sub(8))
end
rimeac.print_session()
return true
elseif line == "print schema list" then
local list = rimeac.get_schema_id_list()
local names = rimeac.get_schema_name_list()
local schema = rimeac.get_current_schema()
for i = 1, #list do
print(tostring(i) .. " " .. list[i] .. " " .. names[i])
end
print("current schema: " .. schema)
return true
end
return false
end
-------------------------------------------------------------------
local continue = true
-------------------------------------------------------------------
local function main_loop()
rimeac.init_rime()
rimeac.add_session()
while continue do
local input = io.read()
if input == "exit" then
continue = false
break
elseif input == "reload" then
rimeac.destroy_sessions()
rimeac.finalize_rime()
input = ""
main_loop()
end
if input == "synchronize" then
rimeac.synchronize()
rimeac.destroy_sessions()
rimeac.finalize_rime()
input = ""
main_loop()
end
if not execute_special_command(input) then
if rimeac.simulate_keys(input) then
rimeac.print_session()
elseif input and input ~= "" then
print("Error processing key sequence: " .. input)
end
end
end
rimeac.destroy_sessions()
rimeac.finalize_rime()
end
-- setup rime
rimeac.setup_rime("rimeac.lua", "./shared", "./usr", "./log")
-- main loop
main_loop()