diff options
Diffstat (limited to 'python/pythonrc')
-rw-r--r-- | python/pythonrc | 48 |
1 files changed, 32 insertions, 16 deletions
diff --git a/python/pythonrc b/python/pythonrc index cd954b6..c7cf7d5 100644 --- a/python/pythonrc +++ b/python/pythonrc @@ -1,24 +1,40 @@ -def is_vanilla() -> bool: +try: + import atexit + import os import sys - return not hasattr(__builtins__, '__IPYTHON__') and 'bpython' not in sys.argv[0] + from pathlib import Path + import readline +except ImportError as e: + print(f"Couldn't load module. {e}") + sys.exit(1) -def setup_history(): - import os - import atexit - import readline - from pathlib import Path +################ +# TAB COMPLETION # +################## + +try: + readline.parse_and_bind("tab: complete") +except ImportError: + pass + - if state_home := os.environ.get('XDG_STATE_HOME'): - state_home = Path(state_home) - else: - state_home = Path.home() / '.local' / 'state' +### XDG Compliant History File +# See https://gist.github.com/viliampucik/8713b09ff7e4d984b29bfcd7804dc1f4?permalink_comment_id=4582040#gistcomment-4582040 - history: Path = state_home / 'python_history' +# Destroy default history file writing hook (and also tab completion, which is why we manually added it above) +if hasattr(sys, '__interactivehook__'): + del sys.__interactivehook__ - readline.read_history_file(str(history)) - atexit.register(readline.write_history_file, str(history)) +histfile = Path(os.getenv("XDG_CACHE_HOME", Path.home() / ".cache")) / "python_history" +try: + histfile.touch(exist_ok=True) +except FileNotFoundError: # Probably the parent directory doesn't exist + histfile.parent.mkdir(parents=True, exist_ok=True) -if is_vanilla(): - setup_history() +readline.read_history_file(histfile) +# Don't store an obscene amount of history +readline.set_history_length(5000) +# Write to history file on exit +atexit.register(readline.write_history_file, histfile) |