Skip to content

Commit e80ebe5

Browse files
MarukoheHeWei
and
HeWei
authored
feat(list): format problem list with Chinese unicode (#190)
Signed-off-by: HeWei <[email protected]> Co-authored-by: HeWei <[email protected]>
1 parent 409d483 commit e80ebe5

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ scraper = "0.23.1"
3535
anyhow = "1.0.97"
3636
clap_complete = "4.5.47"
3737
thiserror = "2.0.12"
38+
unicode-width = "0.1"
3839

3940
[dependencies.diesel]
4041
version = "2.2.8"

src/cache/models.rs

+22-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
//! Leetcode data models
2+
use unicode_width::UnicodeWidthStr;
3+
use unicode_width::UnicodeWidthChar;
24
use super::schemas::{problems, tags};
35
use crate::helper::HTML;
46
use colored::Colorize;
@@ -54,7 +56,7 @@ impl Problem {
5456
static DONE: &str = " ✔";
5557
static ETC: &str = "...";
5658
static LOCK: &str = "🔒";
57-
static NDONE: &str = "✘";
59+
static NDONE: &str = " ✘";
5860
static SPACE: &str = " ";
5961
impl std::fmt::Display for Problem {
6062
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
@@ -98,14 +100,27 @@ impl std::fmt::Display for Problem {
98100
}
99101
}
100102

101-
if self.name.len() < 60_usize {
103+
let name_width = UnicodeWidthStr::width(self.name.as_str());
104+
let target_width = 60;
105+
if name_width <= target_width {
102106
name.push_str(&self.name);
103-
name.push_str(&SPACE.repeat(60 - &self.name.len()));
107+
name.push_str(&SPACE.repeat(target_width - name_width));
104108
} else {
105-
name.push_str(&self.name[..49]);
106-
name = name.trim_end().to_string();
107-
name.push_str(ETC);
108-
name.push_str(&SPACE.repeat(60 - name.len()));
109+
// truncate carefully to target width - 3 (because "..." will take some width)
110+
let mut truncated = String::new();
111+
let mut current_width = 0;
112+
for c in self.name.chars() {
113+
let char_width = UnicodeWidthChar::width(c).unwrap_or(0);
114+
if current_width + char_width > target_width - 3 {
115+
break;
116+
}
117+
truncated.push(c);
118+
current_width += char_width;
119+
}
120+
truncated.push_str(ETC); // add "..."
121+
let truncated_width = UnicodeWidthStr::width(truncated.as_str());
122+
truncated.push_str(&SPACE.repeat(target_width - truncated_width));
123+
name = truncated;
109124
}
110125

111126
level = match self.level {

0 commit comments

Comments
 (0)