From 37a497debce5a521b590b405ebdfeecb8a932bd6 Mon Sep 17 00:00:00 2001 From: Guocork Date: Thu, 19 Sep 2024 14:42:47 +0800 Subject: [PATCH] encode and attach it to playground url --- Cargo.lock | 1 + Cargo.toml | 1 + src/chat/chat_line.rs | 24 +++++++++++++----------- 3 files changed, 15 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e8bd037f..4f3b941a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1811,6 +1811,7 @@ dependencies = [ "serde", "serde_json", "unicode-segmentation", + "urlencoding", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 93d1bed2..234a0d0e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,6 +43,7 @@ lipsum = "0.9" rand = "0.8.5" rfd = "0.14.1" +urlencoding = "2.1.3" ## Configuration for `cargo packager` diff --git a/src/chat/chat_line.rs b/src/chat/chat_line.rs index 0af2b6e0..4c18e2b6 100644 --- a/src/chat/chat_line.rs +++ b/src/chat/chat_line.rs @@ -3,6 +3,7 @@ use makepad_widgets::markdown::MarkdownWidgetExt; use makepad_widgets::*; use makepad_markdown::parse_markdown; +use urlencoding::encode; live_design! { import makepad_widgets::base::*; @@ -371,15 +372,14 @@ impl ChatLine { } if self.button(id!(link_button)).clicked(&actions) { - if self.code_block.contains("rust") { - let code_block = &self.code_block[4..]; - } else if self.code_block.contains("rs"){ - let code_block = &self.code_block[2..]; - } - // 转化code成为encode - // let playground_url = &format!("https://play.rust-lang.org/?code={}", ); + let encoded_code = encode(&self.code_block); + + let playground_url = &format!( + "https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&code={}", + encoded_code + ); - if let Err(e) = robius_open::Uri::new("https://play.rust-lang.org").open() { + if let Err(e) = robius_open::Uri::new(playground_url).open() { error!("Error opening URL: {:?}", e); } } @@ -429,10 +429,12 @@ impl ChatLine { if !latest_message.is_empty() { // this mean its markdown let code_block = extract_code_block(&latest_message); - if code_block.contains("rust") || code_block.contains("rs") { + if code_block.contains("rust") { + self.button(id!(link_button)).set_visible(true); + self.code_block = code_block[4..].to_owned(); + } else if code_block.contains("rs") { self.button(id!(link_button)).set_visible(true); - self.code_block = code_block.to_owned(); // to_string 还是 to_owned - log!("{}", self.code_block); + self.code_block = code_block[2..].to_owned(); } } }