Skip to content

Commit

Permalink
feat: add rich text to handle invisible character and ANSI color codes
Browse files Browse the repository at this point in the history
  • Loading branch information
matheuswhite committed Feb 16, 2024
1 parent 50baaad commit 65cea97
Show file tree
Hide file tree
Showing 6 changed files with 408 additions and 196 deletions.
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
61 changes: 36 additions & 25 deletions src/messages.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::rich_string::RichText;
use crate::text::ViewData;
use chrono::{DateTime, Local};
use tui::style::Color;
Expand All @@ -24,7 +25,7 @@ pub enum UserTxData {
pub enum SerialRxData {
RxData {
timestamp: DateTime<Local>,
content: String,
content: Vec<u8>,
},
TxData {
timestamp: DateTime<Local>,
Expand Down Expand Up @@ -67,22 +68,28 @@ impl Into<ViewData> for SerialRxData {
fn into(self) -> ViewData {
match self {
SerialRxData::RxData { timestamp, content } => {
ViewData::new(timestamp, content, Color::Reset, Color::Reset)
RichText::new(content, Color::Reset, Color::Reset)
.decode_ansi_color()
.highlight_invisible()
.into_view_data(timestamp)
}
SerialRxData::TxData {
timestamp,
content,
is_successful,
} => {
if is_successful {
ViewData::new(timestamp, content, Color::Black, Color::LightCyan)
RichText::from_string(content, Color::Black, Color::LightCyan)
.highlight_invisible()
.into_view_data(timestamp)
} else {
ViewData::new(
timestamp,
RichText::from_string(
format!("Cannot send \"{}\"", content),
Color::White,
Color::LightRed,
)
.highlight_invisible()
.into_view_data(timestamp)
}
}
SerialRxData::Command {
Expand All @@ -92,19 +99,21 @@ impl Into<ViewData> for SerialRxData {
is_successful,
} => {
if is_successful {
ViewData::new(
timestamp,
RichText::from_string(
format!("</{}> {}", command_name, content),
Color::Black,
Color::LightGreen,
)
.highlight_invisible()
.into_view_data(timestamp)
} else {
ViewData::new(
timestamp,
RichText::from_string(
format!("Cannot send </{}>", command_name),
Color::White,
Color::LightRed,
)
.highlight_invisible()
.into_view_data(timestamp)
}
}
SerialRxData::HexString {
Expand All @@ -113,19 +122,17 @@ impl Into<ViewData> for SerialRxData {
is_successful,
} => {
if is_successful {
ViewData::new(
timestamp,
format!("{:02x?}", &content),
Color::Black,
Color::Yellow,
)
RichText::from_string(format!("{:02x?}", &content), Color::Black, Color::Yellow)
.highlight_invisible()
.into_view_data(timestamp)
} else {
ViewData::new(
timestamp,
RichText::from_string(
format!("Cannot send {:02x?}", &content),
Color::White,
Color::LightRed,
)
.highlight_invisible()
.into_view_data(timestamp)
}
}
SerialRxData::Plugin {
Expand All @@ -135,19 +142,21 @@ impl Into<ViewData> for SerialRxData {
is_successful,
} => {
if is_successful {
ViewData::new(
timestamp,
RichText::from_string(
format!(" [{plugin_name}] {content} "),
Color::Black,
Color::White,
)
.highlight_invisible()
.into_view_data(timestamp)
} else {
ViewData::new(
timestamp,
RichText::from_string(
format!(" [{plugin_name}] {content} "),
Color::White,
Color::Red,
)
.highlight_invisible()
.into_view_data(timestamp)
}
}
SerialRxData::PluginSerialTx {
Expand All @@ -157,19 +166,21 @@ impl Into<ViewData> for SerialRxData {
is_successful,
} => {
if is_successful {
ViewData::new(
timestamp,
RichText::from_string(
format!(" [{plugin_name}] => {:02x?} ", content),
Color::Black,
Color::White,
)
.highlight_invisible()
.into_view_data(timestamp)
} else {
ViewData::new(
timestamp,
RichText::from_string(
format!(" [{plugin_name}] => Fail to send {:02x?} ", content),
Color::White,
Color::Red,
)
.highlight_invisible()
.into_view_data(timestamp)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/plugin_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl PluginManager {
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 Down
Loading

0 comments on commit 65cea97

Please sign in to comment.