wduquette / molt

Embeddable TCL Interpreter for Rust applications
BSD 3-Clause "New" or "Revised" License
104 stars 13 forks source link

Dynamic prompts for the REPL #87

Closed dontlaugh closed 4 years ago

dontlaugh commented 4 years ago

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

https://github.com/wduquette/molt/blob/d58531cb4ad101fc71913a0744c959069809d43d/molt-shell/src/shell.rs#L36

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

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.

wduquette commented 4 years ago

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:

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.

dontlaugh commented 4 years ago

I've gotten something working against the current master branch. Here's the commit: https://github.com/dontlaugh/molt/commit/b1cef56a438a782a4ac9083b5fb7e4e698b06ef8 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.

wduquette commented 4 years ago

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

dontlaugh commented 4 years ago

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.

wduquette commented 4 years ago

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. :-)

wduquette commented 4 years ago

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

wduquette commented 4 years ago

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.