Skip to content

Commit d8ba22d

Browse files
committed
refactor(preview): simplify preview max length handling
1 parent bc14ba7 commit d8ba22d

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

commitizen/interactive_preview.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def make_toolbar_content(
4242
current_field: str,
4343
current_text: str,
4444
*,
45-
max_length: int | None,
45+
max_length: int,
4646
) -> str:
4747
"""Build bottom toolbar content with live preview and length counter.
4848
@@ -53,10 +53,11 @@ def make_toolbar_content(
5353
preview = subject_builder(current_field, current_text)
5454
current_length = len(preview)
5555

56-
if max_length is not None and max_length > 0:
57-
counter = f"{current_length}/{max_length} chars"
58-
else:
59-
counter = f"{current_length} chars"
56+
counter = (
57+
f"{current_length}/{max_length} chars"
58+
if max_length > 0
59+
else f"{current_length} chars"
60+
)
6061

6162
try:
6263
width = get_terminal_size().columns
@@ -76,19 +77,19 @@ def make_length_validator(
7677
subject_builder: SubjectBuilder,
7778
field: str,
7879
*,
79-
max_length: int | None,
80+
max_length: int,
8081
) -> Callable[[str], bool | str]:
8182
"""Create a questionary-style validator for subject length.
8283
8384
The validator:
8485
- Uses the subject_builder to get the full preview string for the current
8586
answers_state and field value.
86-
- Applies max_length on the character count (len). A value of 0 or None
87-
disables the limit.
87+
- Applies max_length on the character count (len). A value of 0 disables
88+
the limit.
8889
"""
8990

9091
def _validate(text: str) -> bool | str:
91-
if not max_length or max_length <= 0:
92+
if max_length <= 0:
9293
return True
9394

9495
preview = subject_builder(field, text)

commitizen/preview_questions.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def build_preview_questions(
4242
for q in questions
4343
if isinstance(q.get("name"), str)
4444
}
45-
field_filters: dict[str, Any] = {
45+
field_filters: dict[str, Callable[[str], str] | None] = {
4646
q["name"]: q.get("filter") for q in questions if isinstance(q.get("name"), str)
4747
}
4848
answers_state: dict[str, Any] = {}
@@ -100,7 +100,7 @@ def make_length_validator(name: str) -> Callable[[str], bool | str]:
100100
max_length=max_preview_length,
101101
)
102102

103-
enhanced_questions: list[dict[str, object]] = []
103+
enhanced_questions: list[CzQuestion] = []
104104
for q in questions:
105105
q_dict = q.copy()
106106
q_type = q_dict.get("type")

0 commit comments

Comments
 (0)