Enable readline for arrow keys, line editing, and persistent history.
()
| 87 | |
| 88 | |
| 89 | def _enable_line_editing() -> None: |
| 90 | """Enable readline for arrow keys, line editing, and persistent history.""" |
| 91 | global _READLINE, _HISTORY_FILE, _HISTORY_HOOK_REGISTERED, _USING_LIBEDIT, _SAVED_TERM_ATTRS |
| 92 | |
| 93 | # Save terminal state before readline touches it |
| 94 | try: |
| 95 | import termios |
| 96 | |
| 97 | _SAVED_TERM_ATTRS = termios.tcgetattr(sys.stdin.fileno()) |
| 98 | except Exception: |
| 99 | pass |
| 100 | |
| 101 | history_file = Path.home() / ".nanobot" / "history" / "cli_history" |
| 102 | history_file.parent.mkdir(parents=True, exist_ok=True) |
| 103 | _HISTORY_FILE = history_file |
| 104 | |
| 105 | try: |
| 106 | import readline |
| 107 | except ImportError: |
| 108 | return |
| 109 | |
| 110 | _READLINE = readline |
| 111 | _USING_LIBEDIT = "libedit" in (readline.__doc__ or "").lower() |
| 112 | |
| 113 | try: |
| 114 | if _USING_LIBEDIT: |
| 115 | readline.parse_and_bind("bind ^I rl_complete") |
| 116 | else: |
| 117 | readline.parse_and_bind("tab: complete") |
| 118 | readline.parse_and_bind("set editing-mode emacs") |
| 119 | except Exception: |
| 120 | pass |
| 121 | |
| 122 | try: |
| 123 | readline.read_history_file(str(history_file)) |
| 124 | except Exception: |
| 125 | pass |
| 126 | |
| 127 | if not _HISTORY_HOOK_REGISTERED: |
| 128 | atexit.register(_save_history) |
| 129 | _HISTORY_HOOK_REGISTERED = True |
| 130 | |
| 131 | |
| 132 | def _prompt_text() -> str: |