forked from xjsender/SublimeApex
/
progress.py
129 lines (99 loc) · 3.76 KB
/
progress.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
123
124
125
126
127
128
129
import sublime
from .salesforce import message
from . import util
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, open_console=True):
self.api = api
self.thread = thread
self.message = message
self.success_message = success_message
self.addend = 1
self.size = 15
self.open_console = open_console
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
# Invoked in update.py
if self.api == None: return
# If result is None, it means timeout
if self.api.result == None:
sublime.error_message("Request timeout, please check your network access")
return
# After thread is end, display feedback to end user
# according to response
result = self.api.result
if isinstance(result, dict) and\
(("status_code" in result and result["status_code"] > 399) or\
("success" in result and not result["success"])):
print (message.SEPRATE.format(util.format_error_message(result)))
if self.open_console:
sublime.active_window().run_command("show_panel",
{"panel": "console", "toggle": False})
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