forked from SublimeLinter/SublimeLinter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbusy_indicator_view.py
104 lines (78 loc) · 2.51 KB
/
busy_indicator_view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import sublime
import sublime_plugin
import time
from .lint import events, util
MYPY = False
if MYPY:
from typing import Dict, Optional
from mypy_extensions import TypedDict
FileName = str
LinterName = str
State_ = TypedDict('State_', {
'active_view': Optional[sublime.View],
'running': Dict[FileName, float],
})
INITIAL_DELAY = 2
CYCLE_TIME = 200
TIMEOUT = 20
STATUS_BUSY_KEY = "sublime_linter_status_busy"
State = {
'active_view': None,
'running': {}
} # type: State_
def plugin_loaded():
active_view = sublime.active_window().active_view()
if util.is_lintable(active_view):
State.update({
'active_view': active_view
})
def plugin_unloaded():
events.off(on_begin_linting)
events.off(on_finished_linting)
for window in sublime.windows():
for view in window.views():
view.erase_status(STATUS_BUSY_KEY)
@events.on(events.LINT_START)
def on_begin_linting(filename):
# type: (FileName) -> None
State['running'][filename] = time.time()
active_view = State['active_view']
if active_view and util.canonical_filename(active_view) == filename:
sublime.set_timeout_async(
lambda: draw(active_view, filename), # type: ignore # mypy bug
INITIAL_DELAY * 1000
)
@events.on(events.LINT_END)
def on_finished_linting(filename):
# type: (FileName) -> None
State['running'].pop(filename, None)
active_view = State['active_view']
if active_view and util.canonical_filename(active_view) == filename:
draw(active_view, filename)
class UpdateState(sublime_plugin.EventListener):
def on_activated(self, active_view):
# type: (sublime.View) -> None
if not util.is_lintable(active_view):
return
State.update({
'active_view': active_view
})
draw(active_view, util.canonical_filename(active_view))
indicators = [
'Linting. ',
'Linting.. ',
'Linting. .',
'Linting ..',
'Linting .',
]
def draw(view, filename):
# type: (sublime.View, FileName) -> None
start_time = State['running'].get(filename, None)
now = time.time()
if start_time and (INITIAL_DELAY <= (now - start_time) < TIMEOUT):
num = len(indicators)
text = indicators[int((now - start_time) * 1000 / CYCLE_TIME) % num]
view.set_status(STATUS_BUSY_KEY, text)
sublime.set_timeout_async(lambda: draw(view, filename), CYCLE_TIME)
else:
view.erase_status(STATUS_BUSY_KEY)