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/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..87e818e 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 { @@ -147,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" pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy