]> glassweightruler.freedombox.rocks Git - xdg-ninja.git/commitdiff
Use native readline function for new history file (#417)
authorItsDrike <itsdrike@protonmail.com>
Fri, 28 Jun 2024 13:03:49 +0000 (13:03 +0000)
committerGitHub <noreply@github.com>
Fri, 28 Jun 2024 13:03:49 +0000 (15:03 +0200)
programs/python.json

index 9ad7bbd361054ca91dd80ac49041c66eaf188607..fb9d0adce1dd70ac4ebf422bd96938f9889a15c7 100644 (file)
@@ -1,7 +1,7 @@
 {
     "files": [
         {
-            "help": "If you're running python v3.13.0a3 or later, you can simply set the `PYTHON_HISTORY` environment variable.\n\nOtherwise, export the following environment variable:\n\n```bash\nexport PYTHONSTARTUP=\"$XDG_CONFIG_HOME\"/python/pythonrc\n```\n\nThen create the file _$XDG_CONFIG_HOME/python/pythonrc_, and put the following code into it:\n\n```python\n#!/usr/bin/env python3\n# This entire thing is unnecessary post v3.13.0a3\n# https://github.com/python/cpython/issues/73965\n\ndef is_vanilla() -> bool:\n    \"\"\" :return: whether running \"vanilla\" Python \"\"\"\n    import sys\n    return not hasattr(__builtins__, '__IPYTHON__') and 'bpython' not in sys.argv[0]\n\n\ndef setup_history():\n    \"\"\" read and write history from state file \"\"\"\n    import os\n    import atexit\n    import readline\n    from pathlib import Path\n\n    # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables\n    if state_home := os.environ.get('XDG_STATE_HOME'):\n            state_home = Path(state_home)\n    else:\n        state_home = Path.home() / '.local' / 'state'\n    if not state_home.is_dir():\n        print(\"Error: XDG_SATE_HOME does not exist at\", state_home)\n\n    history: Path = state_home / 'python_history'\n\n    # https://github.com/python/cpython/issues/105694\n    if not history.is_file():\n        with open(history,\"w\") as f:\n            f.write(\"_HiStOrY_V2_\" + \"\\n\\n\") # breaks on macos + python3 without this.\n\n    readline.read_history_file(history)\n    atexit.register(readline.write_history_file, history)\n\n\nif is_vanilla():\n    setup_history()\n```\n",
+            "help": "If you're running python v3.13.0a3 or later, you can simply set the `PYTHON_HISTORY` environment variable.\n\nOtherwise, export the following environment variable:\n\n```bash\nexport PYTHONSTARTUP=\"$XDG_CONFIG_HOME\"/python/pythonrc\n```\n\nThen create the file _$XDG_CONFIG_HOME/python/pythonrc_, and put the following code into it:\n\n```python\n#!/usr/bin/env python3\n# This entire thing is unnecessary post v3.13.0a3\n# https://github.com/python/cpython/issues/73965\n\ndef is_vanilla() -> bool:\n    \"\"\" :return: whether running \"vanilla\" Python \"\"\"\n    import sys\n    return not hasattr(__builtins__, '__IPYTHON__') and 'bpython' not in sys.argv[0]\n\n\ndef setup_history():\n    \"\"\" read and write history from state file \"\"\"\n    import os\n    import atexit\n    import readline\n    from pathlib import Path\n\n    # https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables\n    if state_home := os.environ.get('XDG_STATE_HOME'):\n            state_home = Path(state_home)\n    else:\n        state_home = Path.home() / '.local' / 'state'\n    if not state_home.is_dir():\n        print(\"Error: XDG_STATE_HOME does not exist at\", state_home)\n\n    history: Path = state_home / 'python_history'\n\n    # https://github.com/python/cpython/issues/105694\n    if not history.is_file():\n        readline.write_history_file(str(history)) # breaks on macos + python3 without this.\n\n    readline.read_history_file(history)\n    atexit.register(readline.write_history_file, history)\n\n\nif is_vanilla():\n    setup_history()\n```\n",
             "movable": true,
             "path": "$HOME/.python_history"
         }