Skip to content

Commit

Permalink
Merge pull request #40 from matheuswhite/change_to_vec_u8
Browse files Browse the repository at this point in the history
Changing String to Bytes on appropriate places
  • Loading branch information
matheuswhite authored Feb 21, 2024
2 parents ecbc626 + d4add34 commit dc9019c
Show file tree
Hide file tree
Showing 7 changed files with 638 additions and 295 deletions.
20 changes: 13 additions & 7 deletions src/command_bar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ impl<B: Backend + Send + Sync + 'static> CommandBar<B> {
let data_to_send = yaml_content.get(key).unwrap();
let data_to_send = data_to_send.replace("\\r", "\r").replace("\\n", "\n");
let interface = self.interface.lock().unwrap();
interface.send(UserTxData::Command(key.to_string(), data_to_send));
interface.send(UserTxData::Command {
command_name: key.to_string(),
content: data_to_send,
});
}
'!' => {
let command_line_split = command_line
Expand All @@ -386,11 +389,12 @@ impl<B: Backend + Send + Sync + 'static> CommandBar<B> {
msg_lut.insert("reload".to_string(), "Plugin reloaded!");

let mut text_view = self.text_view.lock().unwrap();
text_view.add_data_out(SerialRxData::Plugin(
Local::now(),
text_view.add_data_out(SerialRxData::Plugin {
timestamp: Local::now(),
plugin_name,
msg_lut[&cmd].to_string(),
))
content: msg_lut[&cmd].to_string(),
is_successful: true,
})
}
Err(err_msg) => {
self.set_error_pop_up(err_msg);
Expand Down Expand Up @@ -422,11 +426,13 @@ impl<B: Backend + Send + Sync + 'static> CommandBar<B> {
};

let interface = self.interface.lock().unwrap();
interface.send(UserTxData::HexString(bytes));
interface.send(UserTxData::HexString { content: bytes });
}
_ => {
let interface = self.interface.lock().unwrap();
interface.send(UserTxData::Data(command_line));
interface.send(UserTxData::Data {
content: command_line,
});
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod messages;
mod plugin;
mod plugin_installer;
mod plugin_manager;
mod rich_string;
mod serial;
mod text;

Expand Down
234 changes: 161 additions & 73 deletions src/messages.rs
Original file line number Diff line number Diff line change
@@ -1,103 +1,191 @@
use crate::rich_string::RichText;
use crate::text::ViewData;
use chrono::{DateTime, Local};
use tui::style::Color;

pub enum UserTxData {
Exit,
Data(String),
Command(String, String),
HexString(Vec<u8>),
PluginSerialTx(String, Vec<u8>),
Data {
content: String,
},
Command {
command_name: String,
content: String,
},
HexString {
content: Vec<u8>,
},
PluginSerialTx {
plugin_name: String,
content: Vec<u8>,
},
}

#[derive(Clone)]
pub enum SerialRxData {
Data(DateTime<Local>, String),
ConfirmData(DateTime<Local>, String),
ConfirmCommand(DateTime<Local>, String, String),
ConfirmHexString(DateTime<Local>, Vec<u8>),
Plugin(DateTime<Local>, String, String),
ConfirmPluginSerialTx(DateTime<Local>, String, Vec<u8>),
FailPlugin(DateTime<Local>, String, String),
FailData(DateTime<Local>, String),
FailCommand(DateTime<Local>, String, String),
FailHexString(DateTime<Local>, Vec<u8>),
FailPluginSerialTx(DateTime<Local>, String, Vec<u8>),
RxData {
timestamp: DateTime<Local>,
content: Vec<u8>,
},
TxData {
timestamp: DateTime<Local>,
content: String,
is_successful: bool,
},
Command {
timestamp: DateTime<Local>,
command_name: String,
content: String,
is_successful: bool,
},
HexString {
timestamp: DateTime<Local>,
content: Vec<u8>,
is_successful: bool,
},
Plugin {
timestamp: DateTime<Local>,
plugin_name: String,
content: String,
is_successful: bool,
},
PluginSerialTx {
timestamp: DateTime<Local>,
plugin_name: String,
content: Vec<u8>,
is_successful: bool,
},
}

impl SerialRxData {
pub fn is_plugin_serial_tx(&self) -> bool {
matches!(
self,
SerialRxData::ConfirmPluginSerialTx(..) | SerialRxData::FailPluginSerialTx(..)
)
matches!(self, SerialRxData::PluginSerialTx { .. })
}
}

#[allow(clippy::from_over_into)]
impl Into<ViewData> for SerialRxData {
fn into(self) -> ViewData {
match self {
SerialRxData::Data(timestamp, content) => {
ViewData::new(timestamp, content, Color::Reset, Color::Reset)
SerialRxData::RxData { timestamp, content } => {
RichText::new(content, Color::Reset, Color::Reset)
.decode_ansi_color()
.highlight_invisible()
.into_view_data(timestamp)
}
SerialRxData::ConfirmData(timestamp, content) => {
ViewData::new(timestamp, content, Color::Black, Color::LightCyan)
}
SerialRxData::ConfirmCommand(timestamp, cmd_name, content) => ViewData::new(
timestamp,
format!("</{}> {}", cmd_name, content),
Color::Black,
Color::LightGreen,
),
SerialRxData::ConfirmHexString(timestamp, bytes) => ViewData::new(
timestamp,
format!("{:02x?}", &bytes),
Color::Black,
Color::Yellow,
),
SerialRxData::Plugin(timestamp, plugin_name, message) => ViewData::new(
timestamp,
format!(" [{plugin_name}] {message} "),
Color::Black,
Color::White,
),
SerialRxData::ConfirmPluginSerialTx(timestamp, plugin_name, message) => ViewData::new(
timestamp,
format!(" [{plugin_name}] => {:02x?} ", message),
Color::Black,
Color::White,
),
SerialRxData::FailData(timestamp, content) => ViewData::new(
SerialRxData::TxData {
timestamp,
format!("Cannot send \"{}\"", content),
Color::White,
Color::LightRed,
),
SerialRxData::FailCommand(timestamp, cmd_name, _content) => ViewData::new(
content,
is_successful,
} => {
if is_successful {
RichText::from_string(content, Color::Black, Color::LightCyan)
.highlight_invisible()
.into_view_data(timestamp)
} else {
RichText::from_string(
format!("Cannot send \"{}\"", content),
Color::White,
Color::LightRed,
)
.highlight_invisible()
.into_view_data(timestamp)
}
}
SerialRxData::Command {
timestamp,
format!("Cannot send </{}>", cmd_name),
Color::White,
Color::LightRed,
),
SerialRxData::FailHexString(timestamp, bytes) => ViewData::new(
command_name,
content,
is_successful,
} => {
if is_successful {
RichText::from_string(
format!("</{}> {}", command_name, content),
Color::Black,
Color::LightGreen,
)
.highlight_invisible()
.into_view_data(timestamp)
} else {
RichText::from_string(
format!("Cannot send </{}>", command_name),
Color::White,
Color::LightRed,
)
.highlight_invisible()
.into_view_data(timestamp)
}
}
SerialRxData::HexString {
timestamp,
format!("Cannot send {:02x?}", &bytes),
Color::White,
Color::LightRed,
),
SerialRxData::FailPlugin(timestamp, plugin_name, message) => ViewData::new(
content,
is_successful,
} => {
if is_successful {
RichText::from_string(format!("{:02x?}", &content), Color::Black, Color::Yellow)
.highlight_invisible()
.into_view_data(timestamp)
} else {
RichText::from_string(
format!("Cannot send {:02x?}", &content),
Color::White,
Color::LightRed,
)
.highlight_invisible()
.into_view_data(timestamp)
}
}
SerialRxData::Plugin {
timestamp,
format!(" [{plugin_name}] {message} "),
Color::White,
Color::Red,
),
SerialRxData::FailPluginSerialTx(timestamp, pluging_name, message) => ViewData::new(
plugin_name,
content,
is_successful,
} => {
if is_successful {
RichText::from_string(
format!(" [{plugin_name}] {content} "),
Color::Black,
Color::White,
)
.highlight_invisible()
.into_view_data(timestamp)
} else {
RichText::from_string(
format!(" [{plugin_name}] {content} "),
Color::White,
Color::Red,
)
.highlight_invisible()
.into_view_data(timestamp)
}
}
SerialRxData::PluginSerialTx {
timestamp,
format!(" [{pluging_name}] => Fail to send {:02x?} ", message),
Color::White,
Color::Red,
),
plugin_name,
content,
is_successful,
} => {
if is_successful {
RichText::from_string(
format!(" [{plugin_name}] => {} ", String::from_utf8_lossy(&content)),
Color::Black,
Color::White,
)
.highlight_invisible()
.into_view_data(timestamp)
} else {
RichText::from_string(
format!(
" [{plugin_name}] => Fail to send {} ",
String::from_utf8_lossy(&content)
),
Color::White,
Color::Red,
)
.highlight_invisible()
.into_view_data(timestamp)
}
}
}
}
}
27 changes: 22 additions & 5 deletions src/plugin_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,15 @@ impl PluginManager {

pub fn call_plugins_serial_rx(&self, data_out: SerialRxData) {
for plugin in self.plugins.values().cloned().collect::<Vec<_>>() {
let SerialRxData::Data(_timestamp, line) = &data_out else {
let SerialRxData::RxData {
timestamp: _timestamp,
content: line,
} = &data_out
else {
continue;
};

let serial_rx_call = plugin.serial_rx_call(line.as_bytes().to_vec());
let serial_rx_call = plugin.serial_rx_call(line.clone());
let plugin_name = plugin.name().to_string();
self.serial_rx_tx
.send((plugin_name, serial_rx_call))
Expand All @@ -178,19 +182,32 @@ impl PluginManager {
match req {
PluginRequest::Println { msg } => {
let mut text_view = text_view.lock().unwrap();
text_view.add_data_out(SerialRxData::Plugin(Local::now(), plugin_name, msg))
text_view.add_data_out(SerialRxData::Plugin {
timestamp: Local::now(),
plugin_name,
content: msg,
is_successful: true,
})
}
PluginRequest::Eprintln { msg } => {
let mut text_view = text_view.lock().unwrap();
text_view.add_data_out(SerialRxData::FailPlugin(Local::now(), plugin_name, msg))
text_view.add_data_out(SerialRxData::Plugin {
timestamp: Local::now(),
plugin_name,
content: msg,
is_successful: false,
})
}
PluginRequest::Connect { .. } => {}
PluginRequest::Disconnect => {}
PluginRequest::Reconnect => {}
PluginRequest::SerialTx { msg } => {
let mut text_view = text_view.lock().unwrap();
let interface = interface.lock().unwrap();
interface.send(UserTxData::PluginSerialTx(plugin_name, msg));
interface.send(UserTxData::PluginSerialTx {
plugin_name,
content: msg,
});

'plugin_serial_tx: loop {
match interface.recv() {
Expand Down
Loading

0 comments on commit dc9019c

Please sign in to comment.