-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathq_menu.py
160 lines (125 loc) · 5.33 KB
/
q_menu.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import sublime, sublime_plugin
from . import QCon as Q
from . import Settings as S
class PerViewCommand(sublime_plugin.WindowCommand):
settings = S.Settings()
def view(self):
return self.window.active_view()
def local(self):
return self.view().settings()
def g(self):
return self.settings
def getCurrentConnection(self):
qcon_dict = self.local().get('q_handle')
if qcon_dict:
return Q.QCon.fromDict(qcon_dict)
return None
def isCurrentConnection(self, qcon):
current = self.getCurrentConnection()
return current and current.equals(qcon)
def deleteConnection(self):
return self.local().set('q_handle', None)
def setConnection(self, qcon):
self.local().set('q_handle', qcon.toDict())
def set_status(self, tag, message, temp=False):
self.view().set_status(tag, message)
if temp:
sublime.set_timeout(lambda:self.set_status(tag, ''), 3000)
def loadQCons(self):
con_dicts = self.settings.get_connections()
self.qcons = []
for con_dict in con_dicts:
self.qcons.append(Q.QCon.fromDict(con_dict))
return self.qcons
class QShowConnectionListCommand(PerViewCommand):
ACTIONS = ['Use', 'Use HDB', 'Update', 'Delete', 'Rename']
def run(self):
self.prompt_connection_list()
def prompt_connection_list(self):
qcons = self.loadQCons()
l = [['Add...', 'New Connection']] #add new connection to first element in the list
#array of dict to array of array format as reqruired by show_quick_panel()
current = self.getCurrentConnection()
for qcon in qcons:
c = ' [Current]' if current and qcon.equals(current) else ''
item = [qcon.name + c, qcon.h()]
l.append(item)
self.window.show_quick_panel(l, self.prompt_actions, 0, 1) #start at index 1 (so user can press up for new)
def prompt_actions(self, i):
if i < 0: #we get index=-1 if cancel
return
elif i == 0: #new connection
self.window.run_command('new_connection')
return
self.qcon = self.qcons[i - 1]
self.window.show_quick_panel(QShowConnectionListCommand.ACTIONS, self.on_done)
def on_done(self, i):
if i < 0: #we get index=-1 if cancel
return
action = QShowConnectionListCommand.ACTIONS[i]
action = action.replace(' ', '_').lower()
print(action + ' ' + str(self.qcon.h()))
self.window.run_command(action + '_connection', {'name': self.qcon.name, 'h': self.qcon.h()})
class NewConnectionCommand(PerViewCommand):
def run(self):
self.prompt_new_connection()
def prompt_new_connection(self):
self.window.show_input_panel('New q Connectionœ', self.settings.default_new_connection(), self.prompt_name, None, None)
def prompt_name(self, h):
self.h = h
self.window.show_input_panel('Connection Name', h, self.on_done, None, self.on_cancel)
def on_cancel(self):
self.on_done('')
def on_done(self, name):
self.window.run_command('use_connection', {'name': name, 'h': self.h})
class CommandWithQCon(PerViewCommand):
def run(self, name, h):
selected_qcon = Q.QCon.new(name, h)
current = self.getCurrentConnection()
if current and current.equals(selected_qcon):
self.current = True
self.qcon = current
else:
self.current = False
self.qcon = selected_qcon
self.do()
def done(self):
if self.current:
self.setConnection(self.qcon)
self.set_status('q', self.getCurrentConnection().status())
class UseConnectionCommand(CommandWithQCon):
def do(self):
self.settings.move_to_top(self.qcon) #this will put use connection on the top of connection list
self.qcon.useHdb(False)
self.setConnection(self.qcon)
self.done()
class UseHdbConnectionCommand(CommandWithQCon):
def do(self):
self.settings.move_to_top(self.qcon) #this will put use connection on the top of connection list
self.qcon.useHdb(True)
self.setConnection(self.qcon)
self.done()
class DeleteConnectionCommand(CommandWithQCon):
def do(self):
self.settings.delete_connection(self.qcon)
self.set_status('result', 'Deleted "' + self.qcon.name + '"', True)
class RenameConnectionCommand(CommandWithQCon):
def do(self):
self.window.show_input_panel('Rename Connection', self.qcon.name, self.on_done, None, None)
def on_done(self, new_name):
new_qcon = self.qcon.clone()
new_qcon.name = new_name
self.settings.update_connection(self.qcon, new_qcon)
self.set_status('result', 'Renamed "' + new_qcon.h() + '" to "' + new_qcon.name + '"', True)
self.qcon = new_qcon
self.done()
class UpdateConnectionCommand(CommandWithQCon):
def do(self):
self.window.show_input_panel('Update Connection', self.qcon.h(), self.on_done, None, None)
def on_done(self, new_h):
new_qcon = self.qcon.clone()
new_qcon.h(new_h)
self.settings.update_connection(self.qcon, new_qcon)
self.set_status('result', 'Updated "' + self.qcon.name + '" to "' + new_qcon.h() + '"', True)
self.qcon = new_qcon
self.done()