Is it possible to render markdown in a prompt?
# clikt
p
Is it possible to render markdown in a prompt?
s
As of clikt 4 it should be, as it uses Mordant underneath, we in turn can render Markdown: https://github.com/ajalt/mordant/#markdown
p
I can't find an easily accessible API to use this in prompts. Do you have an idea, how to?
a
Sure, you can just use a Mordant prompt directly:
Copy code
class MarkdownPrompt(
    prompt: String, terminal: Terminal
): Prompt<String>(prompt, terminal) {
    override fun makePrompt(): Widget = Markdown(prompt)
    override fun convert(input: String) = ConversionResult.Valid(input)
}
MarkdownPrompt("*prompt*", terminal).ask()
p
Ah that's cool!
What about adding a useMarkdown param to the prompt function itself?
a
Hm, I doubt most people will use markdown in a prompt, so I'm not sure it's worth adding another configuration flag. I'm curious what kind of markdown you're looking to use? I expect most prompts will be something simple like "enter a string:"
p
Both in echo and prompt. But in echo actually more
a
Nice use of color and emojii, that looks great!
echo
just forwards to
terminal.println
, so you could e.g. make an extension like
Copy code
fun CliktCommand.echoMarkdown(message: String) {
    currentContext.terminal.println(Markdown(message))
}
and use that
p
I'll try that, thanks! That color is actually fake. It's one of these terminal screenshot websites. Im on mobile and just pasted it from written notion docs ^^
Originally I had but that got clipped in monospace
Btw I really really love clikt, it makes so much sense
❤️ 1