Skip to content

Commit

Permalink
feat(ing list): content auto wrap
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Sep 19, 2023
1 parent d0deb8c commit 1a642fc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
19 changes: 15 additions & 4 deletions src/display/colorful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::fmt::Display;
use std::ops::Not;
use std::path::PathBuf;
use unicode_width::UnicodeWidthStr;
use crate::infra::str::StrExt;

pub fn login(cfg_path: &Result<PathBuf>) {
match cfg_path {
Expand Down Expand Up @@ -82,10 +83,20 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: b
print!(" {}⭐", star_text.yellow());
}
println!(" {} {}", "#".dimmed(), ing.id.to_string().dimmed());
let user_name_width = ing.user_name.width_cjk();
let content = if align {
fmt_content(&ing.content)
.replace('\n', &format!("\n{}", " ".repeat(user_name_width + 3)))
let user_name_width = ing.user_name.width_cjk();
let (term_width, _) = term_size::dimensions()
.expect("Can not get terminal size");
let left_width = term_width.checked_sub(user_name_width + 3).unwrap_or(0);
if let Some(lines) = fmt_content(&ing.content).width_split(left_width) {
if comment_list.len() > 0 {
lines.join("\n").replace("\n", &format!("\n │{}", " ".repeat(user_name_width - 2)))
} else {
lines.join("\n").replace("\n", &format!("\n{}", " ".repeat(user_name_width + 3)))
}
} else {
ing.content.clone()
}
} else {
fmt_content(&ing.content)
};
Expand Down Expand Up @@ -237,7 +248,7 @@ pub fn list_news(news_list: &Result<Vec<NewsEntry>>, 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!();
Expand Down
17 changes: 14 additions & 3 deletions src/display/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use std::fmt::Display;
use std::ops::Not;
use std::path::PathBuf;
use unicode_width::UnicodeWidthStr;
use crate::infra::str::StrExt;

pub fn login(cfg_path: &Result<PathBuf>) {
match cfg_path {
Expand Down Expand Up @@ -82,10 +83,20 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: b
print!(" {}⭐", star_text);
}
println!(" # {}", ing.id);
let user_name_width = ing.user_name.width_cjk();
let content = if align {
fmt_content(&ing.content)
.replace('\n', &format!("\n{}", " ".repeat(user_name_width + 3)))
let user_name_width = ing.user_name.width_cjk();
let (term_width, _) = term_size::dimensions()
.expect("Can not get terminal size");
let left_width = term_width.checked_sub(user_name_width + 3).unwrap_or(0);
if let Some(lines) = fmt_content(&ing.content).width_split(left_width) {
if comment_list.len() > 0 {
lines.join("\n").replace("\n", &format!("\n │{}", " ".repeat(user_name_width - 2)))
} else {
lines.join("\n").replace("\n", &format!("\n{}", " ".repeat(user_name_width + 3)))
}
} else {
ing.content.clone()
}
} else {
fmt_content(&ing.content)
};
Expand Down

0 comments on commit 1a642fc

Please sign in to comment.