diff --git a/src/args/cmd/post.rs b/src/args/cmd/post.rs index 4cf1c2d..c9dd8bc 100644 --- a/src/args/cmd/post.rs +++ b/src/args/cmd/post.rs @@ -19,6 +19,15 @@ pub struct Opt { #[arg(visible_alias = "sm")] pub show_meta: bool, + #[arg(verbatim_doc_comment)] + /// Show comment list of post, order by time in DESC + /// Example: cnb --id 114514 post --show-comment + /// You should also specify the id of the post via --id + /// * + #[arg(long)] + #[arg(visible_alias = "sc")] + pub show_comment: bool, + #[arg(verbatim_doc_comment)] /// Show post list, order by time in DESC /// Example: cnb post --list diff --git a/src/args/parser.rs b/src/args/parser.rs index d9972ce..d4c7e73 100644 --- a/src/args/parser.rs +++ b/src/args/parser.rs @@ -180,6 +180,7 @@ pub fn show_post(args: &Args) -> Option { Some(Cmd::Post(cmd::post::Opt { show: true, show_meta: false, + show_comment: false, list: false, delete: false, search: None, @@ -207,6 +208,7 @@ pub fn show_post_meta(args: &Args) -> Option { Some(Cmd::Post(cmd::post::Opt { show: false, show_meta: true, + show_comment: false, list: false, delete: false, search: None, @@ -227,6 +229,34 @@ pub fn show_post_meta(args: &Args) -> Option { .into_some() } +pub fn show_post_comment(args: &Args) -> Option { + match args { + Args { + cmd: + Some(Cmd::Post(cmd::post::Opt { + show: false, + show_meta: false, + show_comment: true, + list: false, + delete: false, + search: None, + cmd: None, + })), + id: Some(id), + with_pat: _, + rev: _, + skip: None, + take: None, + debug: _, + style: _, + fail_on_error: _, + quiet: _, + } => *id, + _ => return None, + } + .into_some() +} + pub fn list_post(args: &Args) -> Option<(usize, usize)> { match args { Args { @@ -234,6 +264,7 @@ pub fn list_post(args: &Args) -> Option<(usize, usize)> { Some(Cmd::Post(cmd::post::Opt { show: false, show_meta: false, + show_comment: false, list: true, delete: false, search: None, @@ -265,6 +296,7 @@ pub fn delete_post(args: &Args) -> Option { Some(Cmd::Post(cmd::post::Opt { show: false, show_meta: false, + show_comment: false, list: false, delete: true, search: None, @@ -292,6 +324,7 @@ pub fn search_post(args: &Args) -> Option<(&String, usize, usize)> { Some(Cmd::Post(cmd::post::Opt { show: false, show_meta: false, + show_comment: false, list: false, delete: false, search: Some(keyword), @@ -323,6 +356,7 @@ pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> { Some(Cmd::Post(cmd::post::Opt { show: false, show_meta: false, + show_comment: false, list: false, delete: false, search: None, @@ -359,6 +393,7 @@ pub fn update_post( Some(Cmd::Post(cmd::post::Opt { show: false, show_meta: false, + show_comment: false, list: false, delete: false, search: None, diff --git a/src/display/colorful.rs b/src/display/colorful.rs index 7247764..14ddae3 100644 --- a/src/display/colorful.rs +++ b/src/display/colorful.rs @@ -4,6 +4,7 @@ use crate::api::ing::{ fmt_content, get_ing_at_user_tag_text, ing_star_tag_to_text, rm_ing_at_user_tag, IngSendFrom, }; use crate::api::news::get_list::NewsEntry; +use crate::api::post::get_comment_list::PostCommentEntry; use crate::api::post::get_one::PostEntry; use crate::api::user::info::UserInfo; use crate::infra::iter::IteratorExt; @@ -201,6 +202,29 @@ pub fn show_post_meta(entry: &Result) { println!("Link https:{}", entry.url); } +pub fn show_post_comment(comment_list: &Result>, rev: bool) { + let comment_list = match comment_list { + Ok(entry) => entry, + Err(e) => return println_err(e), + }; + + comment_list.iter().dyn_rev(rev).for_each(|comment| { + let create_time = { + let rfc3339 = patch_rfc3339(&comment.create_time); + let dt = DateTime::parse_from_rfc3339(&rfc3339) + .unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339)); + dt.format("%Y-%m-%d %H:%M") + }; + let floor_text = format!("{}F", comment.floor); + println!( + "{} {}", + create_time.to_string().dimmed(), + floor_text.dimmed() + ); + println!(" {} {}", comment.user_name.cyan(), comment.content); + }) +} + pub fn list_post(result: &Result<(Vec, usize)>, rev: bool) { let (entry_list, total_count) = match result { Ok(o) => o, diff --git a/src/display/json.rs b/src/display/json.rs index 08e8802..9c7d1fa 100644 --- a/src/display/json.rs +++ b/src/display/json.rs @@ -1,6 +1,7 @@ use crate::api::ing::get_comment_list::IngCommentEntry; use crate::api::ing::get_list::IngEntry; use crate::api::news::get_list::NewsEntry; +use crate::api::post::get_comment_list::PostCommentEntry; use crate::api::post::get_one::PostEntry; use crate::api::user::info::UserInfo; use crate::infra::iter::IteratorExt; @@ -59,6 +60,17 @@ pub fn show_post_meta(entry: &Result) { println_result(entry); } +pub fn show_post_comment(comment_list: &Result>, rev: bool) { + let comment_list = match comment_list { + Ok(entry) => entry, + Err(e) => return println_err(e), + }; + + let comment_vec = comment_list.iter().dyn_rev(rev).collect::>(); + let json = json::serialize(comment_vec).expect("Can not serialize comment_vec"); + print!("{}", json); +} + pub fn list_post(result: &Result<(Vec, usize)>, rev: bool) { let (entry_list, total_count) = match result { Ok(o) => o, diff --git a/src/display/mod.rs b/src/display/mod.rs index cd68785..194ec45 100644 --- a/src/display/mod.rs +++ b/src/display/mod.rs @@ -1,6 +1,7 @@ use crate::api::ing::get_comment_list::IngCommentEntry; use crate::api::ing::get_list::IngEntry; use crate::api::news::get_list::NewsEntry; +use crate::api::post::get_comment_list::PostCommentEntry; use crate::api::post::get_one::PostEntry; use crate::api::user::info::UserInfo; use crate::args::Style; @@ -80,6 +81,14 @@ pub fn show_post_meta(style: &Style, entry: &Result) { } } +pub fn show_post_comment(style: &Style, comment_list: &Result>, rev: bool) { + match style { + Style::Colorful => colorful::show_post_comment(comment_list, rev), + Style::Normal => normal::show_post_comment(comment_list, rev), + Style::Json => json::show_post_comment(comment_list, rev), + } +} + pub fn list_post(style: &Style, result: &Result<(Vec, usize)>, rev: bool) { match style { Style::Colorful => colorful::list_post(result, rev), diff --git a/src/display/normal.rs b/src/display/normal.rs index debe727..3b6c60a 100644 --- a/src/display/normal.rs +++ b/src/display/normal.rs @@ -4,6 +4,7 @@ use crate::api::ing::{ fmt_content, get_ing_at_user_tag_text, ing_star_tag_to_text, rm_ing_at_user_tag, IngSendFrom, }; use crate::api::news::get_list::NewsEntry; +use crate::api::post::get_comment_list::PostCommentEntry; use crate::api::post::get_one::PostEntry; use crate::api::user::info::UserInfo; use crate::infra::iter::IteratorExt; @@ -201,6 +202,24 @@ pub fn show_post_meta(entry: &Result) { println!("Link https:{}", entry.url); } +pub fn show_post_comment(comment_list: &Result>, rev: bool) { + let comment_list = match comment_list { + Ok(entry) => entry, + Err(e) => return println_err(e), + }; + + comment_list.iter().dyn_rev(rev).for_each(|comment| { + let create_time = { + let rfc3339 = patch_rfc3339(&comment.create_time); + let dt = DateTime::parse_from_rfc3339(&rfc3339) + .unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339)); + dt.format("%Y-%m-%d %H:%M") + }; + println!("{} {}F", create_time, comment.floor); + println!(" {} {}", comment.user_name, comment.content); + }) +} + pub fn list_post(result: &Result<(Vec, usize)>, rev: bool) { let (entry_list, total_count) = match result { Ok(o) => o, diff --git a/src/main.rs b/src/main.rs index ef0186d..1eb405d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -110,6 +110,11 @@ async fn main() -> Result<()> { foe.then(|| panic_if_err(&entry)); quiet.not().then(|| display::show_post_meta(style, &entry)); } + _ if let Some(id) = parser::show_post_comment(&args) => { + let comment_vec = Post::new(pat?).get_comment_list(id).await; + foe.then(|| panic_if_err(&comment_vec)); + quiet.not().then(|| display::show_post_comment(style, &comment_vec, rev)); + } _ if let Some((skip, take)) = parser::list_post(&args) => { let meta_vec = Post::new(pat?).get_meta_list(skip,take).await; foe.then(|| panic_if_err(&meta_vec));