Skip to content

Commit

Permalink
Allow passing in a custom lexer (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
yajo authored Oct 14, 2020
1 parent bbdfad5 commit 2efc8ba
Showing 1 changed file with 7 additions and 3 deletions.
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

0 comments on commit 2efc8ba

Please sign in to comment.