Skip to content

Commit 76c7548

Browse files
committed
style: apply fmt and clippy
1 parent 1a642fc commit 76c7548

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

src/display/colorful.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::api::news::get_list::NewsEntry;
66
use crate::api::post::get_one::PostEntry;
77
use crate::api::user::info::UserInfo;
88
use crate::infra::iter::IteratorExt;
9+
use crate::infra::str::StrExt;
910
use crate::infra::time::patch_rfc3339;
1011
use anyhow::Result;
1112
use chrono::DateTime;
@@ -14,7 +15,6 @@ use std::fmt::Display;
1415
use std::ops::Not;
1516
use std::path::PathBuf;
1617
use unicode_width::UnicodeWidthStr;
17-
use crate::infra::str::StrExt;
1818

1919
pub fn login(cfg_path: &Result<PathBuf>) {
2020
match cfg_path {
@@ -85,18 +85,26 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: b
8585
println!(" {} {}", "#".dimmed(), ing.id.to_string().dimmed());
8686
let content = if align {
8787
let user_name_width = ing.user_name.width_cjk();
88-
let (term_width, _) = term_size::dimensions()
89-
.expect("Can not get terminal size");
90-
let left_width = term_width.checked_sub(user_name_width + 3).unwrap_or(0);
91-
if let Some(lines) = fmt_content(&ing.content).width_split(left_width) {
92-
if comment_list.len() > 0 {
93-
lines.join("\n").replace("\n", &format!("\n │{}", " ".repeat(user_name_width - 2)))
94-
} else {
95-
lines.join("\n").replace("\n", &format!("\n{}", " ".repeat(user_name_width + 3)))
96-
}
97-
} else {
98-
ing.content.clone()
99-
}
88+
let (term_width, _) = term_size::dimensions().expect("Can not get terminal size");
89+
let left_width = term_width.saturating_sub(user_name_width + 3);
90+
fmt_content(&ing.content)
91+
.width_split(left_width)
92+
.map_or_else(
93+
|| ing.content.clone(),
94+
|lines| {
95+
if comment_list.is_empty().not() {
96+
lines.join("\n").replace(
97+
'\n',
98+
&format!("\n │{}", " ".repeat(user_name_width - 2)),
99+
)
100+
} else {
101+
lines.join("\n").replace(
102+
'\n',
103+
&format!("\n{}", " ".repeat(user_name_width + 3)),
104+
)
105+
}
106+
},
107+
)
100108
} else {
101109
fmt_content(&ing.content)
102110
};
@@ -248,7 +256,7 @@ pub fn list_news(news_list: &Result<Vec<NewsEntry>>, rev: bool) {
248256
};
249257

250258
let url = format!("https://news.cnblogs.com/n/{}", news.id);
251-
println!("{} {}", create_time.dimmed(), url.dimmed(), );
259+
println!("{} {}", create_time.dimmed(), url.dimmed(),);
252260
println!(" {}", news.title);
253261
println!(" {}{}", news.summary.dimmed(), "...".dimmed());
254262
println!();

src/display/normal.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::api::news::get_list::NewsEntry;
66
use crate::api::post::get_one::PostEntry;
77
use crate::api::user::info::UserInfo;
88
use crate::infra::iter::IteratorExt;
9+
use crate::infra::str::StrExt;
910
use crate::infra::time::patch_rfc3339;
1011
use anyhow::Result;
1112
use chrono::DateTime;
@@ -14,7 +15,6 @@ use std::fmt::Display;
1415
use std::ops::Not;
1516
use std::path::PathBuf;
1617
use unicode_width::UnicodeWidthStr;
17-
use crate::infra::str::StrExt;
1818

1919
pub fn login(cfg_path: &Result<PathBuf>) {
2020
match cfg_path {
@@ -85,18 +85,26 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, rev: b
8585
println!(" # {}", ing.id);
8686
let content = if align {
8787
let user_name_width = ing.user_name.width_cjk();
88-
let (term_width, _) = term_size::dimensions()
89-
.expect("Can not get terminal size");
90-
let left_width = term_width.checked_sub(user_name_width + 3).unwrap_or(0);
91-
if let Some(lines) = fmt_content(&ing.content).width_split(left_width) {
92-
if comment_list.len() > 0 {
93-
lines.join("\n").replace("\n", &format!("\n │{}", " ".repeat(user_name_width - 2)))
94-
} else {
95-
lines.join("\n").replace("\n", &format!("\n{}", " ".repeat(user_name_width + 3)))
96-
}
97-
} else {
98-
ing.content.clone()
99-
}
88+
let (term_width, _) = term_size::dimensions().expect("Can not get terminal size");
89+
let left_width = term_width.saturating_sub(user_name_width + 3);
90+
fmt_content(&ing.content)
91+
.width_split(left_width)
92+
.map_or_else(
93+
|| ing.content.clone(),
94+
|lines| {
95+
if comment_list.is_empty().not() {
96+
lines.join("\n").replace(
97+
'\n',
98+
&format!("\n │{}", " ".repeat(user_name_width - 2)),
99+
)
100+
} else {
101+
lines.join("\n").replace(
102+
'\n',
103+
&format!("\n{}", " ".repeat(user_name_width + 3)),
104+
)
105+
}
106+
},
107+
)
100108
} else {
101109
fmt_content(&ing.content)
102110
};

src/infra/str.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ impl StrExt for str {
3434
loop {
3535
let (head, tail) = str.width_split_head(width);
3636
// No split strategy exist, return None
37-
if head == "" {
37+
if head.is_empty() {
3838
return None;
3939
}
4040
vec.push(head);
41-
if tail == "" {
41+
if tail.is_empty() {
4242
break;
4343
}
4444
str = tail;

0 commit comments

Comments
 (0)