summaryrefslogtreecommitdiff
path: root/python/pythonrc
diff options
context:
space:
mode:
Diffstat (limited to 'python/pythonrc')
-rw-r--r--python/pythonrc40
1 files changed, 40 insertions, 0 deletions
diff --git a/python/pythonrc b/python/pythonrc
new file mode 100644
index 0000000..c7cf7d5
--- /dev/null
+++ b/python/pythonrc
@@ -0,0 +1,40 @@
+try:
+ import atexit
+ import os
+ import sys
+ from pathlib import Path
+ import readline
+except ImportError as e:
+ print(f"Couldn't load module. {e}")
+ sys.exit(1)
+
+
+################
+# TAB COMPLETION #
+##################
+
+try:
+ readline.parse_and_bind("tab: complete")
+except ImportError:
+ pass
+
+
+### XDG Compliant History File
+# See https://gist.github.com/viliampucik/8713b09ff7e4d984b29bfcd7804dc1f4?permalink_comment_id=4582040#gistcomment-4582040
+
+# 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__
+
+
+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)
+
+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)