Some progress on decorating pipe. Add some logic to handle endl in plugin messages. Basic logging from plugins works now.

This commit is contained in:
Paweł Palenica 2021-07-06 23:41:37 -07:00
parent 0c570a52f5
commit 13dd445574
4 changed files with 61 additions and 14 deletions

View file

@ -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);

View file

@ -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]);
}

View file

@ -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<usize> {
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::<String>(),
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(())
}
}

View file

@ -19,6 +19,7 @@ macro_rules! register_plugin {
}
fn main() {
dbg!("hello from plugin main :)");
STATE.with(|state| {
state.borrow_mut().load();
});