Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix: fix some format errors
Signed-off-by: HeWei <weihe1999@outlook.com>
  • Loading branch information
Marukohe committed Apr 29, 2025
commit 75b595a19baeb6825734e27cd604d0a0c217852f
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
29 changes: 22 additions & 7 deletions src/cache/models.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
Loading