From 13dd44557449919ca434ca2c919dc4b8e4d09bf2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Palenica?= Date: Tue, 6 Jul 2021 23:41:37 -0700 Subject: [PATCH] Some progress on decorating pipe. Add some logic to handle endl in plugin messages. Basic logging from plugins works now. --- default-plugins/status-bar/src/main.rs | 1 + default-plugins/strider/src/main.rs | 1 + zellij-server/src/decorating_pipe.rs | 72 +++++++++++++++++++++----- zellij-tile/src/lib.rs | 1 + 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/default-plugins/status-bar/src/main.rs b/default-plugins/status-bar/src/main.rs index 83eb50c9..e9e60fc3 100644 --- a/default-plugins/status-bar/src/main.rs +++ b/default-plugins/status-bar/src/main.rs @@ -133,6 +133,7 @@ fn color_elements(palette: Palette) -> ColoredElements { impl ZellijPlugin for State { fn load(&mut self) { + dbg!("hello from load"); set_selectable(false); set_invisible_borders(true); set_fixed_height(2); diff --git a/default-plugins/strider/src/main.rs b/default-plugins/strider/src/main.rs index d73f674f..7d80f014 100644 --- a/default-plugins/strider/src/main.rs +++ b/default-plugins/strider/src/main.rs @@ -10,6 +10,7 @@ register_plugin!(State); impl ZellijPlugin for State { fn load(&mut self) { dbg!("hello from load"); + dbg!("hello from load2?"); refresh_directory(self); subscribe(&[EventType::KeyPress]); } diff --git a/zellij-server/src/decorating_pipe.rs b/zellij-server/src/decorating_pipe.rs index 4b95c57b..fb02ce6a 100644 --- a/zellij-server/src/decorating_pipe.rs +++ b/zellij-server/src/decorating_pipe.rs @@ -3,9 +3,10 @@ use std::{ io::{Read, Seek, Write}, }; -use log::info; +use log::{error, info}; use serde::{Deserialize, Serialize}; use wasmer_wasi::{WasiFile, WasiFsError}; +use zellij_utils::logging::debug_log_to_file; #[derive(Debug, Serialize, Deserialize)] pub struct DecoratingPipe { @@ -16,7 +17,7 @@ pub struct DecoratingPipe { impl DecoratingPipe { pub fn new(plugin_name: &str) -> DecoratingPipe { info!("Creating decorating pipe!"); - dbg!("Creating the decorating pipe :)"); + debug_log_to_file("Creating decorating pipe!".to_string()).expect("xd"); DecoratingPipe { buffer: VecDeque::new(), plugin_name: String::from(plugin_name), @@ -39,21 +40,64 @@ impl Read for DecoratingPipe { impl Write for DecoratingPipe { fn write(&mut self, buf: &[u8]) -> std::io::Result { self.buffer.extend(buf); - let current_chunk = std::str::from_utf8(buf).unwrap(); - for c in current_chunk.chars() { - if c == '\n' { - info!( - "{}: {}", - self.plugin_name, - std::str::from_utf8(&self.buffer.make_contiguous().split_last().unwrap().1) - .unwrap() - ); - self.buffer.clear(); - } - } + + debug_log_to_file(format!( + "Write called for {}, currentChunk: {}", + self.plugin_name, + std::str::from_utf8(buf).unwrap() + )) + .expect("xd2"); + Ok(buf.len()) } + + // When we flush, check if current buffer is valid utf8 string, split by '\n' and truncate buffer in the process. + // We assume that, eventually, flush will be called on valid string boundary (i.e. std::str::from_utf8(..).is_ok() returns true at some point). + // Above assumption might not be true, in which case we'll have to think about it. Also, at some point we might actually require some synchronization + // between write and flush (i.e. concurrent writes and flushes?). Make it simple for now. fn flush(&mut self) -> std::io::Result<()> { + debug_log_to_file(format!( + "Flush called for {}, buffer: {:?}", + self.plugin_name, self.buffer + )) + .expect("xd3"); + + self.buffer.make_contiguous(); + + if let Ok(converted_string) = std::str::from_utf8(self.buffer.as_slices().0) { + if converted_string.contains('\n') { + let mut consumed_bytes = 0; + let mut split_msg = converted_string.split('\n').peekable(); + debug_log_to_file(format!( + "Back: {}, len: {}, convertedString: {}", + split_msg.clone().collect::(), + split_msg.clone().count(), + converted_string + )) + .expect("xD"); + while let Some(msg) = split_msg.next() { + if split_msg.peek().is_none() { + // Log last chunk iff the last char is endline. Otherwise do not do it. + if converted_string.chars().last().unwrap() == '\n' && !msg.is_empty() { + info!("special case: {}: {}", self.plugin_name, msg); + consumed_bytes += msg.len() + 1; + } + } else { + info!("normal case: {}: {}", self.plugin_name, msg); + consumed_bytes += msg.len() + 1; + } + } + drop(self.buffer.drain(..consumed_bytes)); + debug_log_to_file(format!( + "Consumed: {} bytes, buffer: {:?}", + consumed_bytes, self.buffer + )) + .expect("xd4"); + } + } else { + error!("Buffer conversion didn't work. This is unexpected"); + } + Ok(()) } } diff --git a/zellij-tile/src/lib.rs b/zellij-tile/src/lib.rs index af7d8ca1..176a9497 100644 --- a/zellij-tile/src/lib.rs +++ b/zellij-tile/src/lib.rs @@ -19,6 +19,7 @@ macro_rules! register_plugin { } fn main() { + dbg!("hello from plugin main :)"); STATE.with(|state| { state.borrow_mut().load(); });