Skip to content

Commit

Permalink
feat: show post comment list
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Sep 20, 2023
1 parent a06bd21 commit bdae7c3
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/args/cmd/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions src/args/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ pub fn show_post(args: &Args) -> Option<usize> {
Some(Cmd::Post(cmd::post::Opt {
show: true,
show_meta: false,
show_comment: false,
list: false,
delete: false,
search: None,
Expand Down Expand Up @@ -207,6 +208,7 @@ pub fn show_post_meta(args: &Args) -> Option<usize> {
Some(Cmd::Post(cmd::post::Opt {
show: false,
show_meta: true,
show_comment: false,
list: false,
delete: false,
search: None,
Expand All @@ -227,13 +229,42 @@ pub fn show_post_meta(args: &Args) -> Option<usize> {
.into_some()
}

pub fn show_post_comment(args: &Args) -> Option<usize> {
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 {
cmd:
Some(Cmd::Post(cmd::post::Opt {
show: false,
show_meta: false,
show_comment: false,
list: true,
delete: false,
search: None,
Expand Down Expand Up @@ -265,6 +296,7 @@ pub fn delete_post(args: &Args) -> Option<usize> {
Some(Cmd::Post(cmd::post::Opt {
show: false,
show_meta: false,
show_comment: false,
list: false,
delete: true,
search: None,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
24 changes: 24 additions & 0 deletions src/display/colorful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -201,6 +202,29 @@ pub fn show_post_meta(entry: &Result<PostEntry>) {
println!("Link https:{}", entry.url);
}

pub fn show_post_comment(comment_list: &Result<Vec<PostCommentEntry>>, 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<PostEntry>, usize)>, rev: bool) {
let (entry_list, total_count) = match result {
Ok(o) => o,
Expand Down
12 changes: 12 additions & 0 deletions src/display/json.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -59,6 +60,17 @@ pub fn show_post_meta(entry: &Result<PostEntry>) {
println_result(entry);
}

pub fn show_post_comment(comment_list: &Result<Vec<PostCommentEntry>>, 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::<Vec<_>>();
let json = json::serialize(comment_vec).expect("Can not serialize comment_vec");
print!("{}", json);
}

pub fn list_post(result: &Result<(Vec<PostEntry>, usize)>, rev: bool) {
let (entry_list, total_count) = match result {
Ok(o) => o,
Expand Down
9 changes: 9 additions & 0 deletions src/display/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -80,6 +81,14 @@ pub fn show_post_meta(style: &Style, entry: &Result<PostEntry>) {
}
}

pub fn show_post_comment(style: &Style, comment_list: &Result<Vec<PostCommentEntry>>, 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<PostEntry>, usize)>, rev: bool) {
match style {
Style::Colorful => colorful::list_post(result, rev),
Expand Down
19 changes: 19 additions & 0 deletions src/display/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -201,6 +202,24 @@ pub fn show_post_meta(entry: &Result<PostEntry>) {
println!("Link https:{}", entry.url);
}

pub fn show_post_comment(comment_list: &Result<Vec<PostCommentEntry>>, 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<PostEntry>, usize)>, rev: bool) {
let (entry_list, total_count) = match result {
Ok(o) => o,
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down

0 comments on commit bdae7c3

Please sign in to comment.