#console #debugging #command #applications #command-line #model #individual

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.

1 unstable release

0.1.0 Jan 14, 2020

#1201 in GUI

CC0 license

21KB
345 lines

dbgcmd

Build Status Crates.io Docs.rs

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');

match console.confirm().unwrap() {
    Command::SayHi => println!("Hi!"),
    Command::SayBye => println!("Bye!"),
}

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>();

// 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());

Dependencies

~460KB