在输入法中支持生成GUID,或者随机数,随机字符,获取自身电脑信息,这将是一个非常酷的功能。
本文所分享滤镜,主要用于生成一些动态的信息词条,效果如下👇:
GUID.lua
GUID.lua
脚本用于生成 GUID
字符串,GUID.lua
脚本如下👇:
--[[
Descripttion:
version:
Author: douyaoyuan
Date: 2023-06-01 08:48:23
LastEditors: douyaoyuan
LastEditTime: 2023-06-01 11:12:53
--]]
--GUID.lua
--这个模块主要用于处理一些utf8字符串相关的操作
local M={}
local dbgFlg = false
--设置 dbg 开关
local function setDbg(flg)
dbgFlg = flg
print('GUID dbgFlg is '..tostring(dbgFlg))
end
--返回一个 GUID 字符串
function guid()
local seed={'e','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}
local tb={}
for i=1,32 do
table.insert(tb,seed[math.random(1,16)])
end
local sid=table.concat(tb)
return string.format('%s-%s-%s-%s-%s',
string.sub(sid,1,8),
string.sub(sid,9,12),
string.sub(sid,13,16),
string.sub(sid,17,20),
string.sub(sid,21,32)
)
end
--[[返回一个 guidInfo 结构,结构体如下:
guidInfo.guid:正常的GUID
guidInfo.noPunctuations:只包含字母和数字的GUID
guidInfo.withUnderline:分隔符是下划线的 guid
]]
function guidInfo()
local id = {}
local tmpId = guid()
id.guid = tmpId
id.noPunctuations = string.gsub(tmpId,"-","")
id.withUnderline = string.gsub(tmpId,"-","_")
return id
end
--这是测试函数
function test()
local s=0
local start_time=os.clock()
while s<50000 do
s=s+1
print(s,guid())
end
print('execute_time='..tostring(os.clock()-start_time))
end
--Module
function M.init(...)
M.guidInfo = guidInfo
M.setDbg = setDbg
M.test = test
end
M.init()
return M
sysInfo.lua
sysInfo.lua
脚本用于获取系统相关的信息,例如用户名,例如电脑名,例如环境变量中的一些路径信息等。sysInfo.lua
脚本如下👇:
-- 导入log模块记录日志
local logEnable, log = pcall(require, "runLog")
local M={}
local dbgFlg = false
--设置 dbg 开关
M.setDbg = function(flg)
dbgFlg = flg
print('sysInfo dbgFlg is '..tostring(dbgFlg))
end
M.homePath = function()
local tmpVar = os.getenv("HOMEPATH") or os.getenv("HOME")
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.computerName = function()
local tmpVar = os.getenv("COMPUTERNAME") or os.getenv("HOSTNAME")
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.userName = function()
local tmpVar = os.getenv("USERNAME") or os.getenv("USER")
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.osName = function()
local tmpVar = os.getenv("OS")
if nil == tmpVar then
tmpVar = 'UnixLike'
end
return tmpVar
end
M.NUMBER_OF_PROCESSORS = function()
local tmpVar = os.getenv("NUMBER_OF_PROCESSORS")
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.pwd = function()
local tmpVar = os.getenv("PWD")
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.oldPwd = function()
local tmpVar = os.getenv("OLDPWD")
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.PROCESSOR_IDENTIFIER = function()
local tmpVar = os.getenv("PROCESSOR_IDENTIFIER")
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.PROGRAMDATA = function()
local tmpVar = os.getenv("PROGRAMDATA")
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.PROGRAMFILES = function()
local tmpVar = os.getenv("PROGRAMW6432")
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.PROGRAMFILESx86 = function()
local tmpVar = os.getenv("PROGRAMFILES(X86)")
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.APPDATA = function()
local tmpVar = os.getenv("APPDATA(X86)")
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.WINDIR = function()
local tmpVar = os.getenv("WINDIR")
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.COMMONPROGRAMFILES = function()
local tmpVar = os.getenv("COMMONPROGRAMFILES")
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.COMMONPROGRAMFILESx86 = function()
local tmpVar = os.getenv("COMMONPROGRAMFILES(x86)")
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.TEMP = function()
local tmpVar = os.getenv("TEMP")
if nil == tmpVar or '' == tmpVar then
tmpVar = os.getenv("TMP")
end
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.SYSTEMDRIVE = function()
local tmpVar = os.getenv("SYSTEMDRIVE")
if nil == tmpVar then
tmpVar = '未知'
end
return tmpVar
end
M.currentDir = function()
local info = debug.getinfo(2) --debug.getinfo(2), 2: 返回调用 currentDir 的函数的信息
--解析info.source所在的路径
local path = info.source
path = string.sub(path, 2, -1) -- 去掉开头的"@"
path = string.gsub(path,'\\','/') -- 路径格式由 c:\\Users\\san.zhang\\ 转换为 c:/Users/san.zhang/
path = string.match(path, "^(.*)/") -- 捕获最后一个 "/" 之前的部分 就是我们最终要的目录部分
return path
end
M.writeLog = function(printPrefix)
printPrefix = printPrefix or ''
if logEnable then
log.writeLog(printPrefix..'homePath: '..M.homePath())
log.writeLog(printPrefix..'computerName: '..M.computerName())
log.writeLog(printPrefix..'userName: '..M.userName())
log.writeLog(printPrefix..'osName: '..M.osName())
log.writeLog(printPrefix..'pwd: '..M.pwd())
log.writeLog(printPrefix..'oldPwd: '..M.oldPwd())
log.writeLog(printPrefix..'numberOfProcessors: '..M.NUMBER_OF_PROCESSORS())
log.writeLog(printPrefix..'progressorIdentifier: '..M.PROCESSOR_IDENTIFIER())
log.writeLog(printPrefix..'programData: '..M.PROGRAMDATA())
log.writeLog(printPrefix..'programFiles: '..M.PROGRAMFILES())
log.writeLog(printPrefix..'programFilesx86: '..M.PROGRAMFILESx86())
log.writeLog(printPrefix..'appData: '..M.APPDATA())
log.writeLog(printPrefix..'winDir: '..M.WINDIR())
log.writeLog(printPrefix..'commonProgramFiles: '..M.COMMONPROGRAMFILES())
log.writeLog(printPrefix..'commonProgramFilesx86: '..M.COMMONPROGRAMFILESx86())
log.writeLog(printPrefix..'temp: '..M.TEMP())
log.writeLog(printPrefix..'systemDrive: '..M.SYSTEMDRIVE())
log.writeLog(printPrefix..'currentDir: '..M.currentDir())
end
end
return M
socket.lua
socket.lua
脚本主要用于加载同路径下的socket.core
程序,为socket
编程提供支持。socket.lua
脚本如下👇:
-- socket.lua
-- Copyright (C) 2023 yaoyuan.dou <douyaoyuan@126.com>
-- 这个模块仅用于导入 socket/core.dll 模块
-- 要求本 socket.lua 文件与 socket 文件夹位于同一路径下,core.dll 文件位于socket文件夹内
-- 使用时请注意 socket/core.dll 的版本 lua程序的版本是否一致
-- 使用时请注意 x86 与 x64 的区别
-- 导入log模块记录日志
local logEnable, log = pcall(require, "runLog")
-- 定义一个模块
local M = {}
-- 获取本 socket.lua文件的路径
local current_path = string.sub(debug.getinfo(1).source, 2, string.len("/socket.lua") * -1)
local pathSpace = "\\"
if string.find(current_path,'/') then
pathSpace = '/'
end
current_path = current_path:gsub(pathSpace..'$','')
local x86x64 = 'x86'
-- 添加 cpath 路径,以使 lua 可以找到 current_path 路径下的 dll 库
if 'Lua 5.4' == _VERSION then
local cpath = "\\luaSocket\\x86\\lua5.4\\?.dll" -- 引入 x86 lua54 版 socket.core
if 'x64' == x86x64 then
cpath = "\\luaSocket\\x64\\lua5.4\\?.dll" -- 引入 x64 lua54 版 socket.core
end
cpath = string.gsub(cpath,'\\',pathSpace) -- 调整路径分隔符
package.cpath = package.cpath..';'..current_path..cpath
end
local socketEnable, socket = pcall(require, "socket.core") -- 加载socket库
M.socketEnable = socketEnable
M.socket = socket
if not socketEnable then
if logEnable then
log.writeLog('socketEnable is False')
log.writeLog(socket)
end
end
return M
dynamicPhrase_Filter.lua
dynamicPhrase_Filter.lua
脚本将以上 GUID.lua
,sysInfo.lua
,socket.lua
所提供的信息进行了整合,统一成 dynamicPhrase_Filter
滤镜方法。dynamicPhrase_Filter.lua
脚本如下👇:
--[[
Descripttion:
version:
Author: douyaoyuan
Date: 2023-06-01 08:48:23
LastEditors: douyaoyuan
LastEditTime: 2023-06-09 13:27:57
--]]
--dynamicPhrase_Filter.lua
--Copyright (C) 2023 yaoyuan.dou <douyaoyuan@126.com>
--本脚本主要用于提供一些与运行环境有关的词条信息
--引入支持模块,处理日期和时间信息
local GUIDEnable, GUID = pcall(require, 'GUID')
local sysInfoEnable, sysInfo = pcall(require, 'sysInfo')
local socketEnable, socket = pcall(require, "socket")
local logEnable, log = pcall(require, "runLog")
local utf8StrEnable, utf8Str = pcall(require, 'utf8String')
if socketEnable then
socketEnable = socket.socketEnable
socket = socket.socket
end
local cands
local theCands
local candTxt_lower
local thisTxt
local thisComment
local function Filter(input, env)
cands={}
for cand in input:iter() do
--抛出原选项
if nil == cands[cand.text] then
yield(cand)
cands[cand.text]=true
end
theCands={}
if true then
candTxt_lower = cand.text:lower()
if ({['id']=true,['标识']=true})[candTxt_lower] then
--添加选项
if GUIDEnable then
local guidInfo = GUID.guidInfo()
table.insert(theCands,{guidInfo.noPunctuations,'💡GUID'})
table.insert(theCands,{guidInfo.guid,'💡GUID'})
table.insert(theCands,{guidInfo.withUnderline,'💡GUID'})
end
elseif ({['电脑']=true,['系统']=true,['本机']=true})[candTxt_lower] then
if sysInfoEnable then
--添加选项
if ({['电脑']=true,['本机']=true})[candTxt_lower] then
local computerName = sysInfo.computerName()
if '未知' ~= computerName then
table.insert(theCands,{computerName,'💡电脑名'})
end
local cpu = sysInfo.PROCESSOR_IDENTIFIER()
if '未知' ~= cpu then
table.insert(theCands,{cpu,'💡CPU'})
end
local coreCnt = sysInfo.NUMBER_OF_PROCESSORS()
if '未知' ~= coreCnt then
table.insert(theCands,{coreCnt..'核','💡Core'})
end
end
table.insert(theCands,{sysInfo.osName(),'💡系统'})
end
elseif ({['ip']=true})[candTxt_lower] then
if socketEnable then
--添加选项
local ip = socket.dns.toip(socket.dns.gethostname())
if ip then
table.insert(theCands,{ip,'💡ipv4'})
end
local addrinfo = socket.dns.getaddrinfo(socket.dns.gethostname(), nil, {family = "inet6"})
if addrinfo then
for _, info in ipairs(addrinfo) do
if info.family == "inet6" then
table.insert(theCands,{info.addr,'💡ipv6'})
end
end
end
end
elseif ({['用户']=true,['路径']=true})[candTxt_lower] then
if sysInfoEnable then
--添加选项
if ({['用户']=true})[candTxt_lower] then
local userName = sysInfo.userName()
if '未知' ~= userName then
table.insert(theCands,{userName,'💡用户名'})
end
local homePath = sysInfo.homePath()
if '未知' ~= homePath then
table.insert(theCands,{homePath,'💡用户路径'})
end
end
if ({['路径']=true})[candTxt_lower] then
local WINDIR = sysInfo.WINDIR()
if '未知' ~= sysInfo.WINDIR() then
table.insert(theCands,{WINDIR,'💡WINDIR'})
end
local homePath = sysInfo.homePath()
if '未知' ~= homePath then
table.insert(theCands,{homePath,'💡用户路径'})
end
local pwd = sysInfo.pwd()
if '未知' ~= pwd then
table.insert(theCands,{pwd,'💡pwd'})
end
local oldPwd = sysInfo.oldPwd()
if '未知' ~= oldPwd then
table.insert(theCands,{oldPwd,'💡oldPwd'})
end
local PROGRAMDATA = sysInfo.PROGRAMDATA()
if '未知' ~= PROGRAMDATA then
table.insert(theCands,{PROGRAMDATA,'💡PROGRAMDATA'})
end
local PROGRAMFILES = sysInfo.PROGRAMFILES()
if '未知' ~= PROGRAMFILES then
table.insert(theCands,{PROGRAMFILES,'💡PROGRAMFILES'})
end
local PROGRAMFILESx86 = sysInfo.PROGRAMFILESx86()
if '未知' ~= PROGRAMFILESx86 then
table.insert(theCands,{PROGRAMFILESx86,'💡PROGRAMFILES(x86)'})
end
local COMMONPROGRAMFILESx86 = sysInfo.COMMONPROGRAMFILESx86()
if '未知' ~= COMMONPROGRAMFILESx86 then
table.insert(theCands,{COMMONPROGRAMFILESx86,'💡COMMONPROGRAMFILES(x86)'})
end
local TEMP = sysInfo.TEMP()
if '未知' ~= TEMP then
table.insert(theCands,{TEMP,'💡TEMP'})
end
local APPDATA = sysInfo.APPDATA()
if '未知' ~= APPDATA then
table.insert(theCands,{APPDATA,'💡APPDATA'})
end
end
end
elseif ({['随机']=true, ['密码']=true})[candTxt_lower] then
if '随机' == candTxt_lower then
--添加选项,返回一个随机数
table.insert(theCands,{math.random(),'💡0-1伪随机数'})
end
if utf8StrEnable then
table.insert(theCands, {utf8Str.newPwd(6), '💡6位随机密码'})
table.insert(theCands, {utf8Str.newPwd(8), '💡8位随机密码'})
table.insert(theCands, {utf8Str.newPwd(10, false), '💡10位随机密码'})
table.insert(theCands, {utf8Str.newPwd(14), '💡14位随机密码'})
table.insert(theCands, {utf8Str.newPwd(16), '💡16位随机密码'})
table.insert(theCands, {utf8Str.newPwd(18), '💡18位随机密码'})
end
end
end
--抛出选项
for idx = 1, #theCands do
thisTxt = theCands[idx][1]
thisComment = theCands[idx][2]
if nil ~= thisTxt and '' ~= thisTxt then
if nil == cands[thisTxt] then
yield(Candidate("word", cand.start, cand._end, thisTxt, thisComment))
cands[thisTxt] = true
end
end
end
end
end
return Filter
💣注意:
👆以上所述 dynamicPhrase_Filter.lua
、GUID.lua
、sysInfo.lua
、socket.lua
四个脚本文档,应该位于 用户文件夹 下的 lua 文件夹内。如下👇:
rime.lua
在 rime.lua
脚本中,我们增加以下配置/代码👇:
help_translator = require("help")
inputShow_translator = require("inputShow")
inputShow_Filter = require("inputShow_Filter")
Upper_Filter = require("Upper_Filter")
dic_4w_Filter = require("dic_4w_Filter")
phraseReplace_Filter = require("phraseReplace_Filter")
pinyinAdding_Filter = require("pinyinAdding_Filter")
dateTime_Filter = require("dateTime_filter")
dynamicPhrase_Filter = require("dynamicPhrase_Filter")
注意最后一行中我们增加了 dynamicPhrase_Filter
滤镜。
wubi_pinyin.custom.yaml
此文以 五笔?拼音 输入方案为例,展示如何配置使用 dynamicPhrase_Filter
滤镜。
在 五笔?拼音 输入方案的方案文档 wubi_pinyin.schema.yaml
的补丁文档 wubi_pinyin.custom.yaml
中,我们增加以下滤镜配置👇:
# encoding:utf-8
patch:
- lua_filter@dynamicPhrase_Filter # 动态候选项,用于动态获取与运行环境有关的信息
👆以上配置并没有完全展示 wubi_pinyin.custom.yaml
中的所有配置项,此处仅展示了与 dynamicPhrase_Filter
滤镜有关的一项配置。
👆上述所有的lua
脚本与yaml
配置文档,你也可以从 rime中州韵小狼毫须鼠管 动态信息滤镜 配置.zip 下载取用。
如果你可以访问gitHub
,你也可以在 dyyRime 中找到完全版本的配置包。
本文分享了一种在rime
中州韵小狼毫须鼠管输入法中配置动态信息的滤镜,滤镜支持生成GUID字符串,支持生成密码密码字符串,支持获取系统信息(例如用户名,系统名等),支持获取常用路径,支持获取本机ip地址等。