From 72e6682c79f9a83911e93f6757115e9ee227dff7 Mon Sep 17 00:00:00 2001 From: Thaumy Date: Fri, 15 Sep 2023 18:06:13 +0800 Subject: [PATCH] feat: align ing wrap --- Cargo.lock | 18 ++++++++++++++++++ Cargo.toml | 2 ++ src/display/colorful.rs | 7 +++++-- src/display/normal.rs | 6 ++++-- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a38b15b..3088d98 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -249,7 +249,9 @@ dependencies = [ "serde_qs", "serde_repr", "serde_with", + "term_size", "tokio", + "unicode-width", "words-count", ] @@ -1253,6 +1255,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "term_size" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e4129646ca0ed8f45d09b929036bafad5377103edd06e50bf574b353d2b08d9" +dependencies = [ + "libc", + "winapi", +] + [[package]] name = "terminal_size" version = "0.2.6" @@ -1439,6 +1451,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "unicode-width" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" + [[package]] name = "untrusted" version = "0.7.1" diff --git a/Cargo.toml b/Cargo.toml index ab00ff3..448ff1b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -27,6 +27,7 @@ getrandom = { version = "0.2.10", features = ["js"] } rand = { version = "0.8.5" } regex = "1.9.5" words-count = "0.1.6" +unicode-width = "0.1.10" serde = { version = "1.0.188", features = ["derive"] } serde_qs = "0.12.0" @@ -43,5 +44,6 @@ futures = "0.3.28" clap = { version = "4.4.2", features = ["derive", "wrap_help"] } colored = "2.0.4" +term_size = "0.3.2" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/src/display/colorful.rs b/src/display/colorful.rs index ccad6dd..6dd85a5 100644 --- a/src/display/colorful.rs +++ b/src/display/colorful.rs @@ -13,6 +13,7 @@ use colored::Colorize; use std::fmt::Display; use std::ops::Not; use std::path::PathBuf; +use unicode_width::UnicodeWidthStr; pub fn login(cfg_path: &Result) { match cfg_path { @@ -81,9 +82,11 @@ pub fn list_ing(ing_list: &Result)>>, rev: b print!(" {}⭐", star_text.yellow()); } println!(" {} {}", "#".dimmed(), ing.id.to_string().dimmed()); - let content = fmt_content(&ing.content); + let user_name_width = ing.user_name.width_cjk(); + let content = fmt_content(&ing.content).replace('\n', &format!("\n{}", " ".repeat(user_name_width + 3))); println!(" {} {}", ing.user_name.cyan(), content); + let len = comment_list.len(); if len != 0 { let max_i = len - 1; @@ -230,7 +233,7 @@ pub fn list_news(news_list: &Result>, rev: bool) { }; let url = format!("https://news.cnblogs.com/n/{}", news.id); - println!("{} {}", create_time.dimmed(), url.dimmed(),); + println!("{} {}", create_time.dimmed(), url.dimmed(), ); println!(" {}", news.title); println!(" {}{}", news.summary.dimmed(), "...".dimmed()); println!(); diff --git a/src/display/normal.rs b/src/display/normal.rs index d85cb57..5f930bf 100644 --- a/src/display/normal.rs +++ b/src/display/normal.rs @@ -13,6 +13,7 @@ use colored::Colorize; use std::fmt::Display; use std::ops::Not; use std::path::PathBuf; +use unicode_width::UnicodeWidthStr; pub fn login(cfg_path: &Result) { match cfg_path { @@ -81,8 +82,9 @@ pub fn list_ing(ing_list: &Result)>>, rev: b print!(" {}⭐", star_text); } println!(" # {}", ing.id); - let content = fmt_content(&ing.content); - println!(" {}: {}", ing.user_name, content); + let user_name_width = ing.user_name.width_cjk(); + let content = fmt_content(&ing.content).replace('\n', &format!("\n{}", " ".repeat(user_name_width + 3))); + println!(" {} {}", ing.user_name, content); let len = comment_list.len(); if len != 0 {