Skip to content

Commit

Permalink
feat: Dynamic String Prompt for REPL
Browse files Browse the repository at this point in the history
  • Loading branch information
khill-fbmc committed Jul 4, 2024
1 parent 24857ed commit fcf07c1
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
21 changes: 21 additions & 0 deletions examples/repl-dynamic-prompt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { command, program } from "../src/index.js";

const app = program({
prompt: () => `[${new Date().toLocaleTimeString()}] > `,
});

async function rng(bounds: [number, number]) {
const [min, max] = bounds;
return Math.floor(Math.random() * (max - min + 1)) + min;
}

const dice = app.add(

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable dice.
command("roll")
.option("min", { default: 1 })
.option("max", { default: 6 })
.action(async (args) => {
console.log(await rng([args.min, args.max]));
}),
);

app.runOrRepl();
2 changes: 1 addition & 1 deletion src/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type ProgramOptions = {
*
* Defaults to `> `.
*/
prompt?: string;
prompt?: string | (() => string);

/**
* Whether or not to add a global help command that displays an overview of
Expand Down
3 changes: 2 additions & 1 deletion src/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ export class Repl {
* @private
*/
public async start() {
const { prompt } = this.program.options;
this.server = nodeRepl.start({
prompt: this.program.options.prompt,
prompt: typeof prompt === "function" ? prompt() : prompt,
eval: this.eval.bind(this),
completer: this.completer.bind(this),
ignoreUndefined: true,
Expand Down

0 comments on commit fcf07c1

Please sign in to comment.