Skip to content

fix: fix some format errors #190

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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