diff --git a/examples/debug_console.rs b/examples/debug_console.rs new file mode 100644 index 0000000..9f1fdcf --- /dev/null +++ b/examples/debug_console.rs @@ -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(); +} diff --git a/examples/logger.rs b/examples/logger.rs deleted file mode 100644 index fa9722a..0000000 --- a/examples/logger.rs +++ /dev/null @@ -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(); -} diff --git a/src/cursive.rs b/src/cursive.rs index e3881ad..974c3f7 100644 --- a/src/cursive.rs +++ b/src/cursive.rs @@ -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(); } } diff --git a/src/theme/color.rs b/src/theme/color.rs index e71d006..325ea3d 100644 --- a/src/theme/color.rs +++ b/src/theme/color.rs @@ -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 for BaseColor { fn from(n: u8) -> Self { match n % 8 { diff --git a/src/views/debug_view.rs b/src/views/debug_view.rs index f4305fe..286569f 100644 --- a/src/views/debug_view.rs +++ b/src/views/debug_view.rs @@ -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)