如题。主要是写了一个显示当前时间,距离上次记录多久了的插件。【工作中需要记录某个事项的处理时间】。
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
from datetime import datetime,timedelta
import sublime_plugin
class AddCurrentTimeCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.deltTime = 0
AddCurrentTimeCommand.runTimes = AddCurrentTimeCommand.runTimes + 1
if hasattr(self,'lastTime'):
# 不要用 self.deltTime++ 不行
self.deltTime = datetime.now() - self.lastTime
self.view.run_command("insert_snippet",
{
"contents": "%s" % str(AddCurrentTimeCommand.runTimes)+":"+datetime.now().strftime("%Y-%m-%d %H:%M:%S")+" 距上次记录时间:"+str(self.deltTime)+"\n"
}
)
else:
self.view.run_command("insert_snippet",
{
"contents": "%s" % str(AddCurrentTimeCommand.runTimes)+":"+datetime.now().strftime("%Y-%m-%d %H:%M:%S")+"\n"
}
)
self.lastTime = datetime.now()
# 注意:这个类静态属性在插件中不能放在上面
runTimes = 0
step1,打开sublime的package:
屏幕快照 2019-07-23 下午7.58.20.png
step2,把上面的代码拷贝到文本中,另存到 Packages/User 中,命名为 addCurrentTime.py
step3,Preference → Key Bindings - User
中括号里面增加一个配置项:{
"command": "add_current_time",
"keys": [
"ctrl+shift+."
]
}
py代码要注意的坑:
类静态属性在插件中不能放在上面
如 runTimes = 0
刚开始在run方法前面没法增加属性self.deltTime = 0,我是这样做的:
先在self.view.run_command("insert_snippet",。。。)下面加,然后跑通过后多增加一个self.view.run_command("insert_snippet",。。。),两个都能运行,说明self.deltTime = 0是没有语法错误的,然后再删除上面的self.view.run_command("insert_snippet",。。。)。 于是就可以了!
貌似不支持++语法
AddCurrentTimeCommand.runTimes = AddCurrentTimeCommand.runTimes + 1