Skip to content

Commit

Permalink
feat: auto align news summary
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Sep 25, 2023
1 parent 5be3e5a commit 6f782b0
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
16 changes: 14 additions & 2 deletions src/display/colorful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ pub fn search_post(result: &Result<(Vec<usize>, usize)>, rev: bool) -> Result<St
)
}

// TODO: auto align
pub fn list_news(
time_style: &TimeStyle,
news_list: &Result<Vec<NewsEntry>>,
Expand All @@ -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::<Vec<_>>()
.join("\n")
},
)
};
writeln!(buf, "{}", summary.dimmed())?;
}
buf
})
Expand Down
16 changes: 14 additions & 2 deletions src/display/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ pub fn search_post(result: &Result<(Vec<usize>, usize)>, rev: bool) -> Result<St
)
}

// TODO: auto align
pub fn list_news(
time_style: &TimeStyle,
news_list: &Result<Vec<NewsEntry>>,
Expand All @@ -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::<Vec<_>>()
.join("\n")
},
)
};
writeln!(buf, "{}", summary)?;
}
buf
})
Expand Down

0 comments on commit 6f782b0

Please sign in to comment.