Skip to content

Commit 2999b55

Browse files
committed
feat(commit-input): enhance multiline input to behave like an editor
1 parent 108860f commit 2999b55

File tree

4 files changed

+15
-33
lines changed

4 files changed

+15
-33
lines changed

commitizen/commands/commit.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from commitizen import factory, git, out
1414
from commitizen.config import BaseConfig
1515
from commitizen.cz.exceptions import CzException
16-
from commitizen.cz.utils import get_backup_file_path, get_input_with_continuation
16+
from commitizen.cz.utils import get_backup_file_path
1717
from commitizen.exceptions import (
1818
CommitError,
1919
CommitMessageLengthExceededError,
@@ -82,14 +82,20 @@ def _prompt_commit_questions(self) -> str:
8282
if isinstance(root_err, CzException):
8383
raise CustomError(root_err.__str__())
8484
raise err
85-
elif question["type"] == "input" and question.get("continuation", False):
86-
print(f"\033[90m💡 Type backslash and press Enter for line continuation\033[0m")
87-
raw_answer = get_input_with_continuation(question["message"])
88-
if "filter" in question:
89-
processed_answer = question["filter"](raw_answer)
90-
else:
91-
processed_answer = raw_answer
92-
answers[question["name"]] = processed_answer
85+
elif question["type"] == "input" and question.get("multiline", False):
86+
print(f"\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")
87+
multiline_question = question.copy()
88+
multiline_question["multiline"] = True
89+
try:
90+
answer = questionary.prompt([multiline_question], style=cz.style)
91+
if not answer:
92+
raise NoAnswersError()
93+
answers.update(answer)
94+
except ValueError as err:
95+
root_err = err.__context__
96+
if isinstance(root_err, CzException):
97+
raise CustomError(root_err.__str__())
98+
raise err
9399
else:
94100
try:
95101
answer = questionary.prompt([question], style=cz.style)

commitizen/cz/utils.py

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,6 @@ def multiple_line_breaker(answer: str, sep: str = "|") -> str:
2020
return "\n".join(line.strip() for line in answer.split(sep) if line)
2121

2222

23-
def get_input_with_continuation(message: str) -> str:
24-
"""Get input with shell-like backslash line continuation."""
25-
lines = []
26-
prompt_msg = message + "\n"
27-
28-
while True:
29-
try:
30-
line = questionary.text(prompt_msg).ask()
31-
if line is None:
32-
return ""
33-
34-
if line.rstrip().endswith("\\"):
35-
lines.append(line.rstrip()[:-1].rstrip())
36-
prompt_msg = ">"
37-
else:
38-
lines.append(line)
39-
break
40-
41-
except KeyboardInterrupt:
42-
return ""
43-
44-
return "\n".join(lines)
4523

4624

4725
def strip_local_version(version: str) -> str:

commitizen/question.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class InputQuestion(TypedDict, total=False):
2222
filter: Callable[[str], str]
2323
multiline: bool
2424
default: str
25-
continuation: bool
2625

2726

2827
class ConfirmQuestion(TypedDict):

docs/customization.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,6 @@ commitizen:
176176
| `choices` | `list` | `None` | (OPTIONAL) The choices when `type = list` or `type = select`. Either use a list of values or a list of dictionaries with `name` and `value` keys. Keyboard shortcuts can be defined via `key`. See examples above. |
177177
| `default` | `Any` | `None` | (OPTIONAL) The default value for this question. |
178178
| `filter` | `str` | `None` | (OPTIONAL) Validator for user's answer. **(Work in Progress)** |
179-
| `multiline` | `bool` | `False` | (OPTIONAL) Enable multiline support when `type = input`. |
180179
| `use_search_filter` | `bool` | `False` | (OPTIONAL) Enable search/filter functionality for list/select type questions. This allows users to type and filter through the choices. |
181180
| `use_jk_keys` | `bool` | `True` | (OPTIONAL) Enable/disable j/k keys for navigation in list/select type questions. Set to false if you prefer arrow keys only. |
182181

0 commit comments

Comments
 (0)