summaryrefslogtreecommitdiff
path: root/python/pythonrc
diff options
context:
space:
mode:
authorGitIR <git@zachir.xyz>2025-08-13 17:36:31 -0500
committerGitIR <git@zachir.xyz>2025-08-13 17:36:31 -0500
commitf1b0dd12663867863f58db2d5735cdb3cc84da04 (patch)
tree2c9d38e6ccd82721090c7030766f91eaf36a82a5 /python/pythonrc
parent2666a2d597f5fb4222142a5ce147546cf588887a (diff)
parentc0ee1d4b5dbd37b05947ffcc584c0834d523626d (diff)
Merge branch 'master' into rai-master
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)