-
Notifications
You must be signed in to change notification settings - Fork 0
/
threadTrack.py
122 lines (93 loc) · 3.48 KB
/
threadTrack.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import sublime
from .salesforce import message
from . import util
from . import context
from .salesforce.lib.panel import Printer
class ThreadProgress():
"""
Stolen from Package Control Source Code, ni dong de
Animates an indicator, [= ], in the status area while a thread runs
:param thread:
The thread to track for activity
:param message:
The message to display next to the activity indicator
:param success_message:
The message to display once the thread is complete
"""
def __init__(self, api, thread, message, success_message, show_error=True):
self.api = api
self.thread = thread
self.message = message
self.success_message = success_message
self.addend = 1
self.size = 15
self.show_error = show_error
sublime.set_timeout(lambda: self.run(0), 100)
def run(self, i):
if not self.thread.is_alive():
if hasattr(self.thread, 'result') and not self.thread.result:
sublime.status_message('')
return
# After thread is end, display feedback to end user
# according to response
result = self.api.result
if isinstance(result, dict) and "Error Message" in result:
Printer.get('error').write(result["Error Message"])
elif isinstance(result, dict) and "success" in result and not result["success"]:
if not self.show_error: return
Printer.get('error').write(message.SEPRATE.format(util.format_error_message(result)))
else:
sublime.status_message(self.success_message)
return
before = i % self.size
after = (self.size - 1) - before
sublime.status_message('%s [%s=%s]' % \
(self.message, ' ' * before, ' ' * after))
if not after:
self.addend = -1
if not before:
self.addend = 1
i += self.addend
sublime.set_timeout(lambda: self.run(i), 100)
class ThreadsProgress():
"""
Stolen from Package Control Source Code, ni dong de
Animates an indicator, [= ], in the status area while a thread runs
:param threads:
The threads to track for activity
:param message:
The message to display next to the activity indicator
:param success_message:
The message to display once the thread is complete
"""
def __init__(self, threads, message, success_message):
self.threads = threads
self.message = message
self.success_message = success_message
self.addend = 1
self.size = 15
sublime.set_timeout(lambda: self.run(0), 100)
def run(self, i):
if self.is_threads_end(self.threads):
sublime.status_message(self.success_message)
return
before = i % self.size
after = (self.size - 1) - before
sublime.status_message('%s [%s=%s]' % \
(self.message, ' ' * before, ' ' * after))
if not after:
self.addend = -1
if not before:
self.addend = 1
i += self.addend
sublime.set_timeout(lambda: self.run(i), 100)
def is_threads_end(self, threads):
"""
Indicate whether all threads are end
@threads: Threads
@return: Bool
"""
is_alive = True
for thread in threads:
if thread.is_alive(): is_alive = False
return is_alive