Crates.io | dbgcmd |
lib.rs | dbgcmd |
version | 0.1.0 |
source | src |
created_at | 2020-01-14 16:17:12.316219 |
updated_at | 2020-01-14 16:17:12.316219 |
description | 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. |
homepage | |
repository | https://github.com/mistodon/dbgcmd |
max_upload_size | |
id | 198348 |
size | 21,326 |
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.
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!"),
}
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());