diff --git a/Cargo.lock b/Cargo.lock index 855bd13..f9eb91b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -155,6 +155,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d6041616acea41d67c4a984709ddab1587fd0b10efe5cc563fee954d2f011854" dependencies = [ "clipboard-win", + "core-graphics", + "image", "log", "objc", "objc-foundation", @@ -2731,6 +2733,7 @@ name = "ubusman" version = "0.1.0" dependencies = [ "anyhow", + "arboard", "async-ssh2-lite", "directories-next", "eframe", @@ -2738,6 +2741,7 @@ dependencies = [ "hex", "image", "lazy-regex", + "lazy_static", "serde", "serde_json", "shell-escape", diff --git a/Cargo.toml b/Cargo.toml index 0069290..eaf67d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ edition = "2021" [dependencies] anyhow = "1.0.70" +arboard = "3.2.0" async-ssh2-lite = { version = "0.4.5", features = ["async-io"] } directories-next = "2.0.0" eframe = "0.21.3" @@ -14,6 +15,7 @@ egui = "0.21.0" hex = "0.4.3" image = "0.24.6" lazy-regex = "2.5.0" +lazy_static = "1.4.0" serde = { version = "1.0.158", features = ["derive"] } serde_json = "1.0.94" shell-escape = "0.1.5" diff --git a/src/app.rs b/src/app.rs index 81530a8..8f9e4c5 100644 --- a/src/app.rs +++ b/src/app.rs @@ -7,14 +7,36 @@ use std::{ use anyhow::Result; use async_ssh2_lite::{AsyncIoTcpStream, AsyncSession}; use eframe::CreationContext; -use egui::{text::LayoutJob, Color32}; +use egui::{text::LayoutJob, Color32, ColorImage, TextureHandle, Vec2, Image}; use lazy_regex::regex_replace_all; use serde_json::Value; +use lazy_static::lazy_static; +use arboard::Clipboard; -use crate::ubus; +use crate::ubus::{self, escape_json}; const ERROR_COLOR: Color32 = Color32::from_rgb(180, 20, 20); const SUCCESS_COLOR: Color32 = Color32::from_rgb(20, 150, 20); +lazy_static! { + pub static ref COPY_ICON: ColorImage = load_image_from_memory(include_bytes!("./copy.png")) + .expect("Failed to load copy icon") as ColorImage; +} + +pub fn load_image_from_memory(image_data: &[u8]) -> Result { + let image = image::load_from_memory(image_data)?; + let size = [image.width() as _, image.height() as _]; + let image_buffer = image.to_rgba8(); + let pixels = image_buffer.as_flat_samples(); + Ok(ColorImage::from_rgba_unmultiplied( + size, + pixels.as_slice(), + )) +} + +fn set_clipboard(text: &str) { + let mut clipboard = Clipboard::new().unwrap(); + clipboard.set_text(text).unwrap(); +} pub enum AsyncEvent { Connect(Result>), @@ -49,6 +71,8 @@ pub struct App { tx: Sender, rx: Receiver, + + copy_texture: Option } impl Default for App { @@ -78,6 +102,8 @@ impl Default for App { tx, rx, + + copy_texture: None } } } @@ -123,7 +149,7 @@ fn json_layouter(ui: &egui::Ui, string: &str, wrap_width: f32) -> Arc Option<&str> { + self.selected_object.as_ref().map(|obj| obj.name.as_str()) + } + + pub fn get_selected_method(&self) -> Option<&str> { + self.selected_method.as_ref().map(|method| method.as_str()) + } + + pub fn get_payload(&self) -> serde_json::Result { + let stripped_payload = remove_json_comments(&self.payload); + serde_json::from_str(&stripped_payload) } fn handle_events(&mut self, _ctx: &egui::Context) { @@ -358,6 +405,7 @@ impl App { } fn show_central_panel(&mut self, ui: &mut egui::Ui) { + use egui::*; ui.horizontal(|ui| { // Object dropdown { @@ -424,13 +472,22 @@ impl App { let call_enabled = self.selected_object.is_some() && self.selected_method.is_some(); ui.add_enabled_ui(call_enabled, |ui| { if ui.button("call").clicked() { - let object_name = self.selected_object.as_ref().unwrap().name.clone(); - let method_name = self.selected_method.as_ref().unwrap().clone(); - let payload = remove_json_comments(&self.payload); - let message = serde_json::from_str(&payload).unwrap(); // TODO: handle parsing error - self.start_call(object_name, method_name, Some(message)); + let object_name = self.get_selected_object().unwrap().into(); + let method_name = self.get_selected_method().unwrap().into(); + let payload = self.get_payload().unwrap(); // TODO: handle parsing error + self.start_call(object_name, method_name, Some(payload)); // TODO: Block sending other requests } + + let copy_icon = self.copy_texture.as_ref().expect("Copy icon not loaded"); + let copy_button = Button::image_and_text(copy_icon.id(), Vec2::new(10.0, 10.0), "copy"); + if ui.add(copy_button).clicked() { + let object_name = self.get_selected_object().unwrap(); + let method_name = self.get_selected_method().unwrap(); + let payload = self.get_payload().unwrap(); // TODO: handle parsing error + let cmd = format!("ubus call {} {} {}", object_name, method_name, escape_json(&payload)); + set_clipboard(&cmd); + } }); }); diff --git a/src/copy.png b/src/copy.png new file mode 100644 index 0000000..68a7a3b Binary files /dev/null and b/src/copy.png differ diff --git a/src/ubus.rs b/src/ubus.rs index c19ae54..212a0b0 100644 --- a/src/ubus.rs +++ b/src/ubus.rs @@ -211,7 +211,7 @@ fn parse_hex_id(id: &str) -> Result { return Ok(byte1 + byte2 + byte3 + byte4); } -fn escape_json(json: &Value) -> String { +pub fn escape_json(json: &Value) -> String { escape(Cow::from(json.to_string())).into() }