Enum liso::Response

source ·
#[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:

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
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

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

source

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.

Trait Implementations§

source§

impl Debug for Response

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.