mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Rename toggle_debug_view -> toggle_debug_console
This commit is contained in:
parent
3a7e10cffe
commit
d7e57fd280
24
examples/debug_console.rs
Normal file
24
examples/debug_console.rs
Normal 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();
|
||||||
|
}
|
@ -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();
|
|
||||||
}
|
|
@ -14,6 +14,8 @@ use vec::Vec2;
|
|||||||
use view::{self, Finder, IntoBoxedView, Position, View};
|
use view::{self, Finder, IntoBoxedView, Position, View};
|
||||||
use views::{self, LayerPosition};
|
use views::{self, LayerPosition};
|
||||||
|
|
||||||
|
static DEBUG_VIEW_ID: &'static str = "_cursive_debug_view";
|
||||||
|
|
||||||
/// Central part of the cursive library.
|
/// Central part of the cursive library.
|
||||||
///
|
///
|
||||||
/// It initializes ncurses on creation and cleans up on drop.
|
/// It initializes ncurses on creation and cleans up on drop.
|
||||||
@ -182,20 +184,26 @@ impl Cursive {
|
|||||||
Self::new(backend::dummy::Backend::init)
|
Self::new(backend::dummy::Backend::init)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Show the debug view, or hide it if it's already visible.
|
/// Show the debug console.
|
||||||
pub fn toggle_debug_view(&mut self) {
|
///
|
||||||
static DEBUG_VIEW_ID: &'static str = "_cursive_debug_view";
|
/// 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();
|
/// Show the debug console, or hide it if it's already visible.
|
||||||
if let Some(pos) = stack.find_layer_from_id(DEBUG_VIEW_ID) {
|
pub fn toggle_debug_console(&mut self) {
|
||||||
stack.remove_layer(pos);
|
if let Some(pos) = self.screen_mut().find_layer_from_id(DEBUG_VIEW_ID)
|
||||||
|
{
|
||||||
|
self.screen_mut().remove_layer(pos);
|
||||||
} else {
|
} else {
|
||||||
stack.add_layer(
|
self.show_debug_console();
|
||||||
views::Dialog::around(views::ScrollView::new(
|
|
||||||
views::IdView::new(DEBUG_VIEW_ID, views::DebugView::new()),
|
|
||||||
))
|
|
||||||
.title("Debug console"),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -35,6 +35,18 @@ pub enum BaseColor {
|
|||||||
White,
|
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 {
|
impl From<u8> for BaseColor {
|
||||||
fn from(n: u8) -> Self {
|
fn from(n: u8) -> Self {
|
||||||
match n % 8 {
|
match n % 8 {
|
||||||
|
@ -8,8 +8,7 @@ use unicode_width::UnicodeWidthStr;
|
|||||||
|
|
||||||
/// View used for debugging, showing logs.
|
/// View used for debugging, showing logs.
|
||||||
pub struct DebugView {
|
pub struct DebugView {
|
||||||
// We'll want to store the formatting (line split)
|
// TODO: wrap log lines if needed, and save the line splits here.
|
||||||
// ... or 1 line per log?
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DebugView {
|
impl DebugView {
|
||||||
@ -27,6 +26,7 @@ impl View for DebugView {
|
|||||||
|
|
||||||
for (i, record) in logs.iter().skip(skipped).enumerate() {
|
for (i, record) in logs.iter().skip(skipped).enumerate() {
|
||||||
// TODO: Apply style to message? (Ex: errors in bold?)
|
// TODO: Apply style to message? (Ex: errors in bold?)
|
||||||
|
// TODO: customizable time format? (24h/AM-PM)
|
||||||
printer.print(
|
printer.print(
|
||||||
(0, i),
|
(0, i),
|
||||||
&format!(
|
&format!(
|
||||||
@ -36,11 +36,11 @@ impl View for DebugView {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
let color = match record.level {
|
let color = match record.level {
|
||||||
log::Level::Error => theme::BaseColor::Red,
|
log::Level::Error => theme::BaseColor::Red.dark(),
|
||||||
log::Level::Warn => theme::BaseColor::Yellow,
|
log::Level::Warn => theme::BaseColor::Yellow.dark(),
|
||||||
log::Level::Info => theme::BaseColor::Black,
|
log::Level::Info => theme::BaseColor::Black.light(),
|
||||||
log::Level::Debug => theme::BaseColor::Green,
|
log::Level::Debug => theme::BaseColor::Green.dark(),
|
||||||
log::Level::Trace => theme::BaseColor::Blue,
|
log::Level::Trace => theme::BaseColor::Blue.dark(),
|
||||||
};
|
};
|
||||||
printer.with_color(color.into(), |printer| {
|
printer.with_color(color.into(), |printer| {
|
||||||
printer.print((16, i), &format!("{:5}", record.level))
|
printer.print((16, i), &format!("{:5}", record.level))
|
||||||
@ -54,7 +54,8 @@ impl View for DebugView {
|
|||||||
|
|
||||||
let level_width = 8; // Width of "[ERROR] "
|
let level_width = 8; // Width of "[ERROR] "
|
||||||
let time_width = 16; // Width of "23:59:59.123 | "
|
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
|
let w = logs
|
||||||
.iter()
|
.iter()
|
||||||
.map(|record| record.message.width() + level_width + time_width)
|
.map(|record| record.message.width() + level_width + time_width)
|
||||||
|
Loading…
Reference in New Issue
Block a user