From 76c7548e2ee4d867811da15f884d03cdd28a9bd8 Mon Sep 17 00:00:00 2001 From: Thaumy Date: Tue, 19 Sep 2023 14:36:44 +0800 Subject: [PATCH] style: apply fmt and clippy --- src/display/colorful.rs | 36 ++++++++++++++++++++++-------------- src/display/normal.rs | 34 +++++++++++++++++++++------------- src/infra/str.rs | 4 ++-- 3 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/display/colorful.rs b/src/display/colorful.rs index 73de7ec..9f34fdb 100644 --- a/src/display/colorful.rs +++ b/src/display/colorful.rs @@ -6,6 +6,7 @@ use crate::api::news::get_list::NewsEntry; use crate::api::post::get_one::PostEntry; use crate::api::user::info::UserInfo; use crate::infra::iter::IteratorExt; +use crate::infra::str::StrExt; use crate::infra::time::patch_rfc3339; use anyhow::Result; use chrono::DateTime; @@ -14,7 +15,6 @@ use std::fmt::Display; use std::ops::Not; use std::path::PathBuf; use unicode_width::UnicodeWidthStr; -use crate::infra::str::StrExt; pub fn login(cfg_path: &Result) { match cfg_path { @@ -85,18 +85,26 @@ pub fn list_ing(ing_list: &Result)>>, rev: b println!(" {} {}", "#".dimmed(), ing.id.to_string().dimmed()); let content = if align { let user_name_width = ing.user_name.width_cjk(); - let (term_width, _) = term_size::dimensions() - .expect("Can not get terminal size"); - let left_width = term_width.checked_sub(user_name_width + 3).unwrap_or(0); - if let Some(lines) = fmt_content(&ing.content).width_split(left_width) { - if comment_list.len() > 0 { - lines.join("\n").replace("\n", &format!("\n │{}", " ".repeat(user_name_width - 2))) - } else { - lines.join("\n").replace("\n", &format!("\n{}", " ".repeat(user_name_width + 3))) - } - } else { - ing.content.clone() - } + let (term_width, _) = term_size::dimensions().expect("Can not get terminal size"); + let left_width = term_width.saturating_sub(user_name_width + 3); + fmt_content(&ing.content) + .width_split(left_width) + .map_or_else( + || ing.content.clone(), + |lines| { + if comment_list.is_empty().not() { + lines.join("\n").replace( + '\n', + &format!("\n │{}", " ".repeat(user_name_width - 2)), + ) + } else { + lines.join("\n").replace( + '\n', + &format!("\n{}", " ".repeat(user_name_width + 3)), + ) + } + }, + ) } else { fmt_content(&ing.content) }; @@ -248,7 +256,7 @@ pub fn list_news(news_list: &Result>, rev: bool) { }; let url = format!("https://news.cnblogs.com/n/{}", news.id); - println!("{} {}", create_time.dimmed(), url.dimmed(), ); + println!("{} {}", create_time.dimmed(), url.dimmed(),); println!(" {}", news.title); println!(" {}{}", news.summary.dimmed(), "...".dimmed()); println!(); diff --git a/src/display/normal.rs b/src/display/normal.rs index 8e014f8..9eb99e0 100644 --- a/src/display/normal.rs +++ b/src/display/normal.rs @@ -6,6 +6,7 @@ use crate::api::news::get_list::NewsEntry; use crate::api::post::get_one::PostEntry; use crate::api::user::info::UserInfo; use crate::infra::iter::IteratorExt; +use crate::infra::str::StrExt; use crate::infra::time::patch_rfc3339; use anyhow::Result; use chrono::DateTime; @@ -14,7 +15,6 @@ use std::fmt::Display; use std::ops::Not; use std::path::PathBuf; use unicode_width::UnicodeWidthStr; -use crate::infra::str::StrExt; pub fn login(cfg_path: &Result) { match cfg_path { @@ -85,18 +85,26 @@ pub fn list_ing(ing_list: &Result)>>, rev: b println!(" # {}", ing.id); let content = if align { let user_name_width = ing.user_name.width_cjk(); - let (term_width, _) = term_size::dimensions() - .expect("Can not get terminal size"); - let left_width = term_width.checked_sub(user_name_width + 3).unwrap_or(0); - if let Some(lines) = fmt_content(&ing.content).width_split(left_width) { - if comment_list.len() > 0 { - lines.join("\n").replace("\n", &format!("\n │{}", " ".repeat(user_name_width - 2))) - } else { - lines.join("\n").replace("\n", &format!("\n{}", " ".repeat(user_name_width + 3))) - } - } else { - ing.content.clone() - } + let (term_width, _) = term_size::dimensions().expect("Can not get terminal size"); + let left_width = term_width.saturating_sub(user_name_width + 3); + fmt_content(&ing.content) + .width_split(left_width) + .map_or_else( + || ing.content.clone(), + |lines| { + if comment_list.is_empty().not() { + lines.join("\n").replace( + '\n', + &format!("\n │{}", " ".repeat(user_name_width - 2)), + ) + } else { + lines.join("\n").replace( + '\n', + &format!("\n{}", " ".repeat(user_name_width + 3)), + ) + } + }, + ) } else { fmt_content(&ing.content) }; diff --git a/src/infra/str.rs b/src/infra/str.rs index 7416938..9390e8a 100644 --- a/src/infra/str.rs +++ b/src/infra/str.rs @@ -34,11 +34,11 @@ impl StrExt for str { loop { let (head, tail) = str.width_split_head(width); // No split strategy exist, return None - if head == "" { + if head.is_empty() { return None; } vec.push(head); - if tail == "" { + if tail.is_empty() { break; } str = tail;