//! Logging events in a scrolling list. //! //! Events are logged in an upwards scrolled box that forgets about the events //! that move off-screen. The logger may optionally add timestamps to events. //! //! To create a log widget, make a `LogBuilder`, optionally modify properties, //! and then use its `create` method to create a `Log` object. use std::ptr; use crate::bindings; use crate::core::Screen; pub struct Log { ptr: *mut bindings::Logwidget, } pub struct LogBuilder<'a, 'b> { _screen: &'a Screen<'b>, lefttop: (u32, u32), size: (u32, u32), title: Option<&'a str>, timestamps: bool, } impl<'a, 'b> LogBuilder<'a, 'b> { pub fn new(screen: &'a Screen<'b>, lefttop: (u32, u32), size: (u32, u32)) -> Self { LogBuilder { _screen: screen, lefttop, size, title: None, timestamps: false, } } pub fn add_title<'s: 'a>(&mut self, title: &'s str) -> &mut Self { self.title = Some(title); self } pub fn set_timestamps(&mut self, timestamps: bool) -> &mut Self { self.timestamps = timestamps; self } pub fn create(&self) -> Log { Log { ptr: unsafe { bindings::lgw_make( self.lefttop.0 as i32, self.lefttop.1 as i32, self.size.0 as i32, self.size.1 as i32, match self.title { None => ptr::null(), Some(s) => s.as_ptr() as *const i8, }, self.timestamps) } } } } impl Log { /// Called automatically; should only be needed if something else overwrote /// the widget. pub fn redraw(&self) { unsafe { bindings::lgw_redraw(self.ptr); } } pub fn add(&mut self, line: &str) { unsafe { bindings::lgw_add(self.ptr, line.as_ptr() as *const i8); } } pub fn clear(&mut self) { unsafe { bindings::lgw_clear(self.ptr); } } pub fn change_title(&mut self, title: &str) { unsafe { bindings::lgw_changetitle(self.ptr, title.as_ptr() as *const i8); } } } impl Drop for Log { fn drop(&mut self) { unsafe { bindings::lgw_destroy(self.ptr); } } }