mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-09 19:00:46 +00:00
Update debug view
This commit is contained in:
parent
8b3f5a8de7
commit
3a7e10cffe
@ -30,6 +30,7 @@ libc = "0.2.47"
|
||||
term_size = { version = "0.3.1", optional = true }
|
||||
crossbeam-channel = "0.3.6"
|
||||
lazy_static = "1.2.0"
|
||||
chrono = "0.4.6"
|
||||
|
||||
[dependencies.num]
|
||||
default-features = false
|
||||
|
@ -10,7 +10,7 @@ fn main() {
|
||||
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', |_| debug!("Wooo"));
|
||||
siv.add_global_callback('l', |_| warn!("Wooo"));
|
||||
error!("BAD!!!");
|
||||
|
||||
siv.run();
|
||||
|
@ -188,7 +188,6 @@ impl Cursive {
|
||||
|
||||
let stack = self.screen_mut();
|
||||
if let Some(pos) = stack.find_layer_from_id(DEBUG_VIEW_ID) {
|
||||
info!("Foundit");
|
||||
stack.remove_layer(pos);
|
||||
} else {
|
||||
stack.add_layer(
|
||||
|
@ -81,6 +81,7 @@ extern crate maplit;
|
||||
#[cfg(unix)]
|
||||
extern crate signal_hook;
|
||||
|
||||
extern crate chrono;
|
||||
extern crate libc;
|
||||
extern crate num;
|
||||
extern crate owning_ref;
|
||||
|
@ -10,9 +10,19 @@ struct CursiveLogger;
|
||||
|
||||
static LOGGER: CursiveLogger = CursiveLogger;
|
||||
|
||||
/// A log record.
|
||||
pub struct Record {
|
||||
/// Log level used for this record
|
||||
pub level: log::Level,
|
||||
/// Time this message was logged
|
||||
pub time: chrono::DateTime<chrono::Utc>,
|
||||
/// Message content
|
||||
pub message: String,
|
||||
}
|
||||
|
||||
lazy_static! {
|
||||
/// Circular buffer for logs. Use it to implement `DebugView`.
|
||||
pub static ref LOGS: Mutex<VecDeque<(log::Level, String)>> =
|
||||
pub static ref LOGS: Mutex<VecDeque<Record>> =
|
||||
Mutex::new(VecDeque::new());
|
||||
}
|
||||
|
||||
@ -27,7 +37,11 @@ impl log::Log for CursiveLogger {
|
||||
if logs.len() == logs.capacity() {
|
||||
logs.pop_front();
|
||||
}
|
||||
logs.push_back((record.level(), format!("{}", record.args())));
|
||||
logs.push_back(Record {
|
||||
level: record.level(),
|
||||
message: format!("{}", record.args()),
|
||||
time: chrono::Utc::now(),
|
||||
});
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
|
@ -121,7 +121,7 @@ impl Color {
|
||||
/// * `"light green"` becomes `Color::Light(BaseColor::Green)`
|
||||
/// * `"default"` becomes `Color::TerminalDefault`
|
||||
/// * `"#123456"` becomes `Color::Rgb(0x12, 0x34, 0x56)`
|
||||
pub(crate) fn parse(value: &str) -> Option<Self> {
|
||||
pub fn parse(value: &str) -> Option<Self> {
|
||||
Some(match value {
|
||||
"black" => Color::Dark(BaseColor::Black),
|
||||
"red" => Color::Dark(BaseColor::Red),
|
||||
|
@ -1,4 +1,4 @@
|
||||
use super::{Color, ColorPair, Palette, PaletteColor};
|
||||
use super::{BaseColor, Color, ColorPair, Palette, PaletteColor};
|
||||
|
||||
/// Possible color style for a cell.
|
||||
///
|
||||
@ -91,6 +91,12 @@ impl From<Color> for ColorStyle {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BaseColor> for ColorStyle {
|
||||
fn from(color: BaseColor) -> Self {
|
||||
Self::new(Color::Dark(color), PaletteColor::View)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<PaletteColor> for ColorStyle {
|
||||
fn from(color: PaletteColor) -> Self {
|
||||
Self::new(color, PaletteColor::View)
|
||||
|
@ -1,4 +1,5 @@
|
||||
use logger;
|
||||
use theme;
|
||||
use vec::Vec2;
|
||||
use view::View;
|
||||
use Printer;
|
||||
@ -24,8 +25,26 @@ impl View for DebugView {
|
||||
// Only print the last logs, so skip what doesn't fit
|
||||
let skipped = logs.len().saturating_sub(printer.size.y);
|
||||
|
||||
for (i, &(level, ref text)) in logs.iter().skip(skipped).enumerate() {
|
||||
printer.print((0, i), &format!("[{}] {}", level, text));
|
||||
for (i, record) in logs.iter().skip(skipped).enumerate() {
|
||||
// TODO: Apply style to message? (Ex: errors in bold?)
|
||||
printer.print(
|
||||
(0, i),
|
||||
&format!(
|
||||
"{} | [ ] {}",
|
||||
record.time.with_timezone(&chrono::Local).format("%T%.3f"),
|
||||
record.message
|
||||
),
|
||||
);
|
||||
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,
|
||||
};
|
||||
printer.with_color(color.into(), |printer| {
|
||||
printer.print((16, i), &format!("{:5}", record.level))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@ -33,10 +52,12 @@ impl View for DebugView {
|
||||
// TODO: read the logs, and compute the required size to print it.
|
||||
let logs = logger::LOGS.lock().unwrap();
|
||||
|
||||
// The longest line sets the width
|
||||
let level_width = 8; // Width of "[ERROR] "
|
||||
let time_width = 16; // Width of "23:59:59.123 | "
|
||||
// The longest line sets the width
|
||||
let w = logs
|
||||
.iter()
|
||||
.map(|&(_, ref content)| content.width() + "[ERROR] ".width())
|
||||
.map(|record| record.message.width() + level_width + time_width)
|
||||
.max()
|
||||
.unwrap_or(1);
|
||||
let h = logs.len();
|
||||
|
Loading…
Reference in New Issue
Block a user