|
140 | 140 | with contextlib.suppress(ImportError): |
141 | 141 | from IPython import start_ipython |
142 | 142 |
|
| 143 | +from prompt_toolkit.auto_suggest import AutoSuggestFromHistory |
143 | 144 | from prompt_toolkit.completion import Completer, DummyCompleter |
144 | 145 | from prompt_toolkit.formatted_text import ANSI |
145 | 146 | from prompt_toolkit.history import InMemoryHistory |
@@ -293,83 +294,86 @@ def __init__( |
293 | 294 | stdin: TextIO | None = None, |
294 | 295 | stdout: TextIO | None = None, |
295 | 296 | *, |
296 | | - persistent_history_file: str = '', |
297 | | - persistent_history_length: int = 1000, |
298 | | - startup_script: str = '', |
299 | | - silence_startup_script: bool = False, |
300 | | - include_py: bool = False, |
301 | | - include_ipy: bool = False, |
302 | 297 | allow_cli_args: bool = True, |
303 | | - transcript_files: list[str] | None = None, |
| 298 | + allow_clipboard: bool = True, |
304 | 299 | allow_redirection: bool = True, |
305 | | - multiline_commands: list[str] | None = None, |
306 | | - terminators: list[str] | None = None, |
307 | | - shortcuts: dict[str, str] | None = None, |
308 | | - command_sets: Iterable[CommandSet] | None = None, |
309 | 300 | auto_load_commands: bool = False, |
310 | | - allow_clipboard: bool = True, |
311 | | - suggest_similar_command: bool = False, |
312 | | - intro: RenderableType = '', |
| 301 | + auto_suggest: bool = True, |
313 | 302 | bottom_toolbar: bool = False, |
| 303 | + command_sets: Iterable[CommandSet] | None = None, |
314 | 304 | complete_style: CompleteStyle = CompleteStyle.COLUMN, |
| 305 | + include_ipy: bool = False, |
| 306 | + include_py: bool = False, |
| 307 | + intro: RenderableType = '', |
315 | 308 | max_column_completion_items: int = 7, |
| 309 | + multiline_commands: list[str] | None = None, |
| 310 | + persistent_history_file: str = '', |
| 311 | + persistent_history_length: int = 1000, |
| 312 | + shortcuts: dict[str, str] | None = None, |
| 313 | + silence_startup_script: bool = False, |
| 314 | + startup_script: str = '', |
| 315 | + suggest_similar_command: bool = False, |
| 316 | + terminators: list[str] | None = None, |
| 317 | + transcript_files: list[str] | None = None, |
316 | 318 | ) -> None: |
317 | 319 | """Easy but powerful framework for writing line-oriented command interpreters, extends Python's cmd package. |
318 | 320 |
|
319 | 321 | :param completekey: name of a completion key, default to Tab |
320 | 322 | :param stdin: alternate input file object, if not specified, sys.stdin is used |
321 | 323 | :param stdout: alternate output file object, if not specified, sys.stdout is used |
322 | | - :param persistent_history_file: file path to load a persistent cmd2 command history from |
323 | | - :param persistent_history_length: max number of history items to write |
324 | | - to the persistent history file |
325 | | - :param startup_script: file path to a script to execute at startup |
326 | | - :param silence_startup_script: if ``True``, then the startup script's output will be |
327 | | - suppressed. Anything written to stderr will still display. |
328 | | - :param include_py: should the "py" command be included for an embedded Python shell |
329 | | - :param include_ipy: should the "ipy" command be included for an embedded IPython shell |
330 | 324 | :param allow_cli_args: if ``True``, then [cmd2.Cmd.__init__][] will process command |
331 | 325 | line arguments as either commands to be run or, if ``-t`` or |
332 | 326 | ``--test`` are given, transcript files to run. This should be |
333 | 327 | set to ``False`` if your application parses its own command line |
334 | 328 | arguments. |
335 | | - :param transcript_files: pass a list of transcript files to be run on initialization. |
336 | | - This allows running transcript tests when ``allow_cli_args`` |
337 | | - is ``False``. If ``allow_cli_args`` is ``True`` this parameter |
338 | | - is ignored. |
| 329 | + :param allow_clipboard: If False, cmd2 will disable clipboard interactions |
339 | 330 | :param allow_redirection: If ``False``, prevent output redirection and piping to shell |
340 | 331 | commands. This parameter prevents redirection and piping, but |
341 | 332 | does not alter parsing behavior. A user can still type |
342 | 333 | redirection and piping tokens, and they will be parsed as such |
343 | 334 | but they won't do anything. |
344 | | - :param multiline_commands: list of commands allowed to accept multi-line input |
345 | | - :param terminators: list of characters that terminate a command. These are mainly |
346 | | - intended for terminating multiline commands, but will also |
347 | | - terminate single-line commands. If not supplied, the default |
348 | | - is a semicolon. If your app only contains single-line commands |
349 | | - and you want terminators to be treated as literals by the parser, |
350 | | - then set this to an empty list. |
351 | | - :param shortcuts: dictionary containing shortcuts for commands. If not supplied, |
352 | | - then defaults to constants.DEFAULT_SHORTCUTS. If you do not want |
353 | | - any shortcuts, pass an empty dictionary. |
354 | | - :param command_sets: Provide CommandSet instances to load during cmd2 initialization. |
355 | | - This allows CommandSets with custom constructor parameters to be |
356 | | - loaded. This also allows the a set of CommandSets to be provided |
357 | | - when `auto_load_commands` is set to False |
358 | 335 | :param auto_load_commands: If True, cmd2 will check for all subclasses of `CommandSet` |
359 | 336 | that are currently loaded by Python and automatically |
360 | 337 | instantiate and register all commands. If False, CommandSets |
361 | 338 | must be manually installed with `register_command_set`. |
362 | | - :param allow_clipboard: If False, cmd2 will disable clipboard interactions |
363 | | - :param suggest_similar_command: if ``True``, then when a command is not found, |
364 | | - [cmd2.Cmd][] will look for similar commands and suggest them. |
365 | | - :param intro: introduction to display at startup |
| 339 | + :param auto_suggest: If True, cmd2 will provide fish shell style auto-suggestions |
| 340 | + based on history. If False, these will not be provided. |
366 | 341 | :param bottom_toolbar: if ``True``, then a bottom toolbar will be displayed. |
| 342 | + :param command_sets: Provide CommandSet instances to load during cmd2 initialization. |
| 343 | + This allows CommandSets with custom constructor parameters to be |
| 344 | + loaded. This also allows the a set of CommandSets to be provided |
| 345 | + when `auto_load_commands` is set to False |
367 | 346 | :param complete_style: style of prompt-toolkit tab completion to use, 3 valid options are: |
368 | 347 | 1. CompleteStyle.COLUMN (default) - displays hints with help next to them in one big column |
369 | 348 | 2. CompleteStyle.MULTI_COLUMN - displays hints across multiple columns, with help when selected |
370 | 349 | 3. CompleteStyle.READLINE_LIKE - displays like readline, complete_in_thread doesn't work |
| 350 | + :param include_ipy: should the "ipy" command be included for an embedded IPython shell |
| 351 | + :param include_py: should the "py" command be included for an embedded Python shell |
| 352 | + :param intro: introduction to display at startup |
371 | 353 | :param max_column_completion_items: The maximum number of completion results to display in a single column, |
372 | 354 | used to provide the initial value for a settable with the same name. |
| 355 | + :param multiline_commands: list of commands allowed to accept multi-line input |
| 356 | + :param persistent_history_file: file path to load a persistent cmd2 command history from |
| 357 | + :param persistent_history_length: max number of history items to write |
| 358 | + to the persistent history file |
| 359 | + :param shortcuts: dictionary containing shortcuts for commands. If not supplied, |
| 360 | + then defaults to constants.DEFAULT_SHORTCUTS. If you do not want |
| 361 | + any shortcuts, pass an empty dictionary. |
| 362 | + :param silence_startup_script: if ``True``, then the startup script's output will be |
| 363 | + suppressed. Anything written to stderr will still display. |
| 364 | + :param startup_script: file path to a script to execute at startup |
| 365 | + :param suggest_similar_command: if ``True``, then when a command is not found, |
| 366 | + [cmd2.Cmd][] will look for similar commands and suggest them. |
| 367 | + :param terminators: list of characters that terminate a command. These are mainly |
| 368 | + intended for terminating multiline commands, but will also |
| 369 | + terminate single-line commands. If not supplied, the default |
| 370 | + is a semicolon. If your app only contains single-line commands |
| 371 | + and you want terminators to be treated as literals by the parser, |
| 372 | + then set this to an empty list. |
| 373 | + :param transcript_files: pass a list of transcript files to be run on initialization. |
| 374 | + This allows running transcript tests when ``allow_cli_args`` |
| 375 | + is ``False``. If ``allow_cli_args`` is ``True`` this parameter |
| 376 | + is ignored. |
373 | 377 | """ |
374 | 378 | # Check if py or ipy need to be disabled in this instance |
375 | 379 | if not include_py: |
@@ -467,30 +471,36 @@ def _(event: Any) -> None: # pragma: no cover |
467 | 471 | self.lexer = Cmd2Lexer(self) |
468 | 472 | self.bottom_toolbar = bottom_toolbar |
469 | 473 |
|
| 474 | + self.auto_suggest = None |
| 475 | + if auto_suggest: |
| 476 | + self.auto_suggest = AutoSuggestFromHistory() |
| 477 | + |
470 | 478 | try: |
471 | 479 | self.session: PromptSession[str] = PromptSession( |
472 | | - history=self.history_adapter, |
473 | | - completer=self.completer, |
474 | | - lexer=self.lexer, |
475 | | - complete_style=complete_style, |
| 480 | + auto_suggest=self.auto_suggest, |
476 | 481 | complete_in_thread=True, |
| 482 | + complete_style=complete_style, |
477 | 483 | complete_while_typing=False, |
| 484 | + completer=self.completer, |
| 485 | + history=self.history_adapter, |
478 | 486 | key_bindings=key_bindings, |
| 487 | + lexer=self.lexer, |
479 | 488 | ) |
480 | 489 | except (NoConsoleScreenBufferError, AttributeError, ValueError): |
481 | 490 | # Fallback to dummy input/output if PromptSession initialization fails. |
482 | 491 | # This can happen in some CI environments (like GitHub Actions on Windows) |
483 | 492 | # where isatty() is True but there is no real console. |
484 | 493 | self.session = PromptSession( |
485 | | - history=self.history_adapter, |
486 | | - completer=self.completer, |
487 | | - lexer=self.lexer, |
488 | | - input=DummyInput(), |
489 | | - output=DummyOutput(), |
490 | | - complete_style=complete_style, |
| 494 | + auto_suggest=self.auto_suggest, |
491 | 495 | complete_in_thread=True, |
| 496 | + complete_style=complete_style, |
492 | 497 | complete_while_typing=False, |
| 498 | + completer=self.completer, |
| 499 | + history=self.history_adapter, |
| 500 | + input=DummyInput(), |
493 | 501 | key_bindings=key_bindings, |
| 502 | + lexer=self.lexer, |
| 503 | + output=DummyOutput(), |
494 | 504 | ) |
495 | 505 |
|
496 | 506 | # Commands to exclude from the history command |
|
0 commit comments