From 8add137b1935ca13a1127c17edfdd3985983d456 Mon Sep 17 00:00:00 2001 From: Tymon Date: Wed, 9 Nov 2022 01:25:17 +0100 Subject: [PATCH] Interaction logging --- Cargo.toml | 1 + src/app.rs | 54 +++++++++++++++++++++++++++++++++++++++++++++++------ src/main.rs | 6 +++--- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 5119534..382fd27 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ panic = "abort" [dependencies] eframe = "0.19.0" +egui-toast = "0.4.0" serde = { version = "1.0.145", features = ["derive"] } serde_json = "1.0.86" tungstenite = "0.17.3" diff --git a/src/app.rs b/src/app.rs index 2dab600..71d5b0a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,16 +1,19 @@ use std::sync::mpsc::Receiver; +use std::time::Duration; + use eframe::{ egui::{self, RichText, ScrollArea, Window}, emath::Align, epaint::Color32, }; -use serde::{Deserialize, Serialize}; +use egui_toast::Toasts; +use serde::Deserialize; use tungstenite::Message; pub struct App { rx: Receiver, - history: Vec, + history: Vec, autoscroll: bool, description: String, selected_channel: String, @@ -18,14 +21,29 @@ pub struct App { description_shown: bool, } -#[derive(Serialize, Deserialize)] -struct Log { +#[derive(Deserialize)] +struct MessageLog { name: String, description: String, message: String, channel: String, } +#[derive(Deserialize)] +struct InteractionLog { + name: String, + command: String, + arguments: String, + channel: String, +} + +#[derive(Deserialize)] +#[serde(tag = "type")] +enum Loggers { + InteractionLog(InteractionLog), + MessageLog(MessageLog), +} + impl App { pub fn new(rx: Receiver) -> Self { Self { @@ -42,15 +60,38 @@ impl App { impl eframe::App for App { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { + let mut toasts = Toasts::new() + .anchor((10.0, 10.0)) + .direction(egui::Direction::TopDown) + .align_to_end(false); let message = self.rx.try_recv(); - // TODO: Add toasts for things like debug information and status changes (https://github.com/urholaukkarinen/egui-toast) + if let Ok(message) = message { let text = message.to_string(); let json = serde_json::from_str(&text); if let Ok(json) = json { - self.history.push(json); + let json: Loggers = json; + match json { + Loggers::MessageLog(msg) => { + self.history.push(msg); + } + + Loggers::InteractionLog(msg) => { + if self.selected_channel == msg.channel { + toasts.info( + format!( + "User {} called command /{} with arguments {}", + msg.name, msg.command, msg.arguments + ), + Duration::from_secs(5), + ); + } + } + } + } else if let Err(json) = json { + println!("{}", json); } } egui::CentralPanel::default().show(ctx, |ui| { @@ -117,5 +158,6 @@ impl eframe::App for App { }); ctx.request_repaint(); + toasts.show(ctx); } } diff --git a/src/main.rs b/src/main.rs index 51ae21a..b53b75b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,10 +21,10 @@ fn main() { socket .write_message(Message::Text("__PONG__".to_string())) .expect("Failed to respond to ping! Did the WebSocket server die?"); + } else { + tx.send(message) + .expect("Failed to send message to channel! (This shouldn't be possible)"); } - - tx.send(message) - .expect("Failed to send message to channel! (This shouldn't be possible)"); } });