menu icon indicating copy to clipboard operation
menu copied to clipboard

Public access to `prompt()`

Open bluespider42 opened this issue 1 year ago • 5 comments

In version 0.6 self.prompt() has moved inside the inner_runner which is not public. It was useful to have access to .prompt() to display the prompt after printing any information so that the user knows it is ready for input. Adding this to the runner impl is working for my purposes (not currently using noline). Can submit pr if wanted.

pub fn prompt(&mut self, newline:bool) {
        self.inner.prompt(&mut self.interface, newline);
    }

bluespider42 avatar Nov 05 '24 10:11 bluespider42

Seems reasonable, but doesn't the prompt print automatically?

thejpster avatar Nov 05 '24 19:11 thejpster

I guess if all the logic is handled within a command callback then yes. I find it useful to be able to send arbitrary messages, particularly from long running tasks. Unless I am missing the intended use.

write!(runner.interface, "My Message - Result = {}", val);

bluespider42 avatar Nov 06 '24 14:11 bluespider42

Ah, yes, mixing indications with command/response is always interesting. That kind of print might mix badly if the user is currently typing a command. Maybe we should offer a function that takes a closure, like:

menu.print_indication(|w| {
  writeln!(w, "+RING");
});
// When the closure ends, the prompt and the current command-line will be re-drawn.

thejpster avatar Nov 06 '24 14:11 thejpster

That would be a neater solution.

bluespider42 avatar Nov 06 '24 14:11 bluespider42

As a first attempt, this is working for my purposes.

pub fn print_indication<F, G>(&mut self, f: F) -> G
    where
        F: Fn(&mut I) -> G,
    {
        let g = f(&mut self.interface);
        let buffer = self.buffer.as_mut();
        if let Ok(s) = core::str::from_utf8(&buffer[0..self.used]) {
            write!(self.interface, "\r").unwrap();
            self.inner.prompt(&mut self.interface, false);
            write!(self.interface, "{}", s).unwrap();
        }
        g
    }

bluespider42 avatar Nov 06 '24 14:11 bluespider42