From 75b595a19baeb6825734e27cd604d0a0c217852f Mon Sep 17 00:00:00 2001 From: HeWei Date: Tue, 29 Apr 2025 16:50:07 +0800 Subject: [PATCH 1/2] Fix: fix some format errors Signed-off-by: HeWei --- Cargo.toml | 1 + src/cache/models.rs | 29 ++++++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c48709d..eb50acf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,7 @@ scraper = "0.23.1" anyhow = "1.0.97" clap_complete = "4.5.47" thiserror = "2.0.12" +unicode-width = "0.1" [dependencies.diesel] version = "2.2.8" diff --git a/src/cache/models.rs b/src/cache/models.rs index 2f49eaf..9b28482 100644 --- a/src/cache/models.rs +++ b/src/cache/models.rs @@ -1,4 +1,6 @@ //! Leetcode data models +use unicode_width::UnicodeWidthStr; +use unicode_width::UnicodeWidthChar; use super::schemas::{problems, tags}; use crate::helper::HTML; use colored::Colorize; @@ -54,7 +56,7 @@ impl Problem { static DONE: &str = " ✔"; static ETC: &str = "..."; static LOCK: &str = "🔒"; -static NDONE: &str = "✘"; +static NDONE: &str = " ✘"; static SPACE: &str = " "; impl std::fmt::Display for Problem { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { @@ -98,14 +100,27 @@ impl std::fmt::Display for Problem { } } - if self.name.len() < 60_usize { + let name_width = UnicodeWidthStr::width(self.name.as_str()); + let target_width = 60; + if name_width <= target_width { name.push_str(&self.name); - name.push_str(&SPACE.repeat(60 - &self.name.len())); + name.push_str(&SPACE.repeat(target_width - name_width)); } else { - name.push_str(&self.name[..49]); - name = name.trim_end().to_string(); - name.push_str(ETC); - name.push_str(&SPACE.repeat(60 - name.len())); + // truncate carefully to target width - 3 (because "..." will take some width) + let mut truncated = String::new(); + let mut current_width = 0; + for c in self.name.chars() { + let char_width = UnicodeWidthChar::width(c).unwrap_or(0); + if current_width + char_width > target_width - 3 { + break; + } + truncated.push(c); + current_width += char_width; + } + truncated.push_str(ETC); // add "..." + let truncated_width = UnicodeWidthStr::width(truncated.as_str()); + truncated.push_str(&SPACE.repeat(target_width - truncated_width)); + name = truncated; } level = match self.level { From 4cf76dbdabffe310de96736ab6b3b4f461b5ceb8 Mon Sep 17 00:00:00 2001 From: Marukohe Date: Mon, 16 Jun 2025 11:30:41 +0800 Subject: [PATCH 2/2] support chinese question description Signed-off-by: Marukohe --- .vscode/launch.json | 14 ++++++++++++++ Cargo.lock | 1 + src/cache/models.rs | 4 ++-- 3 files changed, 17 insertions(+), 2 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..8ea252f --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "Debug leetcode-cli", + "type": "lldb", + "request": "launch", + "program": "${workspaceFolder}/target/debug/leetcode", + "args": ["pick", "1"], + "cwd": "${workspaceFolder}", + "stopOnEntry": false + } + ] + } \ No newline at end of file diff --git a/Cargo.lock b/Cargo.lock index 773f77d..c4682ee 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -892,6 +892,7 @@ dependencies = [ "thiserror", "tokio", "toml", + "unicode-width", ] [[package]] diff --git a/src/cache/models.rs b/src/cache/models.rs index 9b28482..87e818e 100644 --- a/src/cache/models.rs +++ b/src/cache/models.rs @@ -162,11 +162,11 @@ pub struct Question { impl Question { pub fn desc(&self) -> String { - self.content.render() + self.t_content.render() } pub fn desc_comment(&self, conf: &Config) -> String { - let desc = self.content.render(); + let desc = self.t_content.render(); let mut res = desc.lines().fold("\n".to_string(), |acc, e| { acc + "" + conf.code.comment_leading.as_str() + " " + e + "\n"