mirror of
https://github.com/ItsDrike/nixdots
synced 2024-12-26 03:34:35 +00:00
python: fix history location & persist history
This commit is contained in:
parent
8a6311b896
commit
46f5fa83e8
|
@ -57,34 +57,34 @@ in {
|
||||||
'';
|
'';
|
||||||
|
|
||||||
"python/pythonrc.py".text = ''
|
"python/pythonrc.py".text = ''
|
||||||
import atexit
|
def is_vanilla() -> bool:
|
||||||
import os
|
import sys
|
||||||
import readline
|
return not hasattr(__builtins__, '__IPYTHON__') and 'bpython' not in sys.argv[0]
|
||||||
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:
|
def setup_history():
|
||||||
"""
|
import os
|
||||||
We need to get ``readline`` and ``history_file`` as arguments, as it
|
import atexit
|
||||||
seems they get garbage collected when the function is registered and
|
import readline
|
||||||
the program ends, even though we refer to them here.
|
from pathlib import Path
|
||||||
"""
|
|
||||||
readline.write_history_file(history_file)
|
if state_home := os.environ.get('XDG_STATE_HOME'):
|
||||||
|
state_home = Path(state_home)
|
||||||
|
else:
|
||||||
|
state_home = Path.home() / '.local' / 'state'
|
||||||
|
|
||||||
|
history: Path = state_home / 'python_history'
|
||||||
|
|
||||||
|
# https://github.com/python/cpython/issues/105694
|
||||||
|
if not history.is_file():
|
||||||
|
readline.write_history_file(str(history)) # breaks on macos + python3 without this.
|
||||||
|
|
||||||
|
readline.read_history_file(str(history))
|
||||||
|
atexit.register(readline.write_history_file, str(history))
|
||||||
|
|
||||||
|
|
||||||
atexit.register(partial(write_history, readline, history_file))
|
if is_vanilla():
|
||||||
|
setup_history()
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -87,6 +87,7 @@
|
||||||
".cache/walker/history.gob"
|
".cache/walker/history.gob"
|
||||||
".config/pcmanfm-qt/default/recent-files.conf"
|
".config/pcmanfm-qt/default/recent-files.conf"
|
||||||
".config/qalculate/qalculate-gtk.cfg"
|
".config/qalculate/qalculate-gtk.cfg"
|
||||||
|
".local/state/python_history"
|
||||||
];
|
];
|
||||||
|
|
||||||
persistentDataMountPoint = "/data/Data";
|
persistentDataMountPoint = "/data/Data";
|
||||||
|
|
Loading…
Reference in a new issue