-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbrowser_integration.py
119 lines (82 loc) · 2.63 KB
/
browser_integration.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
import sublime
import sublime_plugin
import os
import sys
import threading
import time
sys.path.append(os.path.dirname(__file__))
from .browser import Browser
SETTINGS_FILE = 'BrowserIntegration.sublime-settings'
log_file = '/var/log/sublime-browser-integration.log'
def require_browser(function):
def wrapper(*args, **kwargs):
if not browser.connected():
warning("The browser is not open (or detached).")
return
return function(*args, **kwargs)
return wrapper
def async(function):
def wrapper(*args, **kwargs):
thread = threading.Thread(target=function, args=args, kwargs=kwargs)
try:
thread.start()
return thread
except Exception as e:
with open(log_file, 'a') as f:
f.write(str(e) + '\n')
warning(str(e))
return wrapper
def setting(setting, cmd=None, default=None):
settings = sublime.load_settings(SETTINGS_FILE)
if cmd and hasattr(cmd, 'view'):
view_settings = cmd.view.settings()
else:
view_settings = {}
global_value = settings.get(setting, default)
local_value = view_settings.get(setting, global_value)
return local_value
def status(msg):
msg = "Browser Integration :: %s" % msg
print(msg)
sublime.status_message(msg)
def warning(msg):
msg = "(!) Browser Integration :: %s" % msg
print(msg)
sublime.status_message(msg)
class Loader:
def __init__(self, msg):
self.msg = msg
self.stop = False
def __enter__(self):
self.run()
def __exit__(self, _type, _value, _traceback):
self.stop = True
@async
def run(self):
count = 0
# up = True
chars = "◒◑◓◐"
print("[Start] Browser Integration :: " + self.msg)
while not self.stop:
# load = "[" + " " * count + "=" + " " * (5 - count) + "]"
load = chars[count % 4]
sublime.status_message("Browser Integration :: " + self.msg +
" " + load)
time.sleep(0.1)
count += 1
# if up:
# count += 1
# if count >= 5:
# up = False
# else:
# count -= 1
# if count <= 0:
# up = True
print("[End] Browser Integration :: " + self.msg)
sublime.status_message("")
def loading(msg):
return Loader(msg)
class InsertIntoViewCommand(sublime_plugin.TextCommand):
def run(self, edit, text):
self.view.insert(edit, 0, text)
browser = Browser()