wduquette / molt

Embeddable TCL Interpreter for Rust applications

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Dynamic prompts for the REPL

dontlaugh opened this issue · comments

When starting up the REPL, we can only provide a static string at the moment.

let readline = rl.readline(prompt);

Would you accept a contribution that adds an additional REPL startup function like this (roughly, no idea what can make it compile yet 😄 )

type PromptFn = FnMut(&mut Interp) -> String;

pub fn repl_with_dynamic_prompt(interp: &mut Interp, prompt: Box<PromptFn>) { /* ... */ }

Access to context data would be especially nice.

Alternatively, you could introduce an enum for the prompts, if adding another function is messy.

PS: Great work on this interpreter. Your docs are really nice.

There's a better solution. The classic TCL approach is to have a variable called tcl_prompt1 that (if present) defines a TCL script that returns the prompt. The shell would:

  • Retrieve the variable's value
  • If it's undefined, output the default prompt
  • Otherwise, evaluate the variable's value as a script using Interp::eval and output the result as a string.

That lets you use any TCL command you might want to define, whether you defined it in Rust or in TCL; and naturally, that lets you use context data.

If you want to take a whack at it, feel free. Be aware, my current thrust involves changing the MoltResult type error case type from ResultCode to Exception, which means that how the shell will handle error messages changes a little bit; see the molt-err branch.

Extra credit would involve taking on #15 as well; the variable tcl_prompt2 is used to generate the prompt for continuation lines. But, you know, one thing at a time.

I've gotten something working against the current master branch.
Here's the commit: dontlaugh@b1cef56
And here's a little demo
image

I believe the unwrap is okay because we always yield an Ok(_) from or_else.

Let me know if this is the right approach. I could also rebase your molt-err branch and issue a PR against that branch.

It's a step in the right direction, but misses a couple of things. I left a comment on the commit.

The code you sketched out pretty much worked with a couple of changes. And then, since I removed the default prompt, a few tests had to be updated.

We might consider updating the book, although I'm not sure where's best to mention this feature.

Here, maybe: https://github.com/wduquette/molt/blob/master/molt-book/src/embed/shell.md

When continuation prompts are implemented, it might make sense to document tcl_promp1 and tcl_prompt2 together.

Yeah, it needs to go into the book where I talk about the shell; but also where I talk about using the moltsh app. But don't you worry about it; I'll attend to the book after I merge the PR you're gonna send me. (I'm kind of passionate about the docs, as you've noticed. :-)

Should have closed this on Thursday. Thanks to @dontlaugh!

FYI, I've merged this change onto the molt-err branch, where I'm currently working; I'll make the doc changes as part of that effort.