在输入法中支持生成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 = flgprint('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 dotable.insert(tb,seed[math.random(1,16)])endlocal 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 = tmpIdid.noPunctuations = string.gsub(tmpId,"-","")id.withUnderline = string.gsub(tmpId,"-","_")return id
end--这是测试函数
function test()local s=0local start_time=os.clock()while s<50000 dos=s+1print(s,guid())endprint('execute_time='..tostring(os.clock()-start_time))
end--Module
function M.init(...)M.guidInfo = guidInfoM.setDbg = setDbgM.test = test
endM.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 = flgprint('sysInfo dbgFlg is '..tostring(dbgFlg))
endM.homePath = function()local tmpVar = os.getenv("HOMEPATH") or os.getenv("HOME")if nil == tmpVar thentmpVar = '未知'endreturn tmpVar
endM.computerName = function()local tmpVar = os.getenv("COMPUTERNAME") or os.getenv("HOSTNAME")if nil == tmpVar thentmpVar = '未知'endreturn tmpVar
endM.userName = function()local tmpVar = os.getenv("USERNAME") or os.getenv("USER")if nil == tmpVar thentmpVar = '未知'endreturn tmpVar
endM.osName = function()local tmpVar = os.getenv("OS")if nil == tmpVar thentmpVar = 'UnixLike'endreturn tmpVar
endM.NUMBER_OF_PROCESSORS = function()local tmpVar = os.getenv("NUMBER_OF_PROCESSORS")if nil == tmpVar thentmpVar = '未知'endreturn tmpVar
endM.pwd = function()local tmpVar = os.getenv("PWD")if nil == tmpVar thentmpVar = '未知'endreturn tmpVar
endM.oldPwd = function()local tmpVar = os.getenv("OLDPWD")if nil == tmpVar thentmpVar = '未知'endreturn tmpVar
endM.PROCESSOR_IDENTIFIER = function()local tmpVar = os.getenv("PROCESSOR_IDENTIFIER")if nil == tmpVar thentmpVar = '未知'endreturn tmpVar
endM.PROGRAMDATA = function()local tmpVar = os.getenv("PROGRAMDATA")if nil == tmpVar thentmpVar = '未知'endreturn tmpVar
endM.PROGRAMFILES = function()local tmpVar = os.getenv("PROGRAMW6432")if nil == tmpVar thentmpVar = '未知'endreturn tmpVar
end
M.PROGRAMFILESx86 = function()local tmpVar = os.getenv("PROGRAMFILES(X86)")if nil == tmpVar thentmpVar = '未知'endreturn tmpVar
endM.APPDATA = function()local tmpVar = os.getenv("APPDATA(X86)")if nil == tmpVar thentmpVar = '未知'endreturn tmpVar
endM.WINDIR = function()local tmpVar = os.getenv("WINDIR")if nil == tmpVar thentmpVar = '未知'endreturn tmpVar
endM.COMMONPROGRAMFILES = function()local tmpVar = os.getenv("COMMONPROGRAMFILES")if nil == tmpVar thentmpVar = '未知'endreturn tmpVar
endM.COMMONPROGRAMFILESx86 = function()local tmpVar = os.getenv("COMMONPROGRAMFILES(x86)")if nil == tmpVar thentmpVar = '未知'endreturn tmpVar
endM.TEMP = function()local tmpVar = os.getenv("TEMP")if nil == tmpVar or '' == tmpVar thentmpVar = os.getenv("TMP")endif nil == tmpVar thentmpVar = '未知'endreturn tmpVar
endM.SYSTEMDRIVE = function()local tmpVar = os.getenv("SYSTEMDRIVE")if nil == tmpVar thentmpVar = '未知'endreturn tmpVar
endM.currentDir = function()local info = debug.getinfo(2) --debug.getinfo(2), 2: 返回调用 currentDir 的函数的信息--解析info.source所在的路径local path = info.sourcepath = string.sub(path, 2, -1) -- 去掉开头的"@"path = string.gsub(path,'\\','/') -- 路径格式由 c:\\Users\\san.zhang\\ 转换为 c:/Users/san.zhang/path = string.match(path, "^(.*)/") -- 捕获最后一个 "/" 之前的部分 就是我们最终要的目录部分return path
endM.writeLog = function(printPrefix)printPrefix = printPrefix or ''if logEnable thenlog.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
endreturn 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,'/') thenpathSpace = '/'
endcurrent_path = current_path:gsub(pathSpace..'$','')local x86x64 = 'x86'
-- 添加 cpath 路径,以使 lua 可以找到 current_path 路径下的 dll 库
if 'Lua 5.4' == _VERSION thenlocal cpath = "\\luaSocket\\x86\\lua5.4\\?.dll" -- 引入 x86 lua54 版 socket.coreif 'x64' == x86x64 thencpath = "\\luaSocket\\x64\\lua5.4\\?.dll" -- 引入 x64 lua54 版 socket.coreendcpath = string.gsub(cpath,'\\',pathSpace) -- 调整路径分隔符package.cpath = package.cpath..';'..current_path..cpath
endlocal socketEnable, socket = pcall(require, "socket.core") -- 加载socket库M.socketEnable = socketEnable
M.socket = socketif not socketEnable thenif logEnable thenlog.writeLog('socketEnable is False')log.writeLog(socket)end
endreturn 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 thensocketEnable = socket.socketEnablesocket = socket.socket
endlocal cands
local theCands
local candTxt_lower
local thisTxt
local thisCommentlocal function Filter(input, env)cands={}for cand in input:iter() do--抛出原选项if nil == cands[cand.text] thenyield(cand)cands[cand.text]=trueendtheCands={}if true thencandTxt_lower = cand.text:lower()if ({['id']=true,['标识']=true})[candTxt_lower] then--添加选项if GUIDEnable thenlocal guidInfo = GUID.guidInfo()table.insert(theCands,{guidInfo.noPunctuations,'💡GUID'})table.insert(theCands,{guidInfo.guid,'💡GUID'})table.insert(theCands,{guidInfo.withUnderline,'💡GUID'})endelseif ({['电脑']=true,['系统']=true,['本机']=true})[candTxt_lower] thenif sysInfoEnable then--添加选项if ({['电脑']=true,['本机']=true})[candTxt_lower] thenlocal computerName = sysInfo.computerName()if '未知' ~= computerName thentable.insert(theCands,{computerName,'💡电脑名'})endlocal cpu = sysInfo.PROCESSOR_IDENTIFIER()if '未知' ~= cpu thentable.insert(theCands,{cpu,'💡CPU'})endlocal coreCnt = sysInfo.NUMBER_OF_PROCESSORS()if '未知' ~= coreCnt thentable.insert(theCands,{coreCnt..'核','💡Core'})endendtable.insert(theCands,{sysInfo.osName(),'💡系统'})endelseif ({['ip']=true})[candTxt_lower] thenif socketEnable then--添加选项local ip = socket.dns.toip(socket.dns.gethostname())if ip thentable.insert(theCands,{ip,'💡ipv4'})endlocal addrinfo = socket.dns.getaddrinfo(socket.dns.gethostname(), nil, {family = "inet6"})if addrinfo thenfor _, info in ipairs(addrinfo) doif info.family == "inet6" thentable.insert(theCands,{info.addr,'💡ipv6'})endendendendelseif ({['用户']=true,['路径']=true})[candTxt_lower] thenif sysInfoEnable then--添加选项if ({['用户']=true})[candTxt_lower] thenlocal userName = sysInfo.userName()if '未知' ~= userName thentable.insert(theCands,{userName,'💡用户名'})endlocal homePath = sysInfo.homePath()if '未知' ~= homePath thentable.insert(theCands,{homePath,'💡用户路径'})endendif ({['路径']=true})[candTxt_lower] thenlocal WINDIR = sysInfo.WINDIR()if '未知' ~= sysInfo.WINDIR() thentable.insert(theCands,{WINDIR,'💡WINDIR'})endlocal homePath = sysInfo.homePath()if '未知' ~= homePath thentable.insert(theCands,{homePath,'💡用户路径'})endlocal pwd = sysInfo.pwd()if '未知' ~= pwd thentable.insert(theCands,{pwd,'💡pwd'})endlocal oldPwd = sysInfo.oldPwd()if '未知' ~= oldPwd thentable.insert(theCands,{oldPwd,'💡oldPwd'})endlocal PROGRAMDATA = sysInfo.PROGRAMDATA()if '未知' ~= PROGRAMDATA thentable.insert(theCands,{PROGRAMDATA,'💡PROGRAMDATA'})endlocal PROGRAMFILES = sysInfo.PROGRAMFILES()if '未知' ~= PROGRAMFILES thentable.insert(theCands,{PROGRAMFILES,'💡PROGRAMFILES'})endlocal PROGRAMFILESx86 = sysInfo.PROGRAMFILESx86()if '未知' ~= PROGRAMFILESx86 thentable.insert(theCands,{PROGRAMFILESx86,'💡PROGRAMFILES(x86)'})endlocal COMMONPROGRAMFILESx86 = sysInfo.COMMONPROGRAMFILESx86()if '未知' ~= COMMONPROGRAMFILESx86 thentable.insert(theCands,{COMMONPROGRAMFILESx86,'💡COMMONPROGRAMFILES(x86)'})endlocal TEMP = sysInfo.TEMP()if '未知' ~= TEMP thentable.insert(theCands,{TEMP,'💡TEMP'})endlocal APPDATA = sysInfo.APPDATA()if '未知' ~= APPDATA thentable.insert(theCands,{APPDATA,'💡APPDATA'})endendendelseif ({['随机']=true, ['密码']=true})[candTxt_lower] thenif '随机' == candTxt_lower then--添加选项,返回一个随机数table.insert(theCands,{math.random(),'💡0-1伪随机数'})endif utf8StrEnable thentable.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位随机密码'})endendend--抛出选项for idx = 1, #theCands dothisTxt = theCands[idx][1]thisComment = theCands[idx][2]if nil ~= thisTxt and '' ~= thisTxt thenif nil == cands[thisTxt] thenyield(Candidate("word", cand.start, cand._end, thisTxt, thisComment))cands[thisTxt] = trueendendendend
endreturn 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地址等。