-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsshgo3.py
executable file
·395 lines (324 loc) · 12.7 KB
/
sshgo3.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os,sys,re
import curses
import locale
import math
import traceback
from optparse import OptionParser
locale.setlocale(locale.LC_ALL, 'en_US')
def _assert(exp, err):
if not exp:
print(err, file=sys.stderr)
sys.exit(1)
class SSHGO:
UP = -1
DOWN = 1
KEY_O = 79
KEY_R = 82
KEY_G = 71
KEY_o = 111
KEY_r = 114
KEY_g = 103
KEY_c = 99
KEY_C = 67
KEY_m = 109
KEY_M = 77
KEY_d = 0x64
KEY_u = 0x75
KEY_SPACE = 32
KEY_ENTER = 10
KEY_q = 113
KEY_ESC = 27
KEY_j = 106
KEY_k = 107
KEY_SPLASH = 47
screen = None
def _parse_tree_from_config_file(self, config_file):
tree = {'line_number':None,'expanded':True,'line':None,'sub_lines':[]}
def find_parent_line(new_node):
line_number = new_node['line_number']
level = new_node['level']
if level == 0:
return tree
stack = tree['sub_lines'] + []
parent = None
while len(stack):
node = stack.pop()
if node['line_number'] < line_number and node['level'] == level - 1:
if parent is None:
parent = node
elif node['line_number'] > parent['line_number']:
parent = node
if len(node['sub_lines']) and node['level'] < level:
stack = stack + node['sub_lines']
continue
return parent
tree_level = None
nodes_pool = []
line_number = 0;
for line in open(config_file, 'r'):
line_number += 1
line_con = line.strip()
if line_con == '' or line_con[0] == '#':
continue
expand = True
if line_con[:2] == '- ':
line_con = line_con[2:]
expand = False
indent = re.findall(r'^[\t ]*(?=[^\t ])', line)[0]
line_level = indent.count(' ') + indent.count('\t')
if tree_level == None:
_assert(line_level == 0, 'invalid indent,line:' + str(line_number))
else:
_assert(line_level <= tree_level
or line_level == tree_level + 1, 'invalid indent,line:' + str(line_number))
tree_level = line_level
new_node = {'level':tree_level,'expanded':expand,'line_number':line_number,'line':line_con,'sub_lines':[]}
nodes_pool.append(new_node)
parent = find_parent_line(new_node)
parent['sub_lines'].append(new_node)
return tree, nodes_pool
def __init__(self, config_file):
self.hosts_tree, self.hosts_pool = self._parse_tree_from_config_file(config_file)
self.screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(0)
self.screen.keypad(1)
self.screen.border(0)
self.top_line_number = 0
self.highlight_line_number = 0
self.search_keyword = None
curses.start_color()
curses.use_default_colors()
#highlight
curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLUE)
self.COLOR_HIGHLIGHT = 2
#red
curses.init_pair(3, curses.COLOR_RED, -1)
self.COLOR_RED = 3
#red highlight
curses.init_pair(4, curses.COLOR_RED, curses.COLOR_BLUE)
self.COLOR_RED_HIGH = 4
#white bg
curses.init_pair(5, curses.COLOR_BLACK, curses.COLOR_WHITE)
self.COLOR_WBG = 5
#black bg
curses.init_pair(6, curses.COLOR_BLACK, curses.COLOR_BLACK)
self.COLOR_BBG = 6
try:
self.run()
except SystemExit:
self.restore_screen()
pass
except:
self.restore_screen()
traceback.print_exc()
def run(self):
while True:
self.render_screen()
c = self.screen.getch()
if c == curses.KEY_UP or c == self.KEY_k:
self.updown(-1)
elif c == curses.KEY_DOWN or c == self.KEY_j:
self.updown(1)
elif c == self.KEY_u:
for i in range(0, curses.tigetnum('lines')):
self.updown(-1)
elif c == self.KEY_d:
for i in range(0, curses.tigetnum('lines')):
self.updown(1)
elif c == self.KEY_ENTER or c == self.KEY_SPACE:
self.toggle_node()
elif c == self.KEY_ESC or c == self.KEY_q:
self.exit()
elif c == self.KEY_O or c == self.KEY_M:
self.open_all()
elif c == self.KEY_o or c == self.KEY_m:
self.open_node()
elif c == self.KEY_C or c == self.KEY_R:
self.close_all()
elif c == self.KEY_c or c == self.KEY_r:
self.close_node()
elif c == self.KEY_g:
self.page_top()
elif c == self.KEY_G:
self.page_bottom()
elif c == self.KEY_SPLASH:
self.enter_search_mode()
def exit(self):
if self.search_keyword is not None:
self.search_keyword = None
else:
sys.exit(0)
def enter_search_mode(self):
screen_cols = curses.tigetnum('cols')
self.screen.addstr(0, 0, '/' + ' ' * screen_cols)
curses.echo()
curses.curs_set(1)
self.search_keyword = self.screen.getstr(0, 1).decode('utf-8')
curses.noecho()
curses.curs_set(0)
def _get_visible_lines_for_render(self):
lines = []
stack = self.hosts_tree['sub_lines'] + []
while len(stack):
node = stack.pop()
lines.append(node)
if node['expanded'] and len(node['sub_lines']):
stack = stack + node['sub_lines']
lines.sort(key=lambda n:n['line_number'], reverse=False)
return lines
def _search_node(self):
rt = []
keyword = self.search_keyword.lower()
for node in self.hosts_pool:
if len(node['sub_lines']) == 0 and keyword in node['line'].lower():
rt.append(node)
return rt
def get_lines(self):
if self.search_keyword is not None:
return self._search_node()
else:
return self._get_visible_lines_for_render()
def page_top(self):
self.top_line_number = 0
self.highlight_line_number = 0
def page_bottom(self):
screen_lines = curses.tigetnum('lines')
visible_hosts = self.get_lines()
self.top_line_number = max(len(visible_hosts) - screen_lines, 0)
self.highlight_line_number = min(screen_lines, len(visible_hosts)) - 1
def open_node(self):
visible_hosts = self.get_lines()
linenum = self.top_line_number + self.highlight_line_number
node = visible_hosts[linenum]
if not len(node['sub_lines']):
return
stack = [node]
while len(stack):
node = stack.pop()
node['expanded'] = True
if len(node['sub_lines']):
stack = stack + node['sub_lines']
def close_node(self):
visible_hosts = self.get_lines()
linenum = self.top_line_number + self.highlight_line_number
node = visible_hosts[linenum]
if not len(node['sub_lines']):
return
stack = [node]
while len(stack):
node = stack.pop()
node['expanded'] = False
if len(node['sub_lines']):
stack = stack + node['sub_lines']
def open_all(self):
for node in self.hosts_pool:
if len(node['sub_lines']):
node['expanded'] = True
def close_all(self):
for node in self.hosts_pool:
if len(node['sub_lines']):
node['expanded'] = False
def toggle_node(self):
visible_hosts = self.get_lines()
linenum = self.top_line_number + self.highlight_line_number
node = visible_hosts[linenum]
if len(node['sub_lines']):
node['expanded'] = not node['expanded']
else:
self.restore_screen()
ssh = 'ssh'
if os.popen('which zssh 2> /dev/null').read().strip() != '':
ssh = 'zssh'
cmd = node['line'].split('#')[0]
os.execvp(ssh, [ssh] + re.split(r'[ ]+', cmd))
def render_screen(self):
# clear screen
self.screen.clear()
# now paint the rows
screen_lines = curses.tigetnum('lines')
screen_cols = curses.tigetnum('cols')
if self.highlight_line_number >= screen_lines:
self.highlight_line_number = screen_lines - 1
all_nodes = self.get_lines()
if self.top_line_number >= len(all_nodes):
self.top_line_number = 0
top = self.top_line_number
bottom = self.top_line_number + screen_lines
nodes = all_nodes[top:bottom]
if not len(nodes):
self.screen.refresh()
return
if self.highlight_line_number >= len(nodes):
self.highlight_line_number = len(nodes) - 1
if self.top_line_number >= len(all_nodes):
self.top_line_number = 0
for (index,node,) in enumerate(nodes):
#linenum = self.top_line_number + index
line = node['line']
if len(node['sub_lines']):
line += '(%d)' % len(node['sub_lines'])
prefix = ''
if self.search_keyword is None:
prefix += ' ' * node['level']
if len(node['sub_lines']):
if node['expanded']:
prefix += '-'
else:
prefix += '+'
else:
prefix += 'o'
prefix += ' '
# highlight current line
if index != self.highlight_line_number:
self.screen.addstr(index, 0, prefix, curses.color_pair(self.COLOR_RED))
self.screen.addstr(index, len(prefix), line)
else:
self.screen.addstr(index, 0, prefix, curses.color_pair(self.COLOR_RED_HIGH))
self.screen.addstr(index, len(prefix), line, curses.color_pair(self.COLOR_HIGHLIGHT))
#render scroll bar
for i in range(screen_lines):
self.screen.addstr(i, screen_cols - 2, '|', curses.color_pair(self.COLOR_WBG))
scroll_top = int(math.ceil((self.top_line_number + 1.0) / max(len(all_nodes), screen_lines) * screen_lines - 1))
scroll_height = int(math.ceil((len(nodes) + 0.0) / len(all_nodes) * screen_lines))
highlight_pos = int(math.ceil(scroll_height * ((self.highlight_line_number + 1.0)/min(screen_lines, len(nodes)))))
self.screen.addstr(scroll_top, screen_cols - 2, '^', curses.color_pair(self.COLOR_WBG))
self.screen.addstr(min(screen_lines, scroll_top + scroll_height) - 1, screen_cols - 2, 'v', curses.color_pair(self.COLOR_WBG))
self.screen.addstr(min(screen_lines, scroll_top + highlight_pos) - 1, screen_cols - 2, '+', curses.color_pair(self.COLOR_WBG))
self.screen.refresh()
# move highlight up/down one line
def updown(self, increment):
visible_hosts = self.get_lines()
visible_lines_count = len(visible_hosts)
next_line_number = self.highlight_line_number + increment
# paging
if increment < 0 and self.highlight_line_number == 0 and self.top_line_number != 0:
self.top_line_number += self.UP
return
elif increment > 0 and next_line_number == curses.tigetnum('lines') and (self.top_line_number+curses.tigetnum('lines')) != visible_lines_count:
self.top_line_number += self.DOWN
return
# scroll highlight line
if increment < 0 and (self.top_line_number != 0 or self.highlight_line_number != 0):
self.highlight_line_number = next_line_number
elif increment > 0 and (self.top_line_number+self.highlight_line_number+1) != visible_lines_count and self.highlight_line_number != curses.tigetnum('lines'):
self.highlight_line_number = next_line_number
def restore_screen(self):
curses.initscr()
curses.nocbreak()
curses.echo()
curses.endwin()
if __name__ == '__main__':
parser = OptionParser()
parser.add_option('-c', '--config', help='use specified config file instead of ~/.ssh_hosts')
options, args = parser.parse_args(sys.argv)
host_file = os.path.expanduser('~/.ssh_hosts')
if options.config is not None:
host_file = options.config
if not os.path.exists(host_file):
print("%s is not found" % host_file, file=sys.stderr)
sys.exit(1)
sshgo = SSHGO(host_file)