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

feat: Dynamic String Prompt for REPL #624

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading