Skip to content

Commit df27def

Browse files
committed
feat: auto align news summary
1 parent 5be3e5a commit df27def

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

src/display/colorful.rs

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ use std::fmt::Display;
1919
use std::fmt::Write;
2020
use std::ops::Not;
2121
use std::path::PathBuf;
22-
use terminal_size::terminal_size;
2322
use unicode_width::UnicodeWidthStr;
2423

2524
#[inline]
@@ -306,7 +305,6 @@ pub fn search_post(result: &Result<(Vec<usize>, usize)>, rev: bool) -> Result<St
306305
)
307306
}
308307

309-
// TODO: auto align
310308
pub fn list_news(
311309
time_style: &TimeStyle,
312310
news_list: &Result<Vec<NewsEntry>>,
@@ -328,7 +326,20 @@ pub fn list_news(
328326
let url = format!("https://news.cnblogs.com/n/{}", news.id);
329327
writeln!(buf, "{} {}", create_time.dimmed(), url.dimmed())?;
330328
writeln!(buf, " {}", news.title)?;
331-
writeln!(buf, " {}{}", news.summary.dimmed(), "...".dimmed())?;
329+
330+
let summary = {
331+
let summary = format!("{}...", news.summary);
332+
summary.width_split(get_term_width() - 4).map_or_else(
333+
|| summary.clone(),
334+
|vec| {
335+
vec.into_iter()
336+
.map(|line| format!(" {}", line))
337+
.collect::<Vec<_>>()
338+
.join("\n")
339+
},
340+
)
341+
};
342+
writeln!(buf, "{}", summary.dimmed())?;
332343
}
333344
buf
334345
})

src/display/normal.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ use crate::infra::result::IntoResult;
1313
use crate::infra::str::StrExt;
1414
use crate::infra::terminal::get_term_width;
1515
use crate::infra::time::display_cnb_time;
16-
use anyhow::{Context, Result};
16+
use anyhow::Result;
1717
use std::fmt::{Display, Write};
1818
use std::ops::Not;
1919
use std::path::PathBuf;
20-
use terminal_size::terminal_size;
2120
use unicode_width::UnicodeWidthStr;
2221

2322
#[inline]
@@ -298,7 +297,6 @@ pub fn search_post(result: &Result<(Vec<usize>, usize)>, rev: bool) -> Result<St
298297
)
299298
}
300299

301-
// TODO: auto align
302300
pub fn list_news(
303301
time_style: &TimeStyle,
304302
news_list: &Result<Vec<NewsEntry>>,
@@ -320,7 +318,20 @@ pub fn list_news(
320318
let url = format!("https://news.cnblogs.com/n/{}", news.id);
321319
writeln!(buf, "{} {}", create_time, url)?;
322320
writeln!(buf, " {}", news.title)?;
323-
writeln!(buf, " {}...", news.summary)?;
321+
322+
let summary = {
323+
let summary = format!("{}...", news.summary);
324+
summary.width_split(get_term_width() - 4).map_or_else(
325+
|| summary.clone(),
326+
|vec| {
327+
vec.into_iter()
328+
.map(|line| format!(" {}", line))
329+
.collect::<Vec<_>>()
330+
.join("\n")
331+
},
332+
)
333+
};
334+
writeln!(buf, "{}", summary)?;
324335
}
325336
buf
326337
})

0 commit comments

Comments
 (0)