summaryrefslogtreecommitdiff
path: root/python/pythonrc
diff options
context:
space:
mode:
authorzachir <zachir@librem.one>2025-06-30 20:15:08 -0500
committerzachir <zachir@librem.one>2025-06-30 20:15:08 -0500
commitb413c6caae608500846872c17392e04d39c1bcb9 (patch)
tree3be0bb6ebf9f5612c7b1eef0efb5cb99234ea588 /python/pythonrc
parentacbbf1cf0af13fe4474927425c7d30fedd0e7236 (diff)
parent9ed20f04a91fe019f3a918289df444979bab77b4 (diff)
Merge branch 'master' into cla
Diffstat (limited to 'python/pythonrc')
-rw-r--r--python/pythonrc48
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)