Skip to content

Commit bdae7c3

Browse files
committed
feat: show post comment list
1 parent a06bd21 commit bdae7c3

File tree

7 files changed

+113
-0
lines changed

7 files changed

+113
-0
lines changed

src/args/cmd/post.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ pub struct Opt {
1919
#[arg(visible_alias = "sm")]
2020
pub show_meta: bool,
2121

22+
#[arg(verbatim_doc_comment)]
23+
/// Show comment list of post, order by time in DESC
24+
/// Example: cnb --id 114514 post --show-comment
25+
/// You should also specify the id of the post via --id
26+
/// *
27+
#[arg(long)]
28+
#[arg(visible_alias = "sc")]
29+
pub show_comment: bool,
30+
2231
#[arg(verbatim_doc_comment)]
2332
/// Show post list, order by time in DESC
2433
/// Example: cnb post --list

src/args/parser.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ pub fn show_post(args: &Args) -> Option<usize> {
180180
Some(Cmd::Post(cmd::post::Opt {
181181
show: true,
182182
show_meta: false,
183+
show_comment: false,
183184
list: false,
184185
delete: false,
185186
search: None,
@@ -207,6 +208,7 @@ pub fn show_post_meta(args: &Args) -> Option<usize> {
207208
Some(Cmd::Post(cmd::post::Opt {
208209
show: false,
209210
show_meta: true,
211+
show_comment: false,
210212
list: false,
211213
delete: false,
212214
search: None,
@@ -227,13 +229,42 @@ pub fn show_post_meta(args: &Args) -> Option<usize> {
227229
.into_some()
228230
}
229231

232+
pub fn show_post_comment(args: &Args) -> Option<usize> {
233+
match args {
234+
Args {
235+
cmd:
236+
Some(Cmd::Post(cmd::post::Opt {
237+
show: false,
238+
show_meta: false,
239+
show_comment: true,
240+
list: false,
241+
delete: false,
242+
search: None,
243+
cmd: None,
244+
})),
245+
id: Some(id),
246+
with_pat: _,
247+
rev: _,
248+
skip: None,
249+
take: None,
250+
debug: _,
251+
style: _,
252+
fail_on_error: _,
253+
quiet: _,
254+
} => *id,
255+
_ => return None,
256+
}
257+
.into_some()
258+
}
259+
230260
pub fn list_post(args: &Args) -> Option<(usize, usize)> {
231261
match args {
232262
Args {
233263
cmd:
234264
Some(Cmd::Post(cmd::post::Opt {
235265
show: false,
236266
show_meta: false,
267+
show_comment: false,
237268
list: true,
238269
delete: false,
239270
search: None,
@@ -265,6 +296,7 @@ pub fn delete_post(args: &Args) -> Option<usize> {
265296
Some(Cmd::Post(cmd::post::Opt {
266297
show: false,
267298
show_meta: false,
299+
show_comment: false,
268300
list: false,
269301
delete: true,
270302
search: None,
@@ -292,6 +324,7 @@ pub fn search_post(args: &Args) -> Option<(&String, usize, usize)> {
292324
Some(Cmd::Post(cmd::post::Opt {
293325
show: false,
294326
show_meta: false,
327+
show_comment: false,
295328
list: false,
296329
delete: false,
297330
search: Some(keyword),
@@ -323,6 +356,7 @@ pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> {
323356
Some(Cmd::Post(cmd::post::Opt {
324357
show: false,
325358
show_meta: false,
359+
show_comment: false,
326360
list: false,
327361
delete: false,
328362
search: None,
@@ -359,6 +393,7 @@ pub fn update_post(
359393
Some(Cmd::Post(cmd::post::Opt {
360394
show: false,
361395
show_meta: false,
396+
show_comment: false,
362397
list: false,
363398
delete: false,
364399
search: None,

src/display/colorful.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::api::ing::{
44
fmt_content, get_ing_at_user_tag_text, ing_star_tag_to_text, rm_ing_at_user_tag, IngSendFrom,
55
};
66
use crate::api::news::get_list::NewsEntry;
7+
use crate::api::post::get_comment_list::PostCommentEntry;
78
use crate::api::post::get_one::PostEntry;
89
use crate::api::user::info::UserInfo;
910
use crate::infra::iter::IteratorExt;
@@ -201,6 +202,29 @@ pub fn show_post_meta(entry: &Result<PostEntry>) {
201202
println!("Link https:{}", entry.url);
202203
}
203204

205+
pub fn show_post_comment(comment_list: &Result<Vec<PostCommentEntry>>, rev: bool) {
206+
let comment_list = match comment_list {
207+
Ok(entry) => entry,
208+
Err(e) => return println_err(e),
209+
};
210+
211+
comment_list.iter().dyn_rev(rev).for_each(|comment| {
212+
let create_time = {
213+
let rfc3339 = patch_rfc3339(&comment.create_time);
214+
let dt = DateTime::parse_from_rfc3339(&rfc3339)
215+
.unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339));
216+
dt.format("%Y-%m-%d %H:%M")
217+
};
218+
let floor_text = format!("{}F", comment.floor);
219+
println!(
220+
"{} {}",
221+
create_time.to_string().dimmed(),
222+
floor_text.dimmed()
223+
);
224+
println!(" {} {}", comment.user_name.cyan(), comment.content);
225+
})
226+
}
227+
204228
pub fn list_post(result: &Result<(Vec<PostEntry>, usize)>, rev: bool) {
205229
let (entry_list, total_count) = match result {
206230
Ok(o) => o,

src/display/json.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::api::ing::get_comment_list::IngCommentEntry;
22
use crate::api::ing::get_list::IngEntry;
33
use crate::api::news::get_list::NewsEntry;
4+
use crate::api::post::get_comment_list::PostCommentEntry;
45
use crate::api::post::get_one::PostEntry;
56
use crate::api::user::info::UserInfo;
67
use crate::infra::iter::IteratorExt;
@@ -59,6 +60,17 @@ pub fn show_post_meta(entry: &Result<PostEntry>) {
5960
println_result(entry);
6061
}
6162

63+
pub fn show_post_comment(comment_list: &Result<Vec<PostCommentEntry>>, rev: bool) {
64+
let comment_list = match comment_list {
65+
Ok(entry) => entry,
66+
Err(e) => return println_err(e),
67+
};
68+
69+
let comment_vec = comment_list.iter().dyn_rev(rev).collect::<Vec<_>>();
70+
let json = json::serialize(comment_vec).expect("Can not serialize comment_vec");
71+
print!("{}", json);
72+
}
73+
6274
pub fn list_post(result: &Result<(Vec<PostEntry>, usize)>, rev: bool) {
6375
let (entry_list, total_count) = match result {
6476
Ok(o) => o,

src/display/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::api::ing::get_comment_list::IngCommentEntry;
22
use crate::api::ing::get_list::IngEntry;
33
use crate::api::news::get_list::NewsEntry;
4+
use crate::api::post::get_comment_list::PostCommentEntry;
45
use crate::api::post::get_one::PostEntry;
56
use crate::api::user::info::UserInfo;
67
use crate::args::Style;
@@ -80,6 +81,14 @@ pub fn show_post_meta(style: &Style, entry: &Result<PostEntry>) {
8081
}
8182
}
8283

84+
pub fn show_post_comment(style: &Style, comment_list: &Result<Vec<PostCommentEntry>>, rev: bool) {
85+
match style {
86+
Style::Colorful => colorful::show_post_comment(comment_list, rev),
87+
Style::Normal => normal::show_post_comment(comment_list, rev),
88+
Style::Json => json::show_post_comment(comment_list, rev),
89+
}
90+
}
91+
8392
pub fn list_post(style: &Style, result: &Result<(Vec<PostEntry>, usize)>, rev: bool) {
8493
match style {
8594
Style::Colorful => colorful::list_post(result, rev),

src/display/normal.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::api::ing::{
44
fmt_content, get_ing_at_user_tag_text, ing_star_tag_to_text, rm_ing_at_user_tag, IngSendFrom,
55
};
66
use crate::api::news::get_list::NewsEntry;
7+
use crate::api::post::get_comment_list::PostCommentEntry;
78
use crate::api::post::get_one::PostEntry;
89
use crate::api::user::info::UserInfo;
910
use crate::infra::iter::IteratorExt;
@@ -201,6 +202,24 @@ pub fn show_post_meta(entry: &Result<PostEntry>) {
201202
println!("Link https:{}", entry.url);
202203
}
203204

205+
pub fn show_post_comment(comment_list: &Result<Vec<PostCommentEntry>>, rev: bool) {
206+
let comment_list = match comment_list {
207+
Ok(entry) => entry,
208+
Err(e) => return println_err(e),
209+
};
210+
211+
comment_list.iter().dyn_rev(rev).for_each(|comment| {
212+
let create_time = {
213+
let rfc3339 = patch_rfc3339(&comment.create_time);
214+
let dt = DateTime::parse_from_rfc3339(&rfc3339)
215+
.unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339));
216+
dt.format("%Y-%m-%d %H:%M")
217+
};
218+
println!("{} {}F", create_time, comment.floor);
219+
println!(" {} {}", comment.user_name, comment.content);
220+
})
221+
}
222+
204223
pub fn list_post(result: &Result<(Vec<PostEntry>, usize)>, rev: bool) {
205224
let (entry_list, total_count) = match result {
206225
Ok(o) => o,

src/main.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ async fn main() -> Result<()> {
110110
foe.then(|| panic_if_err(&entry));
111111
quiet.not().then(|| display::show_post_meta(style, &entry));
112112
}
113+
_ if let Some(id) = parser::show_post_comment(&args) => {
114+
let comment_vec = Post::new(pat?).get_comment_list(id).await;
115+
foe.then(|| panic_if_err(&comment_vec));
116+
quiet.not().then(|| display::show_post_comment(style, &comment_vec, rev));
117+
}
113118
_ if let Some((skip, take)) = parser::list_post(&args) => {
114119
let meta_vec = Post::new(pat?).get_meta_list(skip,take).await;
115120
foe.then(|| panic_if_err(&meta_vec));

0 commit comments

Comments
 (0)