Skip to content

Commit 1a642fc

Browse files
committed
feat(ing list): content auto wrap
1 parent d0deb8c commit 1a642fc

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/display/colorful.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::fmt::Display;
1414
use std::ops::Not;
1515
use std::path::PathBuf;
1616
use unicode_width::UnicodeWidthStr;
17+
use crate::infra::str::StrExt;
1718

1819
pub fn login(cfg_path: &Result<PathBuf>) {
1920
match cfg_path {
@@ -82,10 +83,20 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: b
8283
print!(" {}⭐", star_text.yellow());
8384
}
8485
println!(" {} {}", "#".dimmed(), ing.id.to_string().dimmed());
85-
let user_name_width = ing.user_name.width_cjk();
8686
let content = if align {
87-
fmt_content(&ing.content)
88-
.replace('\n', &format!("\n{}", " ".repeat(user_name_width + 3)))
87+
let user_name_width = ing.user_name.width_cjk();
88+
let (term_width, _) = term_size::dimensions()
89+
.expect("Can not get terminal size");
90+
let left_width = term_width.checked_sub(user_name_width + 3).unwrap_or(0);
91+
if let Some(lines) = fmt_content(&ing.content).width_split(left_width) {
92+
if comment_list.len() > 0 {
93+
lines.join("\n").replace("\n", &format!("\n │{}", " ".repeat(user_name_width - 2)))
94+
} else {
95+
lines.join("\n").replace("\n", &format!("\n{}", " ".repeat(user_name_width + 3)))
96+
}
97+
} else {
98+
ing.content.clone()
99+
}
89100
} else {
90101
fmt_content(&ing.content)
91102
};
@@ -237,7 +248,7 @@ pub fn list_news(news_list: &Result<Vec<NewsEntry>>, rev: bool) {
237248
};
238249

239250
let url = format!("https://news.cnblogs.com/n/{}", news.id);
240-
println!("{} {}", create_time.dimmed(), url.dimmed(),);
251+
println!("{} {}", create_time.dimmed(), url.dimmed(), );
241252
println!(" {}", news.title);
242253
println!(" {}{}", news.summary.dimmed(), "...".dimmed());
243254
println!();

src/display/normal.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::fmt::Display;
1414
use std::ops::Not;
1515
use std::path::PathBuf;
1616
use unicode_width::UnicodeWidthStr;
17+
use crate::infra::str::StrExt;
1718

1819
pub fn login(cfg_path: &Result<PathBuf>) {
1920
match cfg_path {
@@ -82,10 +83,20 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: b
8283
print!(" {}⭐", star_text);
8384
}
8485
println!(" # {}", ing.id);
85-
let user_name_width = ing.user_name.width_cjk();
8686
let content = if align {
87-
fmt_content(&ing.content)
88-
.replace('\n', &format!("\n{}", " ".repeat(user_name_width + 3)))
87+
let user_name_width = ing.user_name.width_cjk();
88+
let (term_width, _) = term_size::dimensions()
89+
.expect("Can not get terminal size");
90+
let left_width = term_width.checked_sub(user_name_width + 3).unwrap_or(0);
91+
if let Some(lines) = fmt_content(&ing.content).width_split(left_width) {
92+
if comment_list.len() > 0 {
93+
lines.join("\n").replace("\n", &format!("\n │{}", " ".repeat(user_name_width - 2)))
94+
} else {
95+
lines.join("\n").replace("\n", &format!("\n{}", " ".repeat(user_name_width + 3)))
96+
}
97+
} else {
98+
ing.content.clone()
99+
}
89100
} else {
90101
fmt_content(&ing.content)
91102
};

0 commit comments

Comments
 (0)