|
9 | 9 | from typing import TypedDict |
10 | 10 |
|
11 | 11 | import questionary |
| 12 | +import questionary.prompts.text |
| 13 | +from prompt_toolkit.key_binding import KeyBindings |
| 14 | +from prompt_toolkit.key_binding.key_processor import KeyPressEvent |
| 15 | +from prompt_toolkit.keys import Keys |
12 | 16 |
|
13 | 17 | from commitizen import factory, git, out |
14 | 18 | from commitizen.config import BaseConfig |
@@ -83,21 +87,114 @@ def _prompt_commit_questions(self) -> str: |
83 | 87 | raise CustomError(root_err.__str__()) |
84 | 88 | raise err |
85 | 89 | elif question["type"] == "input" and question.get("multiline", False): |
86 | | - print( |
87 | | - "\033[90m💡 Multiline input:\n Press Enter for new lines and Esc+Enter to finish\033[0m \n \033[90mor (Finish with 'Alt+Enter' or 'Esc then Enter')\033[0m" |
| 90 | + is_optional = ( |
| 91 | + question.get("default") == "" |
| 92 | + or "skip" in question.get("message", "").lower() |
88 | 93 | ) |
| 94 | + |
| 95 | + if is_optional: |
| 96 | + print( |
| 97 | + "\033[90m💡 Multiline input:\n Press Enter on empty line to skip, Enter after text for new lines, Alt+Enter to finish\033[0m" |
| 98 | + ) |
| 99 | + else: |
| 100 | + print( |
| 101 | + "\033[90m💡 Multiline input:\n Press Enter for new lines and Alt+Enter to finish\033[0m" |
| 102 | + ) |
| 103 | + |
| 104 | + # Create custom multiline input with Enter-on-empty behavior for optional fields |
| 105 | + |
89 | 106 | multiline_question = question.copy() |
90 | 107 | multiline_question["multiline"] = True |
91 | | - try: |
92 | | - answer = questionary.prompt([multiline_question], style=cz.style) |
93 | | - if not answer: |
94 | | - raise NoAnswersError() |
95 | | - answers.update(answer) |
96 | | - except ValueError as err: |
97 | | - root_err = err.__context__ |
98 | | - if isinstance(root_err, CzException): |
99 | | - raise CustomError(root_err.__str__()) |
100 | | - raise err |
| 108 | + |
| 109 | + if is_optional: |
| 110 | + # Create custom key bindings for optional fields |
| 111 | + bindings = KeyBindings() |
| 112 | + |
| 113 | + @bindings.add(Keys.Enter) |
| 114 | + def _(event: KeyPressEvent) -> None: |
| 115 | + buffer = event.current_buffer |
| 116 | + # If buffer is completely empty, submit |
| 117 | + if not buffer.text.strip(): |
| 118 | + event.app.exit(result=buffer.text) |
| 119 | + else: |
| 120 | + # If there's text, add new line |
| 121 | + buffer.newline() |
| 122 | + |
| 123 | + # Use the text prompt directly with custom bindings |
| 124 | + try: |
| 125 | + result = questionary.prompts.text.text( |
| 126 | + message=question["message"], |
| 127 | + multiline=True, |
| 128 | + style=cz.style, |
| 129 | + key_bindings=bindings, |
| 130 | + ).ask() |
| 131 | + |
| 132 | + field_name = question["name"] |
| 133 | + if result is None: |
| 134 | + result = question.get("default", "") |
| 135 | + |
| 136 | + # Apply filter if present |
| 137 | + if "filter" in question: |
| 138 | + result = question["filter"](result) |
| 139 | + |
| 140 | + answer = {field_name: result} |
| 141 | + answers.update(answer) |
| 142 | + |
| 143 | + except Exception: |
| 144 | + # Fallback to standard behavior if custom approach fails |
| 145 | + answer = questionary.prompt( |
| 146 | + [multiline_question], style=cz.style |
| 147 | + ) |
| 148 | + if not answer: |
| 149 | + raise NoAnswersError() |
| 150 | + answers.update(answer) |
| 151 | + else: |
| 152 | + # Required fields - don't allow newline on empty first line and show error |
| 153 | + bindings = KeyBindings() |
| 154 | + |
| 155 | + @bindings.add(Keys.Enter) |
| 156 | + def _(event: KeyPressEvent) -> None: |
| 157 | + buffer = event.current_buffer |
| 158 | + # If buffer is completely empty (no content at all), show error and don't allow newline |
| 159 | + if not buffer.text.strip(): |
| 160 | + # Show error message with prompt |
| 161 | + print( |
| 162 | + "\n\033[91m⚠ This field is required. Please enter some content or press Ctrl+C to abort.\033[0m" |
| 163 | + ) |
| 164 | + print("> ", end="", flush=True) |
| 165 | + # Don't do anything - require content first |
| 166 | + pass |
| 167 | + else: |
| 168 | + # If there's text, add new line |
| 169 | + buffer.newline() |
| 170 | + |
| 171 | + try: |
| 172 | + result = questionary.prompts.text.text( |
| 173 | + message=question["message"], |
| 174 | + multiline=True, |
| 175 | + style=cz.style, |
| 176 | + key_bindings=bindings, |
| 177 | + ).ask() |
| 178 | + |
| 179 | + field_name = question["name"] |
| 180 | + if result is None: |
| 181 | + result = "" |
| 182 | + |
| 183 | + # Apply filter if present |
| 184 | + if "filter" in question: |
| 185 | + result = question["filter"](result) |
| 186 | + |
| 187 | + answer = {field_name: result} |
| 188 | + answers.update(answer) |
| 189 | + |
| 190 | + except Exception: |
| 191 | + # Fallback to standard behavior if custom approach fails |
| 192 | + answer = questionary.prompt( |
| 193 | + [multiline_question], style=cz.style |
| 194 | + ) |
| 195 | + if not answer: |
| 196 | + raise NoAnswersError() |
| 197 | + answers.update(answer) |
101 | 198 | else: |
102 | 199 | try: |
103 | 200 | answer = questionary.prompt([question], style=cz.style) |
|
0 commit comments