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:
parent
0c570a52f5
commit
13dd445574
4 changed files with 61 additions and 14 deletions
|
|
@ -133,6 +133,7 @@ fn color_elements(palette: Palette) -> ColoredElements {
|
||||||
|
|
||||||
impl ZellijPlugin for State {
|
impl ZellijPlugin for State {
|
||||||
fn load(&mut self) {
|
fn load(&mut self) {
|
||||||
|
dbg!("hello from load");
|
||||||
set_selectable(false);
|
set_selectable(false);
|
||||||
set_invisible_borders(true);
|
set_invisible_borders(true);
|
||||||
set_fixed_height(2);
|
set_fixed_height(2);
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ register_plugin!(State);
|
||||||
impl ZellijPlugin for State {
|
impl ZellijPlugin for State {
|
||||||
fn load(&mut self) {
|
fn load(&mut self) {
|
||||||
dbg!("hello from load");
|
dbg!("hello from load");
|
||||||
|
dbg!("hello from load2?");
|
||||||
refresh_directory(self);
|
refresh_directory(self);
|
||||||
subscribe(&[EventType::KeyPress]);
|
subscribe(&[EventType::KeyPress]);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,9 +3,10 @@ use std::{
|
||||||
io::{Read, Seek, Write},
|
io::{Read, Seek, Write},
|
||||||
};
|
};
|
||||||
|
|
||||||
use log::info;
|
use log::{error, info};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use wasmer_wasi::{WasiFile, WasiFsError};
|
use wasmer_wasi::{WasiFile, WasiFsError};
|
||||||
|
use zellij_utils::logging::debug_log_to_file;
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct DecoratingPipe {
|
pub struct DecoratingPipe {
|
||||||
|
|
@ -16,7 +17,7 @@ pub struct DecoratingPipe {
|
||||||
impl DecoratingPipe {
|
impl DecoratingPipe {
|
||||||
pub fn new(plugin_name: &str) -> DecoratingPipe {
|
pub fn new(plugin_name: &str) -> DecoratingPipe {
|
||||||
info!("Creating decorating pipe!");
|
info!("Creating decorating pipe!");
|
||||||
dbg!("Creating the decorating pipe :)");
|
debug_log_to_file("Creating decorating pipe!".to_string()).expect("xd");
|
||||||
DecoratingPipe {
|
DecoratingPipe {
|
||||||
buffer: VecDeque::new(),
|
buffer: VecDeque::new(),
|
||||||
plugin_name: String::from(plugin_name),
|
plugin_name: String::from(plugin_name),
|
||||||
|
|
@ -39,21 +40,64 @@ impl Read for DecoratingPipe {
|
||||||
impl Write for DecoratingPipe {
|
impl Write for DecoratingPipe {
|
||||||
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
|
||||||
self.buffer.extend(buf);
|
self.buffer.extend(buf);
|
||||||
let current_chunk = std::str::from_utf8(buf).unwrap();
|
|
||||||
for c in current_chunk.chars() {
|
debug_log_to_file(format!(
|
||||||
if c == '\n' {
|
"Write called for {}, currentChunk: {}",
|
||||||
info!(
|
self.plugin_name,
|
||||||
"{}: {}",
|
std::str::from_utf8(buf).unwrap()
|
||||||
self.plugin_name,
|
))
|
||||||
std::str::from_utf8(&self.buffer.make_contiguous().split_last().unwrap().1)
|
.expect("xd2");
|
||||||
.unwrap()
|
|
||||||
);
|
|
||||||
self.buffer.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(buf.len())
|
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<()> {
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@ macro_rules! register_plugin {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
dbg!("hello from plugin main :)");
|
||||||
STATE.with(|state| {
|
STATE.with(|state| {
|
||||||
state.borrow_mut().load();
|
state.borrow_mut().load();
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue