Skip to content

Commit 4a7a254

Browse files
committed
feat!: time style option
1 parent 69a6fea commit 4a7a254

File tree

7 files changed

+111
-104
lines changed

7 files changed

+111
-104
lines changed

src/args/mod.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ pub enum Style {
1111
Json,
1212
}
1313

14+
#[derive(Clone, Debug, Parser, ValueEnum)]
15+
pub enum TimeStyle {
16+
Friendly,
17+
Normal,
18+
}
19+
1420
#[derive(Debug, Parser)]
1521
#[command(author, about, long_about = None, version)]
1622
pub struct Args {
@@ -81,6 +87,18 @@ pub struct Args {
8187
#[arg(value_name = "NAME")]
8288
pub style: Style,
8389

90+
#[arg(verbatim_doc_comment)]
91+
/// Configure the time style
92+
/// Example: cnb --style normal ing list
93+
/// This option does not affect the output of '--style json'
94+
/// *
95+
#[arg(long)]
96+
#[arg(value_enum)]
97+
#[arg(hide_possible_values = true)]
98+
#[arg(default_value_t = TimeStyle::Friendly)]
99+
#[arg(value_name = "NAME")]
100+
pub time_style: TimeStyle,
101+
84102
#[arg(verbatim_doc_comment)]
85103
/// Fail if error occurred
86104
/// Example: cnb --fail-on-error ing list

src/args/parser.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub const fn no_operation(args: &Args) -> bool {
2222
take: None,
2323
debug: _,
2424
style: _,
25+
time_style: _,
2526
fail_on_error: _,
2627
quiet: _,
2728
}
@@ -44,6 +45,7 @@ pub const fn user_info(args: &Args) -> bool {
4445
take: None,
4546
debug: _,
4647
style: _,
48+
time_style: _,
4749
fail_on_error: _,
4850
quiet: _,
4951
}
@@ -66,6 +68,7 @@ pub fn publish_ing(args: &Args) -> Option<&String> {
6668
take: None,
6769
debug: _,
6870
style: _,
71+
time_style: _,
6972
fail_on_error: _,
7073
quiet: _,
7174
} => content,
@@ -90,6 +93,7 @@ pub fn login(args: &Args) -> Option<&String> {
9093
take: None,
9194
debug: _,
9295
style: _,
96+
time_style: _,
9397
fail_on_error: _,
9498
quiet: _,
9599
} => pat,
@@ -114,6 +118,7 @@ pub const fn logout(args: &Args) -> bool {
114118
take: None,
115119
debug: _,
116120
style: _,
121+
time_style: _,
117122
fail_on_error: _,
118123
quiet: _,
119124
}
@@ -136,6 +141,7 @@ pub fn list_ing(args: &Args) -> Option<(usize, usize, IngType, bool)> {
136141
take,
137142
debug: _,
138143
style: _,
144+
time_style: _,
139145
fail_on_error: _,
140146
quiet: _,
141147
} => {
@@ -165,6 +171,7 @@ pub fn comment_ing(args: &Args) -> Option<(&String, usize)> {
165171
take: None,
166172
debug: _,
167173
style: _,
174+
time_style: _,
168175
fail_on_error: _,
169176
quiet: _,
170177
} => (content, *id),
@@ -193,6 +200,7 @@ pub fn show_post(args: &Args) -> Option<usize> {
193200
take: None,
194201
debug: _,
195202
style: _,
203+
time_style: _,
196204
fail_on_error: _,
197205
quiet: _,
198206
} => *id,
@@ -221,6 +229,7 @@ pub fn show_post_meta(args: &Args) -> Option<usize> {
221229
take: None,
222230
debug: _,
223231
style: _,
232+
time_style: _,
224233
fail_on_error: _,
225234
quiet: _,
226235
} => *id,
@@ -249,6 +258,7 @@ pub fn show_post_comment(args: &Args) -> Option<usize> {
249258
take: None,
250259
debug: _,
251260
style: _,
261+
time_style: _,
252262
fail_on_error: _,
253263
quiet: _,
254264
} => *id,
@@ -277,6 +287,7 @@ pub fn list_post(args: &Args) -> Option<(usize, usize)> {
277287
take,
278288
debug: _,
279289
style: _,
290+
time_style: _,
280291
fail_on_error: _,
281292
quiet: _,
282293
} => {
@@ -309,6 +320,7 @@ pub fn delete_post(args: &Args) -> Option<usize> {
309320
take: None,
310321
debug: _,
311322
style: _,
323+
time_style: _,
312324
fail_on_error: _,
313325
quiet: _,
314326
} => *id,
@@ -337,6 +349,7 @@ pub fn search_post(args: &Args) -> Option<(&String, usize, usize)> {
337349
take,
338350
debug: _,
339351
style: _,
352+
time_style: _,
340353
fail_on_error: _,
341354
quiet: _,
342355
} => {
@@ -374,6 +387,7 @@ pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> {
374387
take: None,
375388
debug: _,
376389
style: _,
390+
time_style: _,
377391
fail_on_error: _,
378392
quiet: _,
379393
} => (title, body, *publish),
@@ -411,6 +425,7 @@ pub fn update_post(
411425
take: None,
412426
debug: _,
413427
style: _,
428+
time_style: _,
414429
fail_on_error: _,
415430
quiet: _,
416431
} => (*id, title, body, publish),
@@ -430,6 +445,7 @@ pub fn list_news(args: &Args) -> Option<(usize, usize)> {
430445
take,
431446
debug: _,
432447
style: _,
448+
time_style: _,
433449
fail_on_error: _,
434450
quiet: _,
435451
} => {

src/display/colorful.rs

Lines changed: 16 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use crate::api::news::get_list::NewsEntry;
77
use crate::api::post::get_comment_list::PostCommentEntry;
88
use crate::api::post::get_one::PostEntry;
99
use crate::api::user::info::UserInfo;
10+
use crate::args::TimeStyle;
1011
use crate::infra::iter::IteratorExt;
1112
use crate::infra::str::StrExt;
12-
use crate::infra::time::{fmt_time_to_string_friendly, patch_rfc3339};
13+
use crate::infra::time::display_cnb_time;
1314
use anyhow::Result;
14-
use chrono::{DateTime, Local, Utc};
1515
use colored::Colorize;
1616
use std::fmt::Display;
1717
use std::ops::Not;
@@ -54,6 +54,7 @@ pub fn user_info(info: &Result<UserInfo>) {
5454
}
5555

5656
pub fn list_ing(
57+
time_style: &TimeStyle,
5758
ing_with_comment_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
5859
rev: bool,
5960
align: bool,
@@ -67,15 +68,9 @@ pub fn list_ing(
6768
.iter()
6869
.dyn_rev(rev)
6970
.for_each(|(ing, comment_list)| {
70-
let create_time = {
71-
let rfc3339 = patch_rfc3339(&ing.create_time);
72-
let dt = DateTime::parse_from_rfc3339(&rfc3339)
73-
.unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339))
74-
.with_timezone(&Utc);
75-
fmt_time_to_string_friendly(dt.into(), Local::now())
76-
};
77-
71+
let create_time = display_cnb_time(&ing.create_time, time_style);
7872
print!("{}", create_time.dimmed());
73+
7974
let send_from_mark = match ing.send_from {
8075
IngSendFrom::Cli => Some("CLI"),
8176
IngSendFrom::CellPhone => Some("Mobile"),
@@ -154,7 +149,7 @@ pub fn show_post(entry: &Result<PostEntry>) {
154149
}
155150
}
156151

157-
pub fn show_post_meta(entry: &Result<PostEntry>) {
152+
pub fn show_post_meta(time_style: &TimeStyle, entry: &Result<PostEntry>) {
158153
let entry = match entry {
159154
Ok(entry) => entry,
160155
Err(e) => return println_err(e),
@@ -186,39 +181,25 @@ pub fn show_post_meta(entry: &Result<PostEntry>) {
186181
println!("Tags {}", tags_text);
187182
}
188183
}
189-
let create_time = {
190-
let rfc3339 = patch_rfc3339(&entry.create_time);
191-
let dt = DateTime::parse_from_rfc3339(&rfc3339)
192-
.unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339))
193-
.with_timezone(&Utc);
194-
fmt_time_to_string_friendly(dt.into(), Local::now())
195-
};
184+
let create_time = display_cnb_time(&entry.create_time, time_style);
196185
println!("Create {}", create_time);
197-
let modify_time = {
198-
let rfc3339 = patch_rfc3339(&entry.modify_time);
199-
let dt = DateTime::parse_from_rfc3339(&rfc3339)
200-
.unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339))
201-
.with_timezone(&Utc);
202-
fmt_time_to_string_friendly(dt.into(), Local::now())
203-
};
186+
let modify_time = display_cnb_time(&entry.create_time, time_style);
204187
println!("Modify {}", modify_time);
205188
println!("Link https:{}", entry.url);
206189
}
207190

208-
pub fn show_post_comment(comment_list: &Result<Vec<PostCommentEntry>>, rev: bool) {
191+
pub fn show_post_comment(
192+
time_style: &TimeStyle,
193+
comment_list: &Result<Vec<PostCommentEntry>>,
194+
rev: bool,
195+
) {
209196
let comment_list = match comment_list {
210197
Ok(entry) => entry,
211198
Err(e) => return println_err(e),
212199
};
213200

214201
comment_list.iter().dyn_rev(rev).for_each(|comment| {
215-
let create_time = {
216-
let rfc3339 = patch_rfc3339(&comment.create_time);
217-
let dt = DateTime::parse_from_rfc3339(&rfc3339)
218-
.unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339))
219-
.with_timezone(&Utc);
220-
fmt_time_to_string_friendly(dt.into(), Local::now())
221-
};
202+
let create_time = display_cnb_time(&comment.create_time, time_style);
222203
let floor_text = format!("{}F", comment.floor);
223204
println!("{} {}", create_time.dimmed(), floor_text.dimmed());
224205
println!(" {} {}", comment.user_name.cyan(), comment.content);
@@ -271,21 +252,14 @@ pub fn println_result<T: Display>(result: &Result<T>) {
271252
}
272253
}
273254

274-
pub fn list_news(news_list: &Result<Vec<NewsEntry>>, rev: bool) {
255+
pub fn list_news(time_style: &TimeStyle, news_list: &Result<Vec<NewsEntry>>, rev: bool) {
275256
let news_list = match news_list {
276257
Ok(o) => o,
277258
Err(e) => return println_err(e),
278259
};
279260

280261
news_list.iter().dyn_rev(rev).for_each(|news| {
281-
let create_time = {
282-
let rfc3339 = patch_rfc3339(&news.create_time);
283-
let dt = DateTime::parse_from_rfc3339(&rfc3339)
284-
.unwrap_or_else(|_| panic!("Invalid RFC3339: {}", rfc3339))
285-
.with_timezone(&Utc);
286-
fmt_time_to_string_friendly(dt.into(), Local::now())
287-
};
288-
262+
let create_time = display_cnb_time(&news.create_time, time_style);
289263
let url = format!("https://news.cnblogs.com/n/{}", news.id);
290264
println!("{} {}", create_time.dimmed(), url.dimmed(),);
291265
println!(" {}", news.title);

src/display/mod.rs

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::api::news::get_list::NewsEntry;
44
use crate::api::post::get_comment_list::PostCommentEntry;
55
use crate::api::post::get_one::PostEntry;
66
use crate::api::user::info::UserInfo;
7-
use crate::args::Style;
7+
use crate::args::{Style, TimeStyle};
88
use anyhow::Result;
99
use std::path::PathBuf;
1010

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

3939
pub fn list_ing(
4040
style: &Style,
41+
time_style: &TimeStyle,
4142
ing_with_comment_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>,
4243
rev: bool,
4344
align: bool,
4445
) {
4546
match style {
46-
Style::Colorful => colorful::list_ing(ing_with_comment_list, rev, align),
47-
Style::Normal => normal::list_ing(ing_with_comment_list, rev, align),
47+
Style::Colorful => colorful::list_ing(time_style, ing_with_comment_list, rev, align),
48+
Style::Normal => normal::list_ing(time_style, ing_with_comment_list, rev, align),
4849
Style::Json => json::list_ing(ing_with_comment_list, rev),
4950
}
5051
}
@@ -73,18 +74,23 @@ pub fn show_post(style: &Style, entry: &Result<PostEntry>) {
7374
}
7475
}
7576

76-
pub fn show_post_meta(style: &Style, entry: &Result<PostEntry>) {
77+
pub fn show_post_meta(style: &Style, time_style: &TimeStyle, entry: &Result<PostEntry>) {
7778
match style {
78-
Style::Colorful => colorful::show_post_meta(entry),
79-
Style::Normal => normal::show_post_meta(entry),
79+
Style::Colorful => colorful::show_post_meta(time_style, entry),
80+
Style::Normal => normal::show_post_meta(time_style, entry),
8081
Style::Json => json::show_post_meta(entry),
8182
}
8283
}
8384

84-
pub fn show_post_comment(style: &Style, comment_list: &Result<Vec<PostCommentEntry>>, rev: bool) {
85+
pub fn show_post_comment(
86+
style: &Style,
87+
time_style: &TimeStyle,
88+
comment_list: &Result<Vec<PostCommentEntry>>,
89+
rev: bool,
90+
) {
8591
match style {
86-
Style::Colorful => colorful::show_post_comment(comment_list, rev),
87-
Style::Normal => normal::show_post_comment(comment_list, rev),
92+
Style::Colorful => colorful::show_post_comment(time_style, comment_list, rev),
93+
Style::Normal => normal::show_post_comment(time_style, comment_list, rev),
8894
Style::Json => json::show_post_comment(comment_list, rev),
8995
}
9096
}
@@ -129,10 +135,15 @@ pub fn update_post(style: &Style, result: &Result<usize>) {
129135
}
130136
}
131137

132-
pub fn list_news(style: &Style, news_list: &Result<Vec<NewsEntry>>, rev: bool) {
138+
pub fn list_news(
139+
style: &Style,
140+
time_style: &TimeStyle,
141+
news_list: &Result<Vec<NewsEntry>>,
142+
rev: bool,
143+
) {
133144
match style {
134-
Style::Colorful => colorful::list_news(news_list, rev),
135-
Style::Normal => normal::list_news(news_list, rev),
145+
Style::Colorful => colorful::list_news(time_style, news_list, rev),
146+
Style::Normal => normal::list_news(time_style, news_list, rev),
136147
Style::Json => json::list_news(news_list, rev),
137148
}
138149
}

0 commit comments

Comments
 (0)