##// END OF EJS Templates
Make readline tab-completion work in two-process terminal frontend.
Thomas Kluyver -
Show More
@@ -1,49 +1,52 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 import readline
2 import readline
3 import time
3 import time
4 import sys
4 import sys
5 stdout = sys.stdout
5 stdout = sys.stdout
6 class ClientCompleter2p(object):
6 class ClientCompleter2p(object):
7 """Client-side completion machinery.
7 """Client-side completion machinery.
8
8
9 How it works: self.complete will be called multiple times, with
9 How it works: self.complete will be called multiple times, with
10 state=0,1,2,... When state=0 it should compute ALL the completion matches,
10 state=0,1,2,... When state=0 it should compute ALL the completion matches,
11 and then return them for each value of state."""
11 and then return them for each value of state."""
12
12
13 def __init__(self,client, km):
13 def __init__(self,client, km):
14 self.km = km
14 self.km = km
15 self.matches = []
15 self.matches = []
16 self.client = client
16 self.client = client
17
17
18 def complete_request(self,text):
18 def complete_request(self,text):
19 line = readline.get_line_buffer()
19 line = readline.get_line_buffer()
20 #msg_id = self.km.xreq_channel.complete(text=text,line=line)#this method is not working, the code not continue
20 #msg_id = self.km.xreq_channel.complete(text=text,line=line)#this method is not working, the code not continue
21 msg = self.km.session.send(self.km.xreq_channel.socket,
21 msg = self.km.session.send(self.km.xreq_channel.socket,
22 'complete_request',
22 'complete_request',
23 dict(text=text, line=line))
23 dict(text=text, line=line))
24 # send completion request to kernel
24 # send completion request to kernel
25 # Give the kernel up to 0.5s to respond
25 # Give the kernel up to 0.5s to respond
26 msg_matches = None
26 msg_matches = None
27 for i in range(5):
27 for i in range(5):
28 if self.km.xreq_channel.was_called():
28 if self.km.xreq_channel.was_called():
29 msg_xreq = self.km.xreq_channel.get_msg()
29 msg_xreq = self.km.xreq_channel.get_msg()
30 if msg["header"]['session'] == msg_xreq["parent_header"]['session'] :
30 if msg["header"]['session'] == msg_xreq["parent_header"]['session'] :
31 if msg_xreq["content"]["status"] == 'ok' :
31 if msg_xreq["content"]["status"] == 'ok' :
32 if msg_xreq["msg_type"] == "complete_reply" :
32 if msg_xreq["msg_type"] == "complete_reply" :
33 msg_matches = msg_xreq["content"]["matches"]
33 msg_matches = msg_xreq["content"]["matches"]
34 #break
34 #break
35 time.sleep(0.1)
35 time.sleep(0.1)
36 return msg_matches
36 return msg_matches
37
37
38 def complete(self, text, state):
38 def complete(self, text, state):
39 if state == 0 :
39 if state == 0 :
40 self.matches = self.complete_request(text)
40 self.matches = self.complete_request(text)
41
41
42 if self.matches is None:
42 if self.matches is None:
43 self.matches = []
43 self.matches = []
44 print('WARNING: Kernel timeout on tab completion.')
44 print('WARNING: Kernel timeout on tab completion.')
45 #print self.matches
45 #print self.matches
46 return self.matches
46 try:
47 return self.matches[state]
48 except IndexError:
49 return None
50
47
51
48
52
49 No newline at end of file
General Comments 0
You need to be logged in to leave comments. Login now