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

ANSI colours for prompt title #107

Closed
ewels opened this issue Jan 14, 2021 · 2 comments
Closed

ANSI colours for prompt title #107

ewels opened this issue Jan 14, 2021 · 2 comments
Labels
Question Further information is requested

Comments

@ewels
Copy link

ewels commented Jan 14, 2021

Hi there,

Is it possible to give colours and formatting using ANSI codes to a questionary prompt title?

I am using the following code to generate prompts:

questionary.Choice(title=nice_title, value=raw_value)

I show the default value and other helpful information in the nice_title which is not passed through to the underlying response. This works brilliantly - I'd just like to be able to add some formatting to this.

Manual ANSI codes

I've tried manually adding ANSI codes to the string, or using libraries like rich but they are escaped and don't work:

myval  ^[[1;3;31m(False)^[[0m

Where I would like:

myval  (False)

..with (False) in bold-red text in this example.

Prompt toolkit

I dug around in the Questionary codebase a little and found that the title attribute is of class FormattedText, which is defined here:

# This is a cut-down version of `prompt_toolkit.formatted_text.AnyFormattedText`
# which does not exist in v2 of prompt_toolkit
FormattedText = Union[
str,
List[Tuple[str, str]],
List[Tuple[str, str, Callable[[Any], None]]],
None,
]

I feel like this is maybe getting close to something I could use, but I pretty much hit a wall at that point. I tried a bit of trial and error with passing lists of strings with stuff like class:selected but succeeded only in breaking stuff. Also I want to format only a partial section of the string so I'm not sure that this would work.

Any tips or pointers on how I might be able to achieve this?

Many thanks in advance,

Phil

@kiancross kiancross added the Question Further information is requested label Jan 14, 2021
@kiancross
Copy link
Collaborator

Hi Phil,

You're very close with FormattedText. I think this should do what you want:

import questionary

style = questionary.Style([
                     #fg:red <- These two are equivalent
    ("choice-title", "fg:#f00 bold"),
])

questionary.select("Choose something", [
    questionary.Choice(
        title=[("", "myval "), ("class:choice-title", "(false)")],
        value="myval",
    )
], style=style).ask()

@ewels
Copy link
Author

ewels commented Jan 14, 2021

Ah fantastic - thank you so much! I've been fighting on and off with this for a while now, your little example brings it all together!

Implemented and working great in no time once I had that pattern. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants