RIME输入法获取当前时间一

Rime输入法通过定义lua文件,可以实现获取当前时间日期的功能。

1.TIME

Rime是一款可以高度自定义的输入法,相关教程可以查看往期文章,关于时间获取是指输入一个指定关键字,输出当前时间,效果如下(我定义了time关键字):

实现如下:

①在用户文件夹中新建一个rime.lua文件加入如下代码

 time_translator = require("time")

②在用户文件夹下新建文件夹Lua,并新建文件time.lua(此处的文件名必需与上文require中的内容相同),加入如下lua代码:

 --lua语言中的注释用“--”
 local function translator(input, seg)
    if (input == "time") then         --关键字更改,你也可以用or语句定义多个关键字
       yield(Candidate("time", seg.start, seg._end, os.date("%H:%M"), " "))
       yield(Candidate("time", seg.start, seg._end, os.date("%H点%M分"), " "))
       yield(Candidate("time", seg.start, seg._end, os.date("%H:%M:%S"), " "))
       yield(Candidate("time", seg.start, seg._end, os.date("%H点%M分%S秒"), " "))
    end
 end
 return translator

此处代码用lua语言编写而成,若有一定语言基础,根据自己的需求调整响应代码。

③在用户配置文件中开启(*.custom.yaml文件)

 #若你的配置文件中有engine/translator,则后面直接加入代码
     - lua_translator@time_translator
 #若你的配置文件中无engine/translator项,则加入
   engine/+:
     translators/+:
       - lua_translator@time_translator
 #此处@后面的名称必需与rime.lua文件中的定义名称一致

2.DATE

与time类似,只需修改lua文件中的实现代码即可,我定义的关键字是date,效果如下:

实现如下:①在rime.lua文件加入如下代码

 date_translator = require("date")

②在文件夹Lua中新建文件date.lua(此处的文件名必需与上文require中的内容相同),加入如下lua代码:

 --lua语言中的注释用“--”
 local function translator(input, seg)
    if (input == "date") then
 ------------------------------------------------------------------------------------
 --普通日期1,类似2022年01月02日
 date1=os.date("%Y年%m月%d日")
 date_y=os.date("%Y") --取年
 date_m=os.date("%m") --取月
 date_d=os.date("%d") --取日
 --yield(Candidate("date", seg.start, seg._end, date1, " "))
 ------------------------------------------------------------------------------------
 --普通日期2,类似2022年1月1日
 num_m=os.date("%m")+0
 num_m1=math.modf(num_m)
 num_d=os.date("%d")+0
 num_d1=math.modf(num_d)
 date2=os.date("%Y年")..tostring(num_m1).."月"..tostring(num_d1).."日"
 yield(Candidate("date", seg.start, seg._end, date2, " "))
 ------------------------------------------------------------------------------------
 --普通日期3,类似1月1日
 num_m=os.date("%m")+0
 num_m1=math.modf(num_m)
 num_d=os.date("%d")+0
 num_d1=math.modf(num_d)
 date3=tostring(num_m1).."月"..tostring(num_d1).."日"
 yield(Candidate("date", seg.start, seg._end, date3, " "))
 yield(Candidate("date", seg.start, seg._end, os.date("%Y/%m/%d"), " "))
 yield(Candidate("date", seg.start, seg._end, os.date("%Y-%m-%d"), " "))
 ------------------------------------------------------------------------------------
 --大写日期,类似二〇二〇年十一月二十六日
 date_y=date_y:gsub("%d",{
 ["1"]="一",
 ["2"]="二",
 ["3"]="三",
 ["4"]="四",
 ["5"]="五",
 ["6"]="六",
 ["7"]="七",
 ["8"]="八",
 ["9"]="九",
 ["0"]="〇",
 })
 date_y=date_y.."年"
 date_m=date_m:gsub("%d",{
 ["1"]="一",
 ["2"]="二",
 ["3"]="三",
 ["4"]="四",
 ["5"]="五",
 ["6"]="六",
 ["7"]="七",
 ["8"]="八",
 ["9"]="九",
 ["0"]="",
 })
 date_m=date_m.."月"
 if num_m1==10 then date_m="十月" end
 if num_m1==11 then date_m="十一月" end
 if num_m1==12 then date_m="十二月" end
 date_d=date_d:gsub("%d",{
 ["1"]="一",
 ["2"]="二",
 ["3"]="三",
 ["4"]="四",
 ["5"]="五",
 ["6"]="六",
 ["7"]="七",
 ["8"]="八",
 ["9"]="九",
 ["0"]="",
 })
 date_d=date_d.."日"
 if num_d1>9 then
 if num_d1<19 then
 date_d="十"..string.sub(date_d,4,#date_d)
 end
 end
 if num_d1>19 then date_d=string.sub(date_d,1,3).."十"..string.sub(date_d,4,#date_d) end
 date4=date_y..date_m..date_d
 yield(Candidate("date", seg.start, seg._end, date4, " "))
 ------------------------------------------------------------------------------------
 --英文日期
     local date_d=os.date("%d")
     local date_m=os.date("%m")
     local date_y=os.date("%Y")
     local date_m1=""
     local date_m2=""
     if date_m=="01" then 
        date_m1="Jan."
        date_m2="January"
     end
     if date_m=="02" then 
        date_m1="Feb."
        date_m2="February"
     end
     if date_m=="03" then 
        date_m1="Mar."
        date_m2="March"
     end
     if date_m=="04" then 
        date_m1="Apr."
        date_m2="April"
     end
     if date_m=="05" then 
        date_m1="May."
        date_m2="May"
     end
     if date_m=="06" then 
        date_m1="Jun."
        date_m2="June"
     end
     if date_m=="07" then 
        date_m1="Jul."
        date_m2="July"
     end
     if date_m=="08" then 
        date_m1="Aug."
        date_m2="August"
     end
     if date_m=="09" then 
        date_m1="Sept."
        date_m2="September"
     end
     if date_m=="10" then 
        date_m1="Oct."
        date_m2="October"
     end
     if date_m=="11" then 
        date_m1="Nov."
        date_m2="November"
     end
     if date_m=="12" then 
        date_m1="Dec."
        date_m2="December"
     end
 ​
      if date_d=="0" then 
        symbal="st" 
      elseif date_d=="1" then
        symbal="nd" 
      elseif date_d=="2" then 
        symbal="rd" 
      else
        symbal="th"
      end
 date5=date_m1.." "..date_d..symbal..", "..date_y
 date6=date_m2.." "..date_d..symbal..", "..date_y
 ​
 yield(Candidate("date", seg.start, seg._end, date5, " "))
 yield(Candidate("date", seg.start, seg._end, date6, " "))
 yield(Candidate("date", seg.start, seg._end, os.date("%Y%m%d"), " "))
    end
 end
 ------------------------------------------------------------------------------------
 return translator

③在用户配置文件中开启(*.custom.yaml文件)

 #若你的配置文件中有engine/translator,则后面直接加入代码
     - lua_translator@date_translator
 #若你的配置文件中无engine/translator项,则加入
   engine/+:
     translators/+:
       - lua_translator@date_translator
 #此处@后面的名称必需与rime.lua文件中的定义名称一致

3.WEEK

与time类似,只需修改lua文件中的实现代码即可,我定义的关键字是week,效果如下:

实现如下:①在rime.lua文件加入如下代码

 week_translator = require("week")

②在

文件夹Lua中新建文件date.lua(此处的文件名必需与上文require中的内容相同),加入如下lua代码:

 --lua语言中的注释用“--”
 function translator(input, seg)
   if (input == "week") then
     local day_w=os.date("%w")
     local day_w1=""
     local day_w2=""
     local day_w3=""
     if day_w=="0" then 
       day_w1="星期日" 
       day_w2="Sunday" 
       day_w3="Sun." 
     end
     if day_w=="1" then
       day_w1="星期一" 
       day_w2="Monday" 
       day_w3="Mon." 
     end
     if day_w=="2" then
       day_w1="星期二" 
       day_w2="Tuesday" 
       day_w3="Tues." 
     end
     if day_w=="3" then 
       day_w1="星期三" 
       day_w2="Wednesday" 
       day_w3="Wed." 
     end
     if day_w=="4" then 
       day_w1="星期四" 
       day_w2="Thursday" 
       day_w3="Thur." 
     end
     if day_w=="5" then 
       day_w1="星期五"  
       day_w2="Friday" 
       day_w3="Fri." 
     end
     if day_w=="6" then 
       day_w1="星期六" 
       day_w2="Saturday" 
       day_w3="Sat." 
     end
     yield(Candidate("date", seg.start, seg._end, day_w1, " "))
     yield(Candidate("date", seg.start, seg._end, day_w2, " "))
     yield(Candidate("date", seg.start, seg._end, day_w3, " "))
     yield(Candidate("week", seg.start, seg._end, os.date("%w"),""))
   end
 end
 return translator

③在用户配置文件中开启(*.custom.yaml文件)

#若你的配置文件中有engine/translator,则后面直接加入代码
    - lua_translator@week_translator
#若你的配置文件中无engine/translator项,则加入
  engine/+:
    translators/+:
      - lua_translator@week_translator
#此处@后面的名称必需与rime.lua文件中的定义名称一致

以上配置文件的合集打包可在公众号获取。

参考:感谢Github项目网址作者的方案。

以上就是今天的内容,这篇文章能帮到你是我写作的意义所在,有任何问题可以评论或私信。当然,您的点赞与关注也是对我最大的支持与肯定,也是我继续写作的动力源泉。

了解更多教程可关注公众号

相关文章

1.github代码实践源代码是lua脚本语言,下载th之后运行thmai...
此文为搬运帖,原帖地址https://www.cnblogs.com/zwywilliam/...
Rime输入法通过定义lua文件,可以实现获取当前时间日期的功能...
localfunctiongenerate_action(params)localscale_action=cc...
2022年1月11日13:57:45 官方:https://opm.openresty.org/官...
在Lua中的table(表),就像c#中的HashMap(哈希表),key和...