Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagesmc
Path: blob/master/src/sage/libs/readline.pyx
8815 views
1
"""
2
Readline
3
4
This is the library behind the command line input, it takes keypresses
5
until you hit Enter and then returns it as a string to Python. We hook
6
into it so we can make it redraw the input area.
7
8
EXAMPLES::
9
10
sage: from sage.libs.readline import *
11
sage: replace_line('foobar', 0)
12
sage: set_point(3)
13
sage: print 'current line:', repr(copy_text(0, get_end()))
14
current line: 'foobar'
15
sage: print 'cursor position:', get_point()
16
cursor position: 3
17
18
When printing with :class:`interleaved_output` the prompt and current
19
line is removed::
20
21
sage: with interleaved_output():
22
....: print 'output'
23
....: print 'current line:', repr(copy_text(0, get_end()))
24
....: print 'cursor position:', get_point()
25
output
26
current line: ''
27
cursor position: 0
28
29
After the interleaved output, the line and cursor is restored to the
30
old value::
31
32
sage: print 'current line:', repr(copy_text(0, get_end()))
33
current line: 'foobar'
34
sage: print 'cursor position:', get_point()
35
cursor position: 3
36
37
Finally, clear the current line for the remaining doctests::
38
39
sage: replace_line('', 1)
40
"""
41
42
#*****************************************************************************
43
# Copyright (C) 2013 Volker Braun <[email protected]>
44
#
45
# Distributed under the terms of the GNU General Public License (GPL)
46
# as published by the Free Software Foundation; either version 3 of
47
# the License, or (at youroption) any later version.
48
# http://www.gnu.org/licenses/
49
#*****************************************************************************
50
51
52
53
cdef extern from 'readline/readline.h':
54
int rl_forced_update_display()
55
int rl_redisplay()
56
int rl_message(char* msg)
57
int rl_clear_message()
58
void rl_replace_line(char* line, int pos)
59
char* rl_copy_text(int begin, int end)
60
void rl_save_prompt()
61
void rl_restore_prompt()
62
int rl_read_key()
63
int rl_set_signals()
64
int rl_clear_signals()
65
int rl_crlf()
66
int rl_initialize()
67
int rl_catch_signals
68
int rl_catch_sigwinch
69
int rl_point
70
int rl_end
71
72
73
def print_status():
74
"""
75
Print readline status for debug purposes
76
77
EXAMPLES::
78
79
sage: from sage.libs.readline import print_status
80
sage: print_status()
81
catch_signals: 1
82
catch_sigwinch: 1
83
"""
84
print 'catch_signals:', rl_catch_signals
85
print 'catch_sigwinch:', rl_catch_sigwinch
86
87
def set_signals():
88
"""
89
Install the readline signal handlers
90
91
Install Readline's signal handler for SIGINT, SIGQUIT, SIGTERM,
92
SIGALRM, SIGTSTP, SIGTTIN, SIGTTOU, and SIGWINCH, depending on the
93
values of rl_catch_signals and rl_catch_sigwinch.
94
95
EXAMPLES::
96
97
sage: from sage.libs.readline import set_signals
98
sage: set_signals()
99
0
100
"""
101
return rl_set_signals()
102
103
104
def clear_signals():
105
"""
106
Remove the readline signal handlers
107
108
Remove all of the Readline signal handlers installed by
109
:func:`set_signals`
110
111
EXAMPLES::
112
113
sage: from sage.libs.readline import clear_signals
114
sage: clear_signals()
115
0
116
"""
117
return rl_clear_signals()
118
119
def get_point():
120
"""
121
Get the cursor position
122
123
OUTPUT:
124
125
Integer
126
127
EXAMPLES::
128
129
sage: from sage.libs.readline import get_point, set_point
130
sage: get_point()
131
0
132
sage: set_point(5)
133
sage: get_point()
134
5
135
sage: set_point(0)
136
"""
137
return rl_point
138
139
def get_end():
140
"""
141
Get the end position of the current input
142
143
OUTPUT:
144
145
Integer
146
147
EXAMPLES::
148
149
sage: from sage.libs.readline import get_end
150
sage: get_end()
151
0
152
"""
153
return rl_end
154
155
def set_point(point):
156
"""
157
Set the cursor position
158
159
INPUT:
160
161
- ``point`` -- integer. The new cursor position.
162
163
EXAMPLES::
164
165
sage: from sage.libs.readline import get_point, set_point
166
sage: get_point()
167
0
168
sage: set_point(5)
169
sage: get_point()
170
5
171
sage: set_point(0)
172
"""
173
global rl_point
174
rl_point = point
175
176
def forced_update_display():
177
"""
178
Force the line to be updated and redisplayed, whether or not
179
Readline thinks the screen display is correct.
180
181
EXAMPLES::
182
183
sage: from sage.libs.readline import forced_update_display
184
sage: forced_update_display()
185
0
186
"""
187
return rl_forced_update_display()
188
189
def copy_text(pos_start, pos_end):
190
"""
191
Return a copy of the text between start and end in the current line.
192
193
INPUT:
194
195
- ``pos_start``, ``pos_end`` -- integer. Start and end position.
196
197
OUTPUT:
198
199
String.
200
201
EXAMPLES::
202
203
sage: from sage.libs.readline import copy_text, replace_line
204
sage: replace_line('foobar', 0)
205
sage: copy_text(1, 5)
206
'ooba'
207
"""
208
return rl_copy_text(pos_start, pos_end)
209
210
def replace_line(text, clear_undo):
211
"""
212
Replace the contents of rl_line_buffer with text.
213
214
The point and mark are preserved, if possible.
215
216
INPUT:
217
218
- ``text`` -- the new content of the line.
219
220
- ``clear_undo`` -- integer. If non-zero, the undo list associated
221
with the current line is cleared.
222
223
EXAMPLES::
224
225
sage: from sage.libs.readline import copy_text, replace_line
226
sage: replace_line('foobar', 0)
227
sage: copy_text(1, 5)
228
'ooba'
229
"""
230
rl_replace_line(text, clear_undo)
231
232
def initialize():
233
"""
234
Initialize or re-initialize Readline’s internal state. It’s not
235
strictly necessary to call this; readline() calls it before
236
reading any input.
237
238
EXAMPLES::
239
240
sage: from sage.libs.readline import initialize
241
sage: initialize()
242
0
243
"""
244
return rl_initialize()
245
246
247
248
class interleaved_output:
249
250
def __init__(self):
251
"""
252
Context manager for asynchronous output
253
254
This allows you to show output while at the readline
255
prompt. When the block is left, the prompt is restored even if
256
it was clobbered by the output.
257
258
EXAMPLES::
259
260
sage: from sage.libs.readline import interleaved_output
261
sage: with interleaved_output():
262
....: print 'output'
263
output
264
"""
265
pass
266
267
def __enter__(self):
268
"""
269
Called when entering the with block
270
271
EXAMPLES::
272
273
sage: from sage.libs.readline import interleaved_output
274
sage: with interleaved_output():
275
....: print 'output'
276
output
277
"""
278
self._saved_point = rl_point;
279
self._saved_line = rl_copy_text(0, rl_end)
280
rl_save_prompt()
281
rl_replace_line('', 0)
282
rl_redisplay()
283
rl_clear_signals()
284
285
def __exit__(self, exc_type, exc_val, exc_tb):
286
"""
287
Called when entering the with block
288
289
EXAMPLES::
290
291
sage: from sage.libs.readline import interleaved_output
292
sage: with interleaved_output():
293
....: print 'output'
294
output
295
"""
296
rl_set_signals()
297
rl_replace_line(self._saved_line, 0)
298
global rl_point
299
rl_point = self._saved_point
300
rl_restore_prompt()
301
rl_forced_update_display()
302
return False
303
304
305
306
307
308
309