Skip to content

Commit

Permalink
feat!: time style option
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Sep 21, 2023
1 parent 69a6fea commit 4a7a254
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 104 deletions.
18 changes: 18 additions & 0 deletions src/args/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ pub enum Style {
Json,
}

#[derive(Clone, Debug, Parser, ValueEnum)]
pub enum TimeStyle {
Friendly,
Normal,
}

#[derive(Debug, Parser)]
#[command(author, about, long_about = None, version)]
pub struct Args {
Expand Down Expand Up @@ -81,6 +87,18 @@ pub struct Args {
#[arg(value_name = "NAME")]
pub style: Style,

#[arg(verbatim_doc_comment)]
/// Configure the time style
/// Example: cnb --style normal ing list
/// This option does not affect the output of '--style json'
/// *
#[arg(long)]
#[arg(value_enum)]
#[arg(hide_possible_values = true)]
#[arg(default_value_t = TimeStyle::Friendly)]
#[arg(value_name = "NAME")]
pub time_style: TimeStyle,

#[arg(verbatim_doc_comment)]
/// Fail if error occurred
/// Example: cnb --fail-on-error ing list
Expand Down
16 changes: 16 additions & 0 deletions src/args/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub const fn no_operation(args: &Args) -> bool {
take: None,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
}
Expand All @@ -44,6 +45,7 @@ pub const fn user_info(args: &Args) -> bool {
take: None,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
}
Expand All @@ -66,6 +68,7 @@ pub fn publish_ing(args: &Args) -> Option<&String> {
take: None,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
} => content,
Expand All @@ -90,6 +93,7 @@ pub fn login(args: &Args) -> Option<&String> {
take: None,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
} => pat,
Expand All @@ -114,6 +118,7 @@ pub const fn logout(args: &Args) -> bool {
take: None,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
}
Expand All @@ -136,6 +141,7 @@ pub fn list_ing(args: &Args) -> Option<(usize, usize, IngType, bool)> {
take,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
} => {
Expand Down Expand Up @@ -165,6 +171,7 @@ pub fn comment_ing(args: &Args) -> Option<(&String, usize)> {
take: None,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
} => (content, *id),
Expand Down Expand Up @@ -193,6 +200,7 @@ pub fn show_post(args: &Args) -> Option<usize> {
take: None,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
} => *id,
Expand Down Expand Up @@ -221,6 +229,7 @@ pub fn show_post_meta(args: &Args) -> Option<usize> {
take: None,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
} => *id,
Expand Down Expand Up @@ -249,6 +258,7 @@ pub fn show_post_comment(args: &Args) -> Option<usize> {
take: None,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
} => *id,
Expand Down Expand Up @@ -277,6 +287,7 @@ pub fn list_post(args: &Args) -> Option<(usize, usize)> {
take,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
} => {
Expand Down Expand Up @@ -309,6 +320,7 @@ pub fn delete_post(args: &Args) -> Option<usize> {
take: None,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
} => *id,
Expand Down Expand Up @@ -337,6 +349,7 @@ pub fn search_post(args: &Args) -> Option<(&String, usize, usize)> {
take,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
} => {
Expand Down Expand Up @@ -374,6 +387,7 @@ pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> {
take: None,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
} => (title, body, *publish),
Expand Down Expand Up @@ -411,6 +425,7 @@ pub fn update_post(
take: None,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
} => (*id, title, body, publish),
Expand All @@ -430,6 +445,7 @@ pub fn list_news(args: &Args) -> Option<(usize, usize)> {
take,
debug: _,
style: _,
time_style: _,
fail_on_error: _,
quiet: _,
} => {
Expand Down
58 changes: 16 additions & 42 deletions src/display/colorful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ 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::TimeStyle;
use crate::infra::iter::IteratorExt;
use crate::infra::str::StrExt;
use crate::infra::time::{fmt_time_to_string_friendly, patch_rfc3339};
use crate::infra::time::display_cnb_time;
use anyhow::Result;
use chrono::{DateTime, Local, Utc};
use colored::Colorize;
use std::fmt::Display;
use std::ops::Not;
Expand Down Expand Up @@ -54,6 +54,7 @@ pub fn user_info(info: &Result<UserInfo>) {
}

pub fn list_ing(
time_style: &TimeStyle,
ing_with_comment_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
rev: bool,
align: bool,
Expand All @@ -67,15 +68,9 @@ pub fn list_ing(
.iter()
.dyn_rev(rev)
.for_each(|(ing, comment_list)| {
let create_time = {
let rfc3339 = patch_rfc3339(&ing.create_time);
let dt = DateTime::parse_from_rfc3339(&rfc3339)
.unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339))
.with_timezone(&Utc);
fmt_time_to_string_friendly(dt.into(), Local::now())
};

let create_time = display_cnb_time(&ing.create_time, time_style);
print!("{}", create_time.dimmed());

let send_from_mark = match ing.send_from {
IngSendFrom::Cli => Some("CLI"),
IngSendFrom::CellPhone => Some("Mobile"),
Expand Down Expand Up @@ -154,7 +149,7 @@ pub fn show_post(entry: &Result<PostEntry>) {
}
}

pub fn show_post_meta(entry: &Result<PostEntry>) {
pub fn show_post_meta(time_style: &TimeStyle, entry: &Result<PostEntry>) {
let entry = match entry {
Ok(entry) => entry,
Err(e) => return println_err(e),
Expand Down Expand Up @@ -186,39 +181,25 @@ pub fn show_post_meta(entry: &Result<PostEntry>) {
println!("Tags {}", tags_text);
}
}
let create_time = {
let rfc3339 = patch_rfc3339(&entry.create_time);
let dt = DateTime::parse_from_rfc3339(&rfc3339)
.unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339))
.with_timezone(&Utc);
fmt_time_to_string_friendly(dt.into(), Local::now())
};
let create_time = display_cnb_time(&entry.create_time, time_style);
println!("Create {}", create_time);
let modify_time = {
let rfc3339 = patch_rfc3339(&entry.modify_time);
let dt = DateTime::parse_from_rfc3339(&rfc3339)
.unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339))
.with_timezone(&Utc);
fmt_time_to_string_friendly(dt.into(), Local::now())
};
let modify_time = display_cnb_time(&entry.create_time, time_style);
println!("Modify {}", modify_time);
println!("Link https:{}", entry.url);
}

pub fn show_post_comment(comment_list: &Result<Vec<PostCommentEntry>>, rev: bool) {
pub fn show_post_comment(
time_style: &TimeStyle,
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))
.with_timezone(&Utc);
fmt_time_to_string_friendly(dt.into(), Local::now())
};
let create_time = display_cnb_time(&comment.create_time, time_style);
let floor_text = format!("{}F", comment.floor);
println!("{} {}", create_time.dimmed(), floor_text.dimmed());
println!(" {} {}", comment.user_name.cyan(), comment.content);
Expand Down Expand Up @@ -271,21 +252,14 @@ pub fn println_result<T: Display>(result: &Result<T>) {
}
}

pub fn list_news(news_list: &Result<Vec<NewsEntry>>, rev: bool) {
pub fn list_news(time_style: &TimeStyle, news_list: &Result<Vec<NewsEntry>>, rev: bool) {
let news_list = match news_list {
Ok(o) => o,
Err(e) => return println_err(e),
};

news_list.iter().dyn_rev(rev).for_each(|news| {
let create_time = {
let rfc3339 = patch_rfc3339(&news.create_time);
let dt = DateTime::parse_from_rfc3339(&rfc3339)
.unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339))
.with_timezone(&Utc);
fmt_time_to_string_friendly(dt.into(), Local::now())
};

let create_time = display_cnb_time(&news.create_time, time_style);
let url = format!("https://news.cnblogs.com/n/{}", news.id);
println!("{} {}", create_time.dimmed(), url.dimmed(),);
println!(" {}", news.title);
Expand Down
35 changes: 23 additions & 12 deletions src/display/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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;
use crate::args::{Style, TimeStyle};
use anyhow::Result;
use std::path::PathBuf;

Expand Down Expand Up @@ -38,13 +38,14 @@ pub fn user_info(style: &Style, user_info: &Result<UserInfo>) {

pub fn list_ing(
style: &Style,
time_style: &TimeStyle,
ing_with_comment_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
rev: bool,
align: bool,
) {
match style {
Style::Colorful => colorful::list_ing(ing_with_comment_list, rev, align),
Style::Normal => normal::list_ing(ing_with_comment_list, rev, align),
Style::Colorful => colorful::list_ing(time_style, ing_with_comment_list, rev, align),
Style::Normal => normal::list_ing(time_style, ing_with_comment_list, rev, align),
Style::Json => json::list_ing(ing_with_comment_list, rev),
}
}
Expand Down Expand Up @@ -73,18 +74,23 @@ pub fn show_post(style: &Style, entry: &Result<PostEntry>) {
}
}

pub fn show_post_meta(style: &Style, entry: &Result<PostEntry>) {
pub fn show_post_meta(style: &Style, time_style: &TimeStyle, entry: &Result<PostEntry>) {
match style {
Style::Colorful => colorful::show_post_meta(entry),
Style::Normal => normal::show_post_meta(entry),
Style::Colorful => colorful::show_post_meta(time_style, entry),
Style::Normal => normal::show_post_meta(time_style, entry),
Style::Json => json::show_post_meta(entry),
}
}

pub fn show_post_comment(style: &Style, comment_list: &Result<Vec<PostCommentEntry>>, rev: bool) {
pub fn show_post_comment(
style: &Style,
time_style: &TimeStyle,
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::Colorful => colorful::show_post_comment(time_style, comment_list, rev),
Style::Normal => normal::show_post_comment(time_style, comment_list, rev),
Style::Json => json::show_post_comment(comment_list, rev),
}
}
Expand Down Expand Up @@ -129,10 +135,15 @@ pub fn update_post(style: &Style, result: &Result<usize>) {
}
}

pub fn list_news(style: &Style, news_list: &Result<Vec<NewsEntry>>, rev: bool) {
pub fn list_news(
style: &Style,
time_style: &TimeStyle,
news_list: &Result<Vec<NewsEntry>>,
rev: bool,
) {
match style {
Style::Colorful => colorful::list_news(news_list, rev),
Style::Normal => normal::list_news(news_list, rev),
Style::Colorful => colorful::list_news(time_style, news_list, rev),
Style::Normal => normal::list_news(time_style, news_list, rev),
Style::Json => json::list_news(news_list, rev),
}
}
Loading

0 comments on commit 4a7a254

Please sign in to comment.