Fluentty is a fluent command-line interface builder that runs on Deno. Here’s an example:
import { blue, green, red }
from "https://deno.land/std@0.76.0/fmt/colors.ts";
import { askYesNo, IO, ifYes, question }
from "https://raw.githubusercontent.com/matt-mcmahon/fluentty/v0.3.0/module.ts";
const name = await question("Choose your Knight:")
.accept(
"Arthur, King of the Britains",
"Sir Lancelot the Brave",
"Sir Robin the Not-Quite-So-Brave-as-Sir-Lancelot",
"Sir Bedevere the Wise",
"Sir Galahad the Pure",
"Sir Bors",
"Sir not Appearing in this Film",
)
.ignoreCase()
.matchPartial()
.retry()
.IO();
const approach = await askYesNo(`${name}, do you approach the bridge of death?`)
.IO()
.then(ifYes(answerTheQuestions));
async function answerTheQuestions() {
const questions = [
question("What is your name?")
.validate((input) => {
const re = new RegExp(input, "i");
return re.test(name) ? name : false;
}),
question("What is your quest?")
.retry()
.validate((input) => /grail/i.test(input) ? input : false)
.format((input) => input.replace(/[\!\.\?]?$/, "!")),
question("What is your favorite color?")
.suggest("red", "green").ignoreCase().matchFull()
.accept("blue").ignoreCase().matchFull()
.format((color) =>
color === "red"
? red("%s")
: color === "green"
? green(color)
: color === "blue"
? blue(color)
: color
),
question("African or European?")
.accept("African", "European").matchCase().matchInitial()
.sanitize((input) =>
input.substr(0, 1).toLocaleUpperCase() +
input.substr(1).toLocaleLowerCase()
)
.retry(),
];
const answers = await IO(...questions);
}
Which produces the following program:
bash : matt@matt-desktop:~/@mwm/fluentty
: [working ≡]
> deno run --unstable source/question.process.ts ↵
Choose your Knight: sir
Choose your Knight: sir b
Choose your Knight: lance
Choose your Knight: sir l
Sir Lancelot the Brave, do you approach the bridge of death?: (yes/no) y
What is your name: lance
What is your quest: I seek a shrubbery!
What is your quest: I seek the Holy Grail?
What is your favorite color: (red/green) blue
African or European: eu
And, finally, our user’s answers.
[
"Sir Lancelot the Brave",
"I seek the Holy Grail!",
"\u001b[34mblue\u001b[39m",
"European"
]
In this example we’re using the question
and IO
exports from Fluentty to ask the user a series of questions. Specifically:
Author: matt-mcmahon
Source Code: https://github.com/matt-mcmahon/fluentty
#deno #nodejs #node #javascript