mirror of
https://github.com/ItsDrike/dotfiles.git
synced 2025-06-29 20:20:41 +00:00
Major rewrite: switching back to Arch from NixOS
This commit is contained in:
parent
df585b737b
commit
254181c0fc
121 changed files with 5433 additions and 2371 deletions
|
@ -1,25 +1,30 @@
|
|||
import atexit
|
||||
import os
|
||||
import readline
|
||||
from functools import partial
|
||||
from pathlib import Path
|
||||
from types import ModuleType
|
||||
def is_vanilla() -> bool:
|
||||
import sys
|
||||
|
||||
cache_xdg_dir = Path(os.environ.get("XDG_CACHE_HOME", str(Path("~/.cache"))))
|
||||
cache_xdg_dir.mkdir(exist_ok=True, parents=True)
|
||||
|
||||
history_file = cache_xdg_dir.joinpath("python_history")
|
||||
|
||||
readline.read_history_file(history_file)
|
||||
return not hasattr(__builtins__, "__IPYTHON__") and "bpython" not in sys.argv[0]
|
||||
|
||||
|
||||
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)
|
||||
def setup_history():
|
||||
import os
|
||||
import atexit
|
||||
import readline
|
||||
from pathlib import Path
|
||||
|
||||
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():
|
||||
# breaks on macos + python3 without this.
|
||||
readline.write_history_file(str(history))
|
||||
|
||||
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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue