Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing in a custom lexer #70

Merged
merged 1 commit into from
Oct 14, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions questionary/prompts/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from prompt_toolkit.document import Document
from prompt_toolkit.shortcuts.prompt import PromptSession
from prompt_toolkit.styles import Style, merge_styles
from prompt_toolkit.lexers import SimpleLexer
from prompt_toolkit.lexers import Lexer, SimpleLexer

from questionary.constants import (
DEFAULT_QUESTION_PREFIX,
Expand All @@ -24,6 +24,7 @@ def text(
style: Optional[Style] = None,
multiline: bool = False,
instruction: Optional[Text] = None,
lexer: Optional[Lexer] = None,
**kwargs: Any,
) -> Question:
"""Prompt the user to enter a free text message.
Expand Down Expand Up @@ -55,12 +56,15 @@ def text(
instruction: Write instructions for the user if needed. If `None`
and `multiline=True`, some instructions will appear.

lexer: Supply a valid lexer to style the answer. Leave empty to
use a simple one by default.

Returns:
Question: Question instance, ready to be prompted (using `.ask()`).
"""

merged_style = merge_styles([DEFAULT_STYLE, style])

lexer = lexer or SimpleLexer("class:answer")
validator = build_validator(validate)

if instruction is None and multiline:
Expand All @@ -76,7 +80,7 @@ def get_prompt_tokens() -> List[Tuple[Text, Text]]:
get_prompt_tokens,
style=merged_style,
validator=validator,
lexer=SimpleLexer("class:answer"),
lexer=lexer,
multiline=multiline,
**kwargs,
)
Expand Down