use std::fmt::{Debug, Formatter};
use r3bl_rs_utils_core::*;
use tokio::sync::mpsc::Sender;
use crate::*;
pub struct GlobalData<S, AS>
where
S: Debug + Default + Clone + Sync + Send,
AS: Debug + Default + Clone + Sync + Send,
{
pub window_size: Size,
pub maybe_saved_offscreen_buffer: Option<OffscreenBuffer>,
pub main_thread_channel_sender: Sender<TerminalWindowMainThreadSignal<AS>>,
pub state: S,
}
mod global_data_impl {
use super::*;
impl<S, AS> Debug for GlobalData<S, AS>
where
S: Debug + Default + Clone + Sync + Send,
AS: Debug + Default + Clone + Sync + Send,
{
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let vec_lines = {
let mut it = vec![];
it.push(format!("window_size: {0:?}", self.window_size));
it.push(match &self.maybe_saved_offscreen_buffer {
None => "no saved offscreen buffer".to_string(),
Some(ref offscreen_buffer) => match DEBUG_TUI_COMPOSITOR {
false => {
"offscreen buffer saved from previous render".to_string()
}
true => offscreen_buffer.pretty_print(),
},
});
it
};
write!(f, "\nGlobalData\n - {}", vec_lines.join("\n - "))
}
}
impl<S, AS> GlobalData<S, AS>
where
S: Debug + Default + Clone + Sync + Send,
AS: Debug + Default + Clone + Sync + Send,
{
pub fn try_to_create_instance(
main_thread_channel_sender: Sender<TerminalWindowMainThreadSignal<AS>>,
state: S,
) -> CommonResult<GlobalData<S, AS>>
where
AS: Debug + Default + Clone + Sync + Send,
{
let mut it = GlobalData {
window_size: Default::default(),
maybe_saved_offscreen_buffer: Default::default(),
state,
main_thread_channel_sender,
};
it.set_size(terminal_lib_operations::lookup_size()?);
Ok(it)
}
pub fn set_size(&mut self, new_size: Size) {
self.window_size = new_size;
self.dump_to_log("main_event_loop -> Resize");
}
pub fn get_size(&self) -> Size { self.window_size }
pub fn dump_to_log(&self, msg: &str) {
let log_msg = format!("{msg} -> {self:?}");
call_if_true!(DEBUG_TUI_MOD, log_info(log_msg));
}
}
}