Skip to content

Commit

Permalink
refactor: lift out rev
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Sep 27, 2023
1 parent 57680ea commit fddb65d
Show file tree
Hide file tree
Showing 6 changed files with 324 additions and 333 deletions.
245 changes: 114 additions & 131 deletions src/display/colorful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::api::post::get_comment_list::PostCommentEntry;
use crate::api::post::get_one::PostEntry;
use crate::api::user::info::UserInfo;
use crate::args::TimeStyle;
use crate::infra::iter::IteratorExt;
use crate::infra::result::IntoResult;
use crate::infra::str::StrExt;
use crate::infra::terminal::get_term_width;
Expand Down Expand Up @@ -78,103 +77,94 @@ pub fn user_info(info: &Result<UserInfo>) -> Result<String> {
// TODO: rm unnecessary line divider
pub fn list_ing(
time_style: &TimeStyle,
ing_with_comment_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
rev: bool,
ing_with_comment_iter: Result<impl ExactSizeIterator<Item = (IngEntry, Vec<IngCommentEntry>)>>,
align: bool,
) -> Result<String> {
let ing_with_comment_list = match ing_with_comment_list {
let mut ing_with_comment_iter = match ing_with_comment_iter {
Ok(o) => o,
Err(e) => return fmt_err(e).into_ok(),
Err(e) => return fmt_err(&e).into_ok(),
};

ing_with_comment_list.iter().dyn_rev(rev).try_fold(
String::new(),
|mut buf, (ing, comment_list)| try {
{
let buf = &mut buf;
let create_time = display_cnb_time(&ing.create_time, time_style);
write!(buf, "{}", create_time.dimmed())?;
ing_with_comment_iter.try_fold(String::new(), |mut buf, (ing, comment_list)| try {
{
let buf = &mut buf;
let create_time = display_cnb_time(&ing.create_time, time_style);
write!(buf, "{}", create_time.dimmed())?;

let send_from_mark = match ing.send_from {
IngSendFrom::Cli => Some("CLI"),
IngSendFrom::CellPhone => Some("Mobile"),
IngSendFrom::VsCode => Some("VSCode"),
IngSendFrom::Web => Some("Web"),
_ => None,
};
if let Some(mark) = send_from_mark {
write!(buf, " {}", mark.dimmed())?;
}
if ing.is_lucky {
let star_text = ing_star_tag_to_text(&ing.icons);
write!(buf, " {}⭐", star_text.yellow())?;
}
writeln!(buf, " {} {}", "#".dimmed(), ing.id.to_string().dimmed())?;
let content = if align {
let user_name_width = ing.user_name.width_cjk();
let left_width = get_term_width().saturating_sub(user_name_width + 3);
fmt_content(&ing.content)
.width_split(left_width)
.map_or_else(
|| ing.content.clone(),
|lines| {
if comment_list.is_empty().not() {
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 {
fmt_content(&ing.content)
};
writeln!(buf, " {} {}", ing.user_name.cyan(), content)?;
let send_from_mark = match ing.send_from {
IngSendFrom::Cli => Some("CLI"),
IngSendFrom::CellPhone => Some("Mobile"),
IngSendFrom::VsCode => Some("VSCode"),
IngSendFrom::Web => Some("Web"),
_ => None,
};
if let Some(mark) = send_from_mark {
write!(buf, " {}", mark.dimmed())?;
}
if ing.is_lucky {
let star_text = ing_star_tag_to_text(&ing.icons);
write!(buf, " {}⭐", star_text.yellow())?;
}
writeln!(buf, " {} {}", "#".dimmed(), ing.id.to_string().dimmed())?;
let content = if align {
let user_name_width = ing.user_name.width_cjk();
let left_width = get_term_width().saturating_sub(user_name_width + 3);
fmt_content(&ing.content)
.width_split(left_width)
.map_or_else(
|| ing.content.clone(),
|lines| {
if comment_list.is_empty().not() {
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 {
fmt_content(&ing.content)
};
writeln!(buf, " {} {}", ing.user_name.cyan(), content)?;

let len = comment_list.len();
if len != 0 {
let max_i = len - 1;
let comment_list_buf: Result<String> = comment_list
.iter()
.enumerate()
.try_fold(String::new(), |mut buf, (i, entry)| try {
{
let buf = &mut buf;
if i != max_i {
write!(buf, " │ {}", entry.user_name.blue())?;
} else {
write!(buf, " └ {}", entry.user_name.blue())?;
}
let at_user = get_ing_at_user_tag_text(&entry.content);
if at_user.is_empty().not() {
write!(
buf,
" {}{}",
"@".bright_black(),
at_user.bright_black()
)?;
}
let content = {
let content = rm_ing_at_user_tag(&entry.content);
fmt_content(&content)
};
writeln!(buf, " {}", content.dimmed())?;
let len = comment_list.len();
if len != 0 {
let max_i = len - 1;
let comment_list_buf: Result<String> = comment_list.iter().enumerate().try_fold(
String::new(),
|mut buf, (i, entry)| try {
{
let buf = &mut buf;
if i != max_i {
write!(buf, " │ {}", entry.user_name.blue())?;
} else {
write!(buf, " └ {}", entry.user_name.blue())?;
}
buf
});
write!(buf, "{}", comment_list_buf?)?;
}
let at_user = get_ing_at_user_tag_text(&entry.content);
if at_user.is_empty().not() {
write!(buf, " {}{}", "@".bright_black(), at_user.bright_black())?;
}
let content = {
let content = rm_ing_at_user_tag(&entry.content);
fmt_content(&content)
};
writeln!(buf, " {}", content.dimmed())?;
}
buf
},
);
write!(buf, "{}", comment_list_buf?)?;
}

writeln!(buf)?;
};
buf
},
)
writeln!(buf)?;
};
buf
})
}

pub fn show_post(entry: &Result<PostEntry>) -> Result<String> {
Expand Down Expand Up @@ -240,37 +230,35 @@ pub fn show_post_meta(time_style: &TimeStyle, entry: &Result<PostEntry>) -> Resu

pub fn show_post_comment(
time_style: &TimeStyle,
comment_list: &Result<Vec<PostCommentEntry>>,
rev: bool,
comment_iter: Result<impl ExactSizeIterator<Item = PostCommentEntry>>,
) -> Result<String> {
let comment_list = match comment_list {
let mut comment_iter = match comment_iter {
Ok(entry) => entry,
Err(e) => return fmt_err(e).into_ok(),
Err(e) => return fmt_err(&e).into_ok(),
};

comment_list
.iter()
.dyn_rev(rev)
.try_fold(String::new(), |mut buf, comment| try {
{
let buf = &mut buf;
let create_time = display_cnb_time(&comment.create_time, time_style);
let floor_text = format!("{}F", comment.floor);
writeln!(buf, "{} {}", create_time.dimmed(), floor_text.dimmed())?;
writeln!(buf, " {} {}", comment.user_name.cyan(), comment.content)?;
}
buf
})
comment_iter.try_fold(String::new(), |mut buf, comment| try {
{
let buf = &mut buf;
let create_time = display_cnb_time(&comment.create_time, time_style);
let floor_text = format!("{}F", comment.floor);
writeln!(buf, "{} {}", create_time.dimmed(), floor_text.dimmed())?;
writeln!(buf, " {} {}", comment.user_name.cyan(), comment.content)?;
}
buf
})
}

pub fn list_post(result: &Result<(Vec<PostEntry>, usize)>, rev: bool) -> Result<String> {
let (entry_list, total_count) = match result {
pub fn list_post(
result: Result<(impl ExactSizeIterator<Item = PostEntry>, usize)>,
) -> Result<String> {
let (mut entry_iter, total_count) = match result {
Ok(o) => o,
Err(e) => return fmt_err(e).into_ok(),
Err(e) => return fmt_err(&e).into_ok(),
};

entry_list.iter().dyn_rev(rev).try_fold(
format!("{}/{}\n", entry_list.len(), total_count),
entry_iter.try_fold(
format!("{}/{}\n", entry_iter.len(), total_count),
|mut buf, entry| try {
{
let buf = &mut buf;
Expand All @@ -291,14 +279,16 @@ pub fn list_post(result: &Result<(Vec<PostEntry>, usize)>, rev: bool) -> Result<
)
}

pub fn search_post(result: &Result<(Vec<usize>, usize)>, rev: bool) -> Result<String> {
let (id_list, total_count) = match result {
pub fn search_post(
result: Result<(impl ExactSizeIterator<Item = usize>, usize)>,
) -> Result<String> {
let (mut id_iter, total_count) = match result {
Ok(o) => o,
Err(e) => return fmt_err(e).into_ok(),
Err(e) => return fmt_err(&e).into_ok(),
};

id_list.iter().dyn_rev(rev).try_fold(
format!("{}/{}\n", id_list.len(), total_count),
id_iter.try_fold(
format!("{}/{}\n", id_iter.len(), total_count),
|mut buf, id| try {
writeln!(&mut buf, "# {}", id)?;
buf
Expand All @@ -308,17 +298,14 @@ pub fn search_post(result: &Result<(Vec<usize>, usize)>, rev: bool) -> Result<St

pub fn list_news(
time_style: &TimeStyle,
news_list: &Result<Vec<NewsEntry>>,
rev: bool,
news_iter: Result<impl ExactSizeIterator<Item = NewsEntry>>,
) -> Result<String> {
let news_list = match news_list {
let news_iter = match news_iter {
Ok(o) => o,
Err(e) => return fmt_err(e).into_ok(),
Err(e) => return fmt_err(&e).into_ok(),
};

news_list
.iter()
.dyn_rev(rev)
news_iter
.map(|news| try {
let mut buf = String::new();
{
Expand Down Expand Up @@ -350,20 +337,16 @@ pub fn list_news(
})
}

// TODO: lift out rev option
pub fn list_fav(
time_style: &TimeStyle,
fav_list: &Result<Vec<FavEntry>>,
rev: bool,
fav_iter: Result<impl ExactSizeIterator<Item = FavEntry>>,
) -> Result<String> {
let fav_list = match fav_list {
let fav_iter = match fav_iter {
Ok(o) => o,
Err(e) => return fmt_err(e).into_ok(),
Err(e) => return fmt_err(&e).into_ok(),
};

fav_list
.iter()
.dyn_rev(rev)
fav_iter
.map(|fav| try {
let mut buf = String::new();
{
Expand Down
Loading

0 comments on commit fddb65d

Please sign in to comment.