Skip to content

Commit fddb65d

Browse files
committed
refactor: lift out rev
1 parent 57680ea commit fddb65d

File tree

6 files changed

+324
-333
lines changed

6 files changed

+324
-333
lines changed

src/display/colorful.rs

Lines changed: 114 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::api::post::get_comment_list::PostCommentEntry;
99
use crate::api::post::get_one::PostEntry;
1010
use crate::api::user::info::UserInfo;
1111
use crate::args::TimeStyle;
12-
use crate::infra::iter::IteratorExt;
1312
use crate::infra::result::IntoResult;
1413
use crate::infra::str::StrExt;
1514
use crate::infra::terminal::get_term_width;
@@ -78,103 +77,94 @@ pub fn user_info(info: &Result<UserInfo>) -> Result<String> {
7877
// TODO: rm unnecessary line divider
7978
pub fn list_ing(
8079
time_style: &TimeStyle,
81-
ing_with_comment_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
82-
rev: bool,
80+
ing_with_comment_iter: Result<impl ExactSizeIterator<Item = (IngEntry, Vec<IngCommentEntry>)>>,
8381
align: bool,
8482
) -> Result<String> {
85-
let ing_with_comment_list = match ing_with_comment_list {
83+
let mut ing_with_comment_iter = match ing_with_comment_iter {
8684
Ok(o) => o,
87-
Err(e) => return fmt_err(e).into_ok(),
85+
Err(e) => return fmt_err(&e).into_ok(),
8886
};
8987

90-
ing_with_comment_list.iter().dyn_rev(rev).try_fold(
91-
String::new(),
92-
|mut buf, (ing, comment_list)| try {
93-
{
94-
let buf = &mut buf;
95-
let create_time = display_cnb_time(&ing.create_time, time_style);
96-
write!(buf, "{}", create_time.dimmed())?;
88+
ing_with_comment_iter.try_fold(String::new(), |mut buf, (ing, comment_list)| try {
89+
{
90+
let buf = &mut buf;
91+
let create_time = display_cnb_time(&ing.create_time, time_style);
92+
write!(buf, "{}", create_time.dimmed())?;
9793

98-
let send_from_mark = match ing.send_from {
99-
IngSendFrom::Cli => Some("CLI"),
100-
IngSendFrom::CellPhone => Some("Mobile"),
101-
IngSendFrom::VsCode => Some("VSCode"),
102-
IngSendFrom::Web => Some("Web"),
103-
_ => None,
104-
};
105-
if let Some(mark) = send_from_mark {
106-
write!(buf, " {}", mark.dimmed())?;
107-
}
108-
if ing.is_lucky {
109-
let star_text = ing_star_tag_to_text(&ing.icons);
110-
write!(buf, " {}⭐", star_text.yellow())?;
111-
}
112-
writeln!(buf, " {} {}", "#".dimmed(), ing.id.to_string().dimmed())?;
113-
let content = if align {
114-
let user_name_width = ing.user_name.width_cjk();
115-
let left_width = get_term_width().saturating_sub(user_name_width + 3);
116-
fmt_content(&ing.content)
117-
.width_split(left_width)
118-
.map_or_else(
119-
|| ing.content.clone(),
120-
|lines| {
121-
if comment_list.is_empty().not() {
122-
lines.join("\n").replace(
123-
'\n',
124-
&format!("\n │{}", " ".repeat(user_name_width - 2)),
125-
)
126-
} else {
127-
lines.join("\n").replace(
128-
'\n',
129-
&format!("\n{}", " ".repeat(user_name_width + 3)),
130-
)
131-
}
132-
},
133-
)
134-
} else {
135-
fmt_content(&ing.content)
136-
};
137-
writeln!(buf, " {} {}", ing.user_name.cyan(), content)?;
94+
let send_from_mark = match ing.send_from {
95+
IngSendFrom::Cli => Some("CLI"),
96+
IngSendFrom::CellPhone => Some("Mobile"),
97+
IngSendFrom::VsCode => Some("VSCode"),
98+
IngSendFrom::Web => Some("Web"),
99+
_ => None,
100+
};
101+
if let Some(mark) = send_from_mark {
102+
write!(buf, " {}", mark.dimmed())?;
103+
}
104+
if ing.is_lucky {
105+
let star_text = ing_star_tag_to_text(&ing.icons);
106+
write!(buf, " {}⭐", star_text.yellow())?;
107+
}
108+
writeln!(buf, " {} {}", "#".dimmed(), ing.id.to_string().dimmed())?;
109+
let content = if align {
110+
let user_name_width = ing.user_name.width_cjk();
111+
let left_width = get_term_width().saturating_sub(user_name_width + 3);
112+
fmt_content(&ing.content)
113+
.width_split(left_width)
114+
.map_or_else(
115+
|| ing.content.clone(),
116+
|lines| {
117+
if comment_list.is_empty().not() {
118+
lines.join("\n").replace(
119+
'\n',
120+
&format!("\n │{}", " ".repeat(user_name_width - 2)),
121+
)
122+
} else {
123+
lines.join("\n").replace(
124+
'\n',
125+
&format!("\n{}", " ".repeat(user_name_width + 3)),
126+
)
127+
}
128+
},
129+
)
130+
} else {
131+
fmt_content(&ing.content)
132+
};
133+
writeln!(buf, " {} {}", ing.user_name.cyan(), content)?;
138134

139-
let len = comment_list.len();
140-
if len != 0 {
141-
let max_i = len - 1;
142-
let comment_list_buf: Result<String> = comment_list
143-
.iter()
144-
.enumerate()
145-
.try_fold(String::new(), |mut buf, (i, entry)| try {
146-
{
147-
let buf = &mut buf;
148-
if i != max_i {
149-
write!(buf, " │ {}", entry.user_name.blue())?;
150-
} else {
151-
write!(buf, " └ {}", entry.user_name.blue())?;
152-
}
153-
let at_user = get_ing_at_user_tag_text(&entry.content);
154-
if at_user.is_empty().not() {
155-
write!(
156-
buf,
157-
" {}{}",
158-
"@".bright_black(),
159-
at_user.bright_black()
160-
)?;
161-
}
162-
let content = {
163-
let content = rm_ing_at_user_tag(&entry.content);
164-
fmt_content(&content)
165-
};
166-
writeln!(buf, " {}", content.dimmed())?;
135+
let len = comment_list.len();
136+
if len != 0 {
137+
let max_i = len - 1;
138+
let comment_list_buf: Result<String> = comment_list.iter().enumerate().try_fold(
139+
String::new(),
140+
|mut buf, (i, entry)| try {
141+
{
142+
let buf = &mut buf;
143+
if i != max_i {
144+
write!(buf, " │ {}", entry.user_name.blue())?;
145+
} else {
146+
write!(buf, " └ {}", entry.user_name.blue())?;
167147
}
168-
buf
169-
});
170-
write!(buf, "{}", comment_list_buf?)?;
171-
}
148+
let at_user = get_ing_at_user_tag_text(&entry.content);
149+
if at_user.is_empty().not() {
150+
write!(buf, " {}{}", "@".bright_black(), at_user.bright_black())?;
151+
}
152+
let content = {
153+
let content = rm_ing_at_user_tag(&entry.content);
154+
fmt_content(&content)
155+
};
156+
writeln!(buf, " {}", content.dimmed())?;
157+
}
158+
buf
159+
},
160+
);
161+
write!(buf, "{}", comment_list_buf?)?;
162+
}
172163

173-
writeln!(buf)?;
174-
};
175-
buf
176-
},
177-
)
164+
writeln!(buf)?;
165+
};
166+
buf
167+
})
178168
}
179169

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

241231
pub fn show_post_comment(
242232
time_style: &TimeStyle,
243-
comment_list: &Result<Vec<PostCommentEntry>>,
244-
rev: bool,
233+
comment_iter: Result<impl ExactSizeIterator<Item = PostCommentEntry>>,
245234
) -> Result<String> {
246-
let comment_list = match comment_list {
235+
let mut comment_iter = match comment_iter {
247236
Ok(entry) => entry,
248-
Err(e) => return fmt_err(e).into_ok(),
237+
Err(e) => return fmt_err(&e).into_ok(),
249238
};
250239

251-
comment_list
252-
.iter()
253-
.dyn_rev(rev)
254-
.try_fold(String::new(), |mut buf, comment| try {
255-
{
256-
let buf = &mut buf;
257-
let create_time = display_cnb_time(&comment.create_time, time_style);
258-
let floor_text = format!("{}F", comment.floor);
259-
writeln!(buf, "{} {}", create_time.dimmed(), floor_text.dimmed())?;
260-
writeln!(buf, " {} {}", comment.user_name.cyan(), comment.content)?;
261-
}
262-
buf
263-
})
240+
comment_iter.try_fold(String::new(), |mut buf, comment| try {
241+
{
242+
let buf = &mut buf;
243+
let create_time = display_cnb_time(&comment.create_time, time_style);
244+
let floor_text = format!("{}F", comment.floor);
245+
writeln!(buf, "{} {}", create_time.dimmed(), floor_text.dimmed())?;
246+
writeln!(buf, " {} {}", comment.user_name.cyan(), comment.content)?;
247+
}
248+
buf
249+
})
264250
}
265251

266-
pub fn list_post(result: &Result<(Vec<PostEntry>, usize)>, rev: bool) -> Result<String> {
267-
let (entry_list, total_count) = match result {
252+
pub fn list_post(
253+
result: Result<(impl ExactSizeIterator<Item = PostEntry>, usize)>,
254+
) -> Result<String> {
255+
let (mut entry_iter, total_count) = match result {
268256
Ok(o) => o,
269-
Err(e) => return fmt_err(e).into_ok(),
257+
Err(e) => return fmt_err(&e).into_ok(),
270258
};
271259

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

294-
pub fn search_post(result: &Result<(Vec<usize>, usize)>, rev: bool) -> Result<String> {
295-
let (id_list, total_count) = match result {
282+
pub fn search_post(
283+
result: Result<(impl ExactSizeIterator<Item = usize>, usize)>,
284+
) -> Result<String> {
285+
let (mut id_iter, total_count) = match result {
296286
Ok(o) => o,
297-
Err(e) => return fmt_err(e).into_ok(),
287+
Err(e) => return fmt_err(&e).into_ok(),
298288
};
299289

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

309299
pub fn list_news(
310300
time_style: &TimeStyle,
311-
news_list: &Result<Vec<NewsEntry>>,
312-
rev: bool,
301+
news_iter: Result<impl ExactSizeIterator<Item = NewsEntry>>,
313302
) -> Result<String> {
314-
let news_list = match news_list {
303+
let news_iter = match news_iter {
315304
Ok(o) => o,
316-
Err(e) => return fmt_err(e).into_ok(),
305+
Err(e) => return fmt_err(&e).into_ok(),
317306
};
318307

319-
news_list
320-
.iter()
321-
.dyn_rev(rev)
308+
news_iter
322309
.map(|news| try {
323310
let mut buf = String::new();
324311
{
@@ -350,20 +337,16 @@ pub fn list_news(
350337
})
351338
}
352339

353-
// TODO: lift out rev option
354340
pub fn list_fav(
355341
time_style: &TimeStyle,
356-
fav_list: &Result<Vec<FavEntry>>,
357-
rev: bool,
342+
fav_iter: Result<impl ExactSizeIterator<Item = FavEntry>>,
358343
) -> Result<String> {
359-
let fav_list = match fav_list {
344+
let fav_iter = match fav_iter {
360345
Ok(o) => o,
361-
Err(e) => return fmt_err(e).into_ok(),
346+
Err(e) => return fmt_err(&e).into_ok(),
362347
};
363348

364-
fav_list
365-
.iter()
366-
.dyn_rev(rev)
349+
fav_iter
367350
.map(|fav| try {
368351
let mut buf = String::new();
369352
{

0 commit comments

Comments
 (0)