|
1 | 1 | //! Leetcode data models
|
| 2 | +use unicode_width::UnicodeWidthStr; |
| 3 | +use unicode_width::UnicodeWidthChar; |
2 | 4 | use super::schemas::{problems, tags};
|
3 | 5 | use crate::helper::HTML;
|
4 | 6 | use colored::Colorize;
|
@@ -54,7 +56,7 @@ impl Problem {
|
54 | 56 | static DONE: &str = " ✔";
|
55 | 57 | static ETC: &str = "...";
|
56 | 58 | static LOCK: &str = "🔒";
|
57 |
| -static NDONE: &str = "✘"; |
| 59 | +static NDONE: &str = " ✘"; |
58 | 60 | static SPACE: &str = " ";
|
59 | 61 | impl std::fmt::Display for Problem {
|
60 | 62 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
@@ -98,14 +100,27 @@ impl std::fmt::Display for Problem {
|
98 | 100 | }
|
99 | 101 | }
|
100 | 102 |
|
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 { |
102 | 106 | 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)); |
104 | 108 | } 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; |
109 | 124 | }
|
110 | 125 |
|
111 | 126 | level = match self.level {
|
|
0 commit comments