From 6f782b06aae1305dae6eeda75c040c7a6d70cb10 Mon Sep 17 00:00:00 2001 From: Thaumy Date: Mon, 25 Sep 2023 17:38:39 +0800 Subject: [PATCH] feat: auto align news summary --- src/display/colorful.rs | 16 ++++++++++++++-- src/display/normal.rs | 16 ++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/display/colorful.rs b/src/display/colorful.rs index f75bb79..040380a 100644 --- a/src/display/colorful.rs +++ b/src/display/colorful.rs @@ -306,7 +306,6 @@ pub fn search_post(result: &Result<(Vec, usize)>, rev: bool) -> Result>, @@ -328,7 +327,20 @@ pub fn list_news( let url = format!("https://news.cnblogs.com/n/{}", news.id); writeln!(buf, "{} {}", create_time.dimmed(), url.dimmed())?; writeln!(buf, " {}", news.title)?; - writeln!(buf, " {}{}", news.summary.dimmed(), "...".dimmed())?; + + let summary = { + let summary = format!("{}...", news.summary); + summary.width_split(get_term_width() - 4).map_or_else( + || summary.clone(), + |vec| { + vec.into_iter() + .map(|line| format!(" {}", line)) + .collect::>() + .join("\n") + }, + ) + }; + writeln!(buf, "{}", summary.dimmed())?; } buf }) diff --git a/src/display/normal.rs b/src/display/normal.rs index f443b46..6b63626 100644 --- a/src/display/normal.rs +++ b/src/display/normal.rs @@ -298,7 +298,6 @@ pub fn search_post(result: &Result<(Vec, usize)>, rev: bool) -> Result>, @@ -320,7 +319,20 @@ pub fn list_news( let url = format!("https://news.cnblogs.com/n/{}", news.id); writeln!(buf, "{} {}", create_time, url)?; writeln!(buf, " {}", news.title)?; - writeln!(buf, " {}...", news.summary)?; + + let summary = { + let summary = format!("{}...", news.summary); + summary.width_split(get_term_width() - 4).map_or_else( + || summary.clone(), + |vec| { + vec.into_iter() + .map(|line| format!(" {}", line)) + .collect::>() + .join("\n") + }, + ) + }; + writeln!(buf, "{}", summary)?; } buf })