Rename toggle_debug_view -> toggle_debug_console

This commit is contained in:
Alexandre Bury 2019-02-22 15:02:15 -08:00
parent 3a7e10cffe
commit d7e57fd280
5 changed files with 65 additions and 37 deletions

24
examples/debug_console.rs Normal file
View File

@ -0,0 +1,24 @@
extern crate cursive;
#[macro_use]
extern crate log;
fn main() {
// Initialize the cursive logger.
cursive::logger::init();
// Use some logging macros from the `log` crate.
error!("Something serious probably happened!");
warn!("Or did it?");
debug!("Logger initialized.");
info!("Starting!");
let mut siv = cursive::Cursive::default();
siv.add_layer(cursive::views::Dialog::text("Press ~ to open the console.\nPress l to generate logs.\nPress q to quit."));
siv.add_global_callback('q', cursive::Cursive::quit);
siv.add_global_callback('~', cursive::Cursive::toggle_debug_console);
siv.add_global_callback('l', |_| trace!("Wooo"));
siv.run();
}

View File

@ -1,17 +0,0 @@
extern crate cursive;
#[macro_use]
extern crate log;
fn main() {
cursive::logger::init();
debug!("Starting!");
let mut siv = cursive::Cursive::default();
siv.add_global_callback('q', cursive::Cursive::quit);
siv.add_global_callback('~', cursive::Cursive::toggle_debug_view);
siv.add_global_callback('l', |_| warn!("Wooo"));
error!("BAD!!!");
siv.run();
}

View File

@ -14,6 +14,8 @@ use vec::Vec2;
use view::{self, Finder, IntoBoxedView, Position, View};
use views::{self, LayerPosition};
static DEBUG_VIEW_ID: &'static str = "_cursive_debug_view";
/// Central part of the cursive library.
///
/// It initializes ncurses on creation and cleans up on drop.
@ -182,20 +184,26 @@ impl Cursive {
Self::new(backend::dummy::Backend::init)
}
/// Show the debug view, or hide it if it's already visible.
pub fn toggle_debug_view(&mut self) {
static DEBUG_VIEW_ID: &'static str = "_cursive_debug_view";
/// Show the debug console.
///
/// Currently, this will show logs if [`logger::init()`] was called.
pub fn show_debug_console(&mut self) {
self.add_layer(
views::Dialog::around(views::ScrollView::new(views::IdView::new(
DEBUG_VIEW_ID,
views::DebugView::new(),
)))
.title("Debug console"),
);
}
let stack = self.screen_mut();
if let Some(pos) = stack.find_layer_from_id(DEBUG_VIEW_ID) {
stack.remove_layer(pos);
/// Show the debug console, or hide it if it's already visible.
pub fn toggle_debug_console(&mut self) {
if let Some(pos) = self.screen_mut().find_layer_from_id(DEBUG_VIEW_ID)
{
self.screen_mut().remove_layer(pos);
} else {
stack.add_layer(
views::Dialog::around(views::ScrollView::new(
views::IdView::new(DEBUG_VIEW_ID, views::DebugView::new()),
))
.title("Debug console"),
);
self.show_debug_console();
}
}

View File

@ -35,6 +35,18 @@ pub enum BaseColor {
White,
}
impl BaseColor {
/// Returns the regular (dark) version of this base color.
pub fn dark(self) -> Color {
Color::Dark(self)
}
/// Returns the light version of this base color.
pub fn light(self) -> Color {
Color::Light(self)
}
}
impl From<u8> for BaseColor {
fn from(n: u8) -> Self {
match n % 8 {

View File

@ -8,8 +8,7 @@ use unicode_width::UnicodeWidthStr;
/// View used for debugging, showing logs.
pub struct DebugView {
// We'll want to store the formatting (line split)
// ... or 1 line per log?
// TODO: wrap log lines if needed, and save the line splits here.
}
impl DebugView {
@ -27,6 +26,7 @@ impl View for DebugView {
for (i, record) in logs.iter().skip(skipped).enumerate() {
// TODO: Apply style to message? (Ex: errors in bold?)
// TODO: customizable time format? (24h/AM-PM)
printer.print(
(0, i),
&format!(
@ -36,11 +36,11 @@ impl View for DebugView {
),
);
let color = match record.level {
log::Level::Error => theme::BaseColor::Red,
log::Level::Warn => theme::BaseColor::Yellow,
log::Level::Info => theme::BaseColor::Black,
log::Level::Debug => theme::BaseColor::Green,
log::Level::Trace => theme::BaseColor::Blue,
log::Level::Error => theme::BaseColor::Red.dark(),
log::Level::Warn => theme::BaseColor::Yellow.dark(),
log::Level::Info => theme::BaseColor::Black.light(),
log::Level::Debug => theme::BaseColor::Green.dark(),
log::Level::Trace => theme::BaseColor::Blue.dark(),
};
printer.with_color(color.into(), |printer| {
printer.print((16, i), &format!("{:5}", record.level))
@ -54,7 +54,8 @@ impl View for DebugView {
let level_width = 8; // Width of "[ERROR] "
let time_width = 16; // Width of "23:59:59.123 | "
// The longest line sets the width
// The longest line sets the width
let w = logs
.iter()
.map(|record| record.message.width() + level_width + time_width)