[][src]Crate dbgcmd

This is a simple library for implementing command-line-style debug consoles within an application.

It doesn't handle rendering, or the logic of any individual commands. All it does is model the state of the console, including command history, and whether or not it is active. You define your own command struct/enum, and drive the inputs to the console. After that, you can use the confirm method to parse the text entered so far into a command.

When running in debug mode (when the debug_assertions flag is set) the entire Console is disabled. This is to avoid shipping something and accidentally including the debug console.

To override this, you can use the force-enabled feature, which will allow the console to work in release mode.

Examples

Input

use std::str::FromStr;
use dbgcmd::Console;

#[derive(Debug, PartialEq, Eq)]
enum Command {
    SayHi,
    SayBye,
}

impl FromStr for Command {
    type Err = ();

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "hi" => Ok(Command::SayHi),
            "bye" => Ok(Command::SayBye),
            _ => Err(())
        }
    }
}

let mut console = Console::new();

// Your application passes along key inputs
console.receive_char('h');
console.receive_char('i');

if console.enabled() {
    assert_eq!(console.confirm(), Ok(Command::SayHi));
}

History

use dbgcmd::Console;

type Command = String;

let mut console = Console::new();

// Set the entire entry instead of individual characters
console.set_entry("first".to_owned());
console.confirm::<Command>();

console.set_entry("second".to_owned());
console.confirm::<Command>();

console.set_entry("third".to_owned());
console.confirm::<Command>();

if console.enabled() {
    // Scroll up or down the history
    console.up();
    assert_eq!(console.entry(), "third".to_owned());

    console.up();
    assert_eq!(console.entry(), "second".to_owned());

    console.up();
    assert_eq!(console.entry(), "first".to_owned());

    console.down();
    console.down();
    console.down();

    assert!(console.entry().is_empty());
}

Structs

Console