Skip to content
Simple model for a debug console, intended mostly for games
Rust Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src
.gitignore
.travis.yml
Cargo.toml
DEV.md
README.md
run_tests

README.md

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());
You can’t perform that action at this time.