mirror of
https://github.com/ItsDrike/dotfiles.git
synced 2024-12-25 12:44:35 +00:00
Add pythonrc for xdg history file
This commit is contained in:
parent
ecac51aadb
commit
ebe36051d5
28
home/.config/pythonrc.py
Normal file
28
home/.config/pythonrc.py
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
import atexit
|
||||||
|
import os
|
||||||
|
import readline
|
||||||
|
from functools import partial
|
||||||
|
from pathlib import Path
|
||||||
|
from types import ModuleType
|
||||||
|
|
||||||
|
cache_xdg_dir = Path(
|
||||||
|
os.environ.get("XDG_CACHE_HOME", str(Path("~/.cache").expanduser()))
|
||||||
|
)
|
||||||
|
cache_xdg_dir.mkdir(exist_ok=True, parents=True)
|
||||||
|
|
||||||
|
history_file = cache_xdg_dir.joinpath("python_history")
|
||||||
|
history_file.touch()
|
||||||
|
|
||||||
|
readline.read_history_file(history_file)
|
||||||
|
|
||||||
|
|
||||||
|
def write_history(readline: ModuleType, history_file: Path) -> None:
|
||||||
|
"""
|
||||||
|
We need to get ``readline`` and ``history_file`` as arguments, as it
|
||||||
|
seems they get garbage collected when the function is registered and
|
||||||
|
the program ends, even though we refer to them here.
|
||||||
|
"""
|
||||||
|
readline.write_history_file(history_file)
|
||||||
|
|
||||||
|
|
||||||
|
atexit.register(partial(write_history, readline, history_file))
|
Loading…
Reference in a new issue