怎么写一个Sublime的插件(2)

怎么写一个Sublime的插件(2)

现场直播, 和大家一起写一个Sublime的插件.

写一个什么插件呢?

平时用PlainTasks来细分一天的工作, 但是没有提醒功能.
想找一个定时器类的插件, github上搜到的都不太好用.
所以, 自己写一个.

因为现场直播, 插件可能还没写好.
https://github.com/FloydaGitHub/WorkTime
https://coding.net/u/Floyda/p/WorkTime/git

参考资料

API

需要管理时间, 所以我们做一个"心跳线程"

import sublime, sublime_plugin


class TimeManager():
    def __init__(self):
        self.thread_flag = False
        self.total_time = 0

    # 心跳函数
    def tick(self):
        # 线程开关
        if self.thread_flag is False:
            return

        self.total_time += 1
        sublime.status_message(str(self.total_time)) 
        sublime.set_timeout_async(self.tick, 1000) # 1秒之后, 再次回调自己, 达到心跳的效果.

    def start(self):
        self.thread_flag = True
        sublime.set_timeout_async(self.tick, 0) # 异步延时回调, 启动心跳线程.


time_manager = TimeManager()


class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        time_manager.start()

再次启动插件, F1, 看左下角的状态栏, 一个计时器就做好了:)

做一个番茄时间的计时器

import sublime, sublime_plugin


class TimeManager():
    def __init__(self):
        self.thread_flag = False
        self.total_time = 0

    def tick(self):
        if self.thread_flag is False:
            return

        # if self.total_time >= 60 * 25: # 番茄时间为25分钟.
        if self.total_time >= 3:
            self.stop()
            sublime.message_dialog("Have a rest!") # 弹出一个消息框.
            return

        self.total_time += 1
        sublime.status_message(str(self.total_time)) 
        sublime.set_timeout_async(self.tick, 1000) 

    def start(self):
        self.thread_flag = True
        sublime.set_timeout_async(self.tick, 0) 

    def stop(self):
        self.total_time = 0
        self.thread_flag = False

time_manager = TimeManager()


class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        time_manager.start()

在状态栏显示时间进度

import sublime, sublime_plugin


class TimeManager():
    def __init__(self):
        self.thread_flag = False
        self.total_time = 0
        # self.finish_time = 60 * 25
        self.finish_time = 5

    def tick(self):
        if self.thread_flag is False:
            return

        self.show_status()

        if self.total_time >= self.finish_time:
            self.stop()
            sublime.message_dialog("Have a rest!")
            return

        self.total_time += 1
        # sublime.status_message(str(self.total_time)) 
        sublime.set_timeout_async(self.tick, 1000) 

    def start(self):
        self.thread_flag = True
        sublime.set_timeout_async(self.tick, 0) 

    def stop(self):
        self.total_time = 0
        self.thread_flag = False
        sublime.status_message('')

    def show_status(self):
        progress = int(self.total_time / self.finish_time * 100)
        msg = "|" + \
            progress * "-" + \
            "o" + \
            (100 - progress) * "-" + \
            "|"

        sublime.status_message(msg)

time_manager = TimeManager()


class ExampleCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        time_manager.start()

基本上, 一个番茄时间的插件就做好了.
最好自己动手写一次, 边写边调试.

-- Floyda --

你可能感兴趣的:(怎么写一个Sublime的插件(2))