|
1 | | -import readline |
| 1 | +from typing import Optional |
2 | 2 |
|
| 3 | +from prompt_toolkit import PromptSession |
| 4 | +from prompt_toolkit.formatted_text import HTML |
| 5 | +from prompt_toolkit.history import InMemoryHistory |
| 6 | +from prompt_toolkit.key_binding import KeyBindings |
3 | 7 |
|
4 | | -async def get_input( |
5 | | - placeholder_text=None, placeholder_color: str = "gray", multiline_support=True |
| 8 | + |
| 9 | +async def async_get_input( |
| 10 | + placeholder_text: Optional[str] = None, |
| 11 | + placeholder_color: str = "gray", |
| 12 | + multiline_support: bool = True, |
| 13 | +) -> str: |
| 14 | + placeholder_text = "Describe command" |
| 15 | + history = InMemoryHistory() |
| 16 | + session = PromptSession( |
| 17 | + history=history, |
| 18 | + enable_open_in_editor=False, |
| 19 | + enable_history_search=False, |
| 20 | + auto_suggest=None, |
| 21 | + multiline=True, |
| 22 | + ) |
| 23 | + kb = KeyBindings() |
| 24 | + multiline = [False] |
| 25 | + |
| 26 | + @kb.add("enter") |
| 27 | + def _(event): |
| 28 | + current_line = event.current_buffer.document.current_line.rstrip() |
| 29 | + |
| 30 | + if current_line == '"""': |
| 31 | + multiline[0] = not multiline[0] |
| 32 | + event.current_buffer.insert_text("\n") |
| 33 | + if not multiline[0]: # If exiting multiline mode, submit |
| 34 | + event.current_buffer.validate_and_handle() |
| 35 | + return |
| 36 | + |
| 37 | + if multiline[0]: |
| 38 | + event.current_buffer.insert_text("\n") |
| 39 | + else: |
| 40 | + event.current_buffer.validate_and_handle() |
| 41 | + |
| 42 | + result = await session.prompt_async( |
| 43 | + "> ", |
| 44 | + placeholder=HTML(f'<style fg="{placeholder_color}">{placeholder_text}</style>') |
| 45 | + if placeholder_text |
| 46 | + else None, |
| 47 | + key_bindings=kb, |
| 48 | + complete_while_typing=False, |
| 49 | + enable_suspend=False, |
| 50 | + search_ignore_case=True, |
| 51 | + include_default_pygments_style=False, |
| 52 | + input_processors=[], |
| 53 | + enable_system_prompt=False, |
| 54 | + wrap_lines=False, |
| 55 | + ) |
| 56 | + return result |
| 57 | + |
| 58 | + |
| 59 | +def get_input( |
| 60 | + placeholder_text: Optional[str] = None, |
| 61 | + placeholder_color: str = "gray", |
| 62 | + multiline_support: bool = True, |
6 | 63 | ) -> str: |
7 | | - return input("> ") |
| 64 | + placeholder_text = "Describe command" |
| 65 | + history = InMemoryHistory() |
| 66 | + session = PromptSession( |
| 67 | + history=history, |
| 68 | + enable_open_in_editor=False, |
| 69 | + enable_history_search=False, |
| 70 | + auto_suggest=None, |
| 71 | + multiline=True, |
| 72 | + ) |
| 73 | + kb = KeyBindings() |
| 74 | + multiline = [False] |
| 75 | + |
| 76 | + @kb.add("enter") |
| 77 | + def _(event): |
| 78 | + current_line = event.current_buffer.document.current_line.rstrip() |
| 79 | + |
| 80 | + if current_line == '"""': |
| 81 | + multiline[0] = not multiline[0] |
| 82 | + event.current_buffer.insert_text("\n") |
| 83 | + if not multiline[0]: # If exiting multiline mode, submit |
| 84 | + event.current_buffer.validate_and_handle() |
| 85 | + return |
| 86 | + |
| 87 | + if multiline[0]: |
| 88 | + event.current_buffer.insert_text("\n") |
| 89 | + else: |
| 90 | + event.current_buffer.validate_and_handle() |
| 91 | + |
| 92 | + result = session.prompt( |
| 93 | + "> ", |
| 94 | + placeholder=HTML(f'<style fg="{placeholder_color}">{placeholder_text}</style>') |
| 95 | + if placeholder_text |
| 96 | + else None, |
| 97 | + key_bindings=kb, |
| 98 | + complete_while_typing=False, |
| 99 | + enable_suspend=False, |
| 100 | + search_ignore_case=True, |
| 101 | + include_default_pygments_style=False, |
| 102 | + input_processors=[], |
| 103 | + enable_system_prompt=False, |
| 104 | + wrap_lines=False, |
| 105 | + ) |
| 106 | + return result |
0 commit comments