diff options
author | Azat Bahawi <azat@bahawi.net> | 1970-01-01 03:00:00 +0300 |
---|---|---|
committer | Azat Bahawi <azat@bahawi.net> | 2022-09-20 11:51:54 +0300 |
commit | 38bab29776950ac2364ed64195b89887717c33de (patch) | |
tree | edaaf366a55dbe96d268019124536bc504c64e87 /python |
Diffstat (limited to '')
-rw-r--r-- | python/.pystartup | 128 | ||||
-rwxr-xr-x | python/@install | 7 |
2 files changed, 135 insertions, 0 deletions
diff --git a/python/.pystartup b/python/.pystartup new file mode 100644 index 0000000..98d17b5 --- /dev/null +++ b/python/.pystartup @@ -0,0 +1,128 @@ +from code import InteractiveConsole +from tempfile import mkstemp +import atexit +import os +import readline +import rlcompleter +import sys + + +readline.parse_and_bind("tab: complete") + + +class TermColors(dict): + color_templates = ( + ("Normal", "0"), + ("Black", "0;30"), + ("Red", "0;31"), + ("Green", "0;32"), + ("Brown", "0;33"), + ("Blue", "0;34"), + ("Purple", "0;35"), + ("Cyan", "0;36"), + ("LightGray", "0;37"), + ("DarkGray", "1;30"), + ("LightRed", "1;31"), + ("LightGreen", "1;32"), + ("Yellow", "1;33"), + ("LightBlue", "1;34"), + ("LightPurple", "1;35"), + ("LightCyan", "1;36"), + ("White", "1;37") + ) + color_base = '\001\033[%sm\002' + + def __init__(self): + self.update(dict( + [(k, self.color_base % v) for k, v in self.color_templates])) + + +class Completer(object): + def save_history(self): + import readline + + readline.write_history_file(self.python_histfile) + + def __init__(self): + self.python_dir = os.path.expanduser( + "%s/python" % os.environ["XDG_DATA_HOME"]) + + if not os.path.exists(self.python_dir): + os.mkdir(self.python_dir) + + self.python_histfile = os.path.expanduser( + "%s/history" % self.python_dir) + + if os.path.exists(self.python_histfile): + readline.read_history_file(self.python_histfile) + + readline.set_history_length(1000) + atexit.register(self.save_history) + + +def DisplayHook(value): + if value is not None: + try: + import __builtin__ + __builtin__._ = value + except ImportError: + __builtins__._ = value + + import pprint + + pprint.pprint(value) + del pprint + + +class EditableBufferInteractiveConsole(InteractiveConsole): + def __init__(self, *args, **kwargs): + self.last_buffer = [] + InteractiveConsole.__init__(self, *args, **kwargs) + + def runsource(self, source, *args): + self.last_buffer = [source.encode('utf-8')] + return InteractiveConsole.runsource(self, source, *args) + + def raw_input(self, *args): + line = InteractiveConsole.raw_input(self, *args) + + if line == EDIT_CMD: + tmp_fd, tmp_file = mkstemp('.py') + + os.write(tmp_fd, b'\n'.join(self.last_buffer)) + os.close(tmp_fd) + + os.system('%s %s' % (EDITOR, tmp_file)) + + line = open(tmp_file).read() + + os.unlink(tmp_file) + tmp_file = '' + + lines = line.split('\n') + + for i in range(len(lines) - 1): + self.push(lines[i]) + + line = lines[-1] + return line + + +TC = TermColors() +ps1 = "%sλ%s %s>%s " +sys.ps1 = ps1 % (TC['Blue'], TC['Normal'], + TC['White'], TC['Normal']) +ps2 = " %s…%s %s>%s " +sys.ps2 = ps2 % (TC['Blue'], TC['Normal'], + TC['White'], TC['Normal']) +sys.displayhook = DisplayHook + +C = Completer() +EDITOR = os.environ.get('EDITOR', 'vim') +EDIT_CMD = ':e' +C = EditableBufferInteractiveConsole(locals=locals()) +C.interact(banner="") + +sys.exit() + +# vim: filetype=python diff --git a/python/@install b/python/@install new file mode 100755 index 0000000..1d8f685 --- /dev/null +++ b/python/@install @@ -0,0 +1,7 @@ +#!/bin/sh + +stow \ + --target="${HOME}" \ + --ignore="^@.*" \ + --verbose \ + python |