author | Oleksandr Gavenko <gavenkoa@gmail.com> |
Tue, 20 Oct 2015 21:22:47 +0300 | |
changeset 757 | 70dae9e483aa |
parent 311 | a63540742a19 |
permissions | -rwxr-xr-x |
226
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
1 |
# -*- mode: python -*- |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
2 |
# Add auto-completion and a stored history file of commands to your Python |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
3 |
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
4 |
# bound to the Esc key by default (you can change it - see readline docs). |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
5 |
# |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
6 |
# Store the file in ~/.pystartup, and set an environment variable to point |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
7 |
# to it: "export PYTHONSTARTUP=~/.pystartup" in bash. |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
8 |
|
227
d2bb2c3da51b
Make color prompt for interactive Python.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
226
diff
changeset
|
9 |
import os |
d2bb2c3da51b
Make color prompt for interactive Python.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
226
diff
changeset
|
10 |
import sys |
226
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
11 |
import atexit |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
12 |
import readline |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
13 |
import rlcompleter |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
14 |
|
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
15 |
historyPath = os.path.expanduser("~/.pyhistory") |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
16 |
|
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
17 |
def save_history(historyPath=historyPath): |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
18 |
import readline |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
19 |
readline.write_history_file(historyPath) |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
20 |
|
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
21 |
if os.path.exists(historyPath): |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
22 |
readline.read_history_file(historyPath) |
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
23 |
|
227
d2bb2c3da51b
Make color prompt for interactive Python.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
226
diff
changeset
|
24 |
term_with_colors = ['xterm', 'xterm-color', 'xterm-256color', 'linux', 'screen', 'screen-256color', 'screen-bce'] |
d2bb2c3da51b
Make color prompt for interactive Python.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
226
diff
changeset
|
25 |
if os.environ.get('TERM') in term_with_colors: |
311
a63540742a19
Fix Readline column calculation by marking non-printable escape sequences.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
227
diff
changeset
|
26 |
green='\001\033[32m\002' |
a63540742a19
Fix Readline column calculation by marking non-printable escape sequences.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
227
diff
changeset
|
27 |
red='\001\033[31m\002' |
a63540742a19
Fix Readline column calculation by marking non-printable escape sequences.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
227
diff
changeset
|
28 |
reset='\001\033[0m\002' |
227
d2bb2c3da51b
Make color prompt for interactive Python.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
226
diff
changeset
|
29 |
sys.ps1 = red + '>>> ' + reset |
d2bb2c3da51b
Make color prompt for interactive Python.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
226
diff
changeset
|
30 |
sys.ps2 = green + '... ' + reset |
d2bb2c3da51b
Make color prompt for interactive Python.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
226
diff
changeset
|
31 |
del term_with_colors |
d2bb2c3da51b
Make color prompt for interactive Python.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
226
diff
changeset
|
32 |
|
226
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
33 |
atexit.register(save_history) |
227
d2bb2c3da51b
Make color prompt for interactive Python.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
226
diff
changeset
|
34 |
del os, sys, atexit, readline, rlcompleter, save_history, historyPath |
226
97139da73273
Enable python statement readline completion and readline history for interactive Python .
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff
changeset
|
35 |