#[non_exhaustive]pub enum Response {
Input(String),
Dead,
Quit,
Discarded(String),
Finish,
Info,
Break,
Escape,
Swap,
Custom(Box<dyn Any + Send>),
Unknown(u8),
}
Expand description
Input received from the user, or a special condition. Returned by any of
the following InputOutput
methods:
read_async
(asynchronous)read_blocking
(synchronous, waiting forever)read_timeout
(synchronous with timeout)read_deadline
(synchronous with deadline)try_read
(polled)
Example usage:
// near the top
io.prompt(liso!(fg=green, bold, "> ", reset), true, false);
// in your main loop
match response {
Response::Input(line) => {
io.echoln(liso!(fg=green, dim, "> ", fg=none, &line));
match line.as_str() {
"hello" => io.println("World!"),
"world" => io.println("Hello!"),
_ => io.println("何って?"),
}
},
Response::Discarded(line) => {
io.echoln(liso!(bold+dim, "X ", -bold, line));
},
Response::Dead => return,
Response::Quit => break,
// (handle any other variants you want)
other => {
io.notice(format!("unknown key {}",
other.as_unknown() as char),
Duration::from_secs(1));
},
}
Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Input(String)
Sent when the user finishes entering a line of input. This is the entire line. This is the most interesting, and common, variant that you will receive.
In case you don’t want to do in-depth parsing of the user’s input, you
can match against static string literals with a little work. You may
also want to use echoln
to echo
the user’s input. See the top of this documentation for an example of
both.
Dead
Sent when the terminal or the IO thread have died. Once you receive
this once, you will never receive any other Response
from Liso again.
Your program should exit soon after, or at the very least should close
down that InputOutput
instance.
If your program receives Response::Dead
on the same InputOutput
instance too many times, Liso will panic. This is to ensure that even
a poorly-written program that ignores Response::Dead
will still exit
soon after after user input is permanently cut off, whether by a hangup
condition or by a bug in Liso.
Quit
Sent when the user types control-C, which normally means they want your program to quit.
Discarded(String)
Sent when the user types control-G, discarding their current input. The
passed string is what the state of their input was when they hit
control-G. You should pass this to echoln
, along with some kind of
feedback that the input was discarded.
Finish
Sent when the user types control-D on an empty line, which normally means that they are done providing input (possibly temporarily).
Info
Sent when the user types control-T, which on some BSDs is a standard way to request that a program give a status report or other progress information.
Break
Sent when the user types control-backslash, or when a break condition is detected. The meaning of this is application-specific. If you’re running on a real, physical terminal line, this usually indicates an excessively noisy line, or a disconnect (“break”) in the line.
Escape
Sent when the user presses Escape.
Swap
Sent when the user presses control-X.
Custom(Box<dyn Any + Send>)
Sent whenever send_custom
is called. This can be used to interrupt
the input thread when it’s doing a read_blocking
call.
Unknown(u8)
Sent when the user presses an unknown control character with the given value (which will be between 0 and 31 inclusive).
Don’t use particular values of Unknown
for any specific purpose.
Later versions of Liso may add additional Response
variants for new
control keys, or handle more control keys itself, replacing the
Unknown(...)
values those keys used to send. See the top of this file
for an example of how this variant should be used (i.e. not directly).
Implementations§
source§impl Response
impl Response
sourcepub fn as_unknown(&self) -> u8
pub fn as_unknown(&self) -> u8
Returns the control code that triggered this response, e.g. 10 for
Input
, 3 for Quit
, … Use this to produce a generic “unknown key
key ^X” kind of message for any Response
variants you don’t handle,
perhaps with code like. See the top of this file for an example.