An interactive terminal console with a persistent input line >>, scrolling message output above that line, and command registration support.
The library has only 3 fixed base colors for built-in output levels:
BaseColor::LogBaseColor::OkBaseColor::Err
The base colors themselves are immutable and cannot be extended or overridden. For custom formats, there is a separate CustomColor palette from which you can build any number of output schemes.
Available CustomColor:
Black,Red,Green,Yellow,Blue,Magenta,Cyan,WhiteBrightBlack,BrightRed,BrightGreen,BrightYellow,BrightBlue,BrightMagenta,BrightCyan,BrightWhite
Each format can include:
bolditalicdimunderlinereversestrike- a color from
CustomColor
Formats are registered by name and then used in output macros and functions. The base log/ok/err remain fixed and cannot be changed via register_output_format.
[dependencies]
consoletools = { path = "." }use consoletools::{
cprintln_err, cprintln_fmt, cprintln_log, cprintln_ok, install_global_console_handle,
register_output_format, CommandConsole, CommandOutput, CustomColor, TextFormat,
};
use std::thread;
use std::time::Duration;
fn main() {
let mut console = CommandConsole::new(">>");
install_global_console_handle(console.handle());
let _ = register_output_format("prompt", TextFormat::custom(CustomColor::BrightCyan).bold(true).underline(true));
let _ = register_output_format("event", TextFormat::custom(CustomColor::BrightMagenta).italic(true).underline(true));
let _ = register_output_format("warning", TextFormat::custom(CustomColor::BrightYellow).bold(true).reverse(true));
let _ = register_output_format("accent", TextFormat::custom(CustomColor::BrightBlue).bold(true).strike(true));
let _ = register_output_format("note", TextFormat::custom(CustomColor::BrightGreen).dim(true).italic(true));
thread::spawn(move || {
let mut i = 1_u64;
loop {
cprintln_log!("tick {}", i);
if i % 5 == 0 {
cprintln_ok!("ok {}", i);
}
if i % 9 == 0 {
cprintln_err!("err {}", i);
}
if i % 7 == 0 {
cprintln_fmt!("event", "custom event {}", i);
}
if i % 13 == 0 {
cprintln_fmt!("accent", "accent {}", i);
}
i += 1;
thread::sleep(Duration::from_secs(1));
}
});
console.add_command("echo", "Print the provided text", |args| {
if args.is_empty() {
CommandOutput::Error("Usage: echo <text>".to_string())
} else {
CommandOutput::Info(args.join(" "))
}
});
console.add_command("sum", "Sum numbers", |args| {
let mut total = 0.0_f64;
for arg in args {
match arg.parse::<f64>() {
Ok(v) => total += v,
Err(_) => return CommandOutput::Error(format!("Not a number: {}", arg)),
}
}
CommandOutput::Success(format!("Sum: {}", total))
});
if let Err(err) = console.run() {
eprintln!("Console error: {}", err);
}
}CommandConsole::new(prompt)- create a consoleCommandConsole::add_command(name, description, handler)- register a commandCommandConsole::handle()- get a thread-safe handle for background threadsCommandConsole::run()- start the input and output loopCommandConsole::save_to_file(path)- save the current console log to a fileCommandConsole::enable_autosave(path)- enable dynamic saving of new linesCommandConsole::disable_autosave()- disable dynamic savinginstall_global_console_handle(handle)- enable output macrosregister_output_format(name, format) -> bool- create or replace a custom format (returnsfalseforlog/ok/err)TextFormat::new(BaseColor::...)- create a format based on one of the 3 fixed colorsTextFormat::custom(CustomColor::...)- create a format based on the extended paletteTextFormat::bold/italic/dim/underline/reverse/strike(...)- configure stylesconsole_write_log/ok/err(text)- direct output by levelconsole_write_format(name, text)- output through any registered formatcprintln_log!,cprintln_ok!,cprintln_err!- output macros by levelcprintln_fmt!(name, ...)- output macro through any formatcprint!- thread-safe output without line break
help- show helpclear- clear the logsave <path/filename>- save the current log to a fileautosave on <path/filename>- enable dynamic savingautosave off- disable dynamic savingexitorquit- exit
- Before using
cprint!andcprintln_*macros, you need to callinstall_global_console_handleonce. - Base colors
log/ok/errare fixed and can only be selected viaBaseColor::Log,BaseColor::Ok, andBaseColor::Err. - For custom formats, use
CustomColor,register_output_format, andcprintln_fmt!. - The
promptformat can be overridden viaregister_output_format("prompt", ...). - The
savecommand saves the log to a text file and automatically creates missing directories. - The
autosave oncommand first saves the entire current log, then automatically appends each new line.