Skip to content

Commit

Permalink
style: apply fmt and clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Sep 19, 2023
1 parent 1a642fc commit 76c7548
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 29 deletions.
36 changes: 22 additions & 14 deletions src/display/colorful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<PathBuf>) {
match cfg_path {
Expand Down Expand Up @@ -85,18 +85,26 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, 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)
};
Expand Down Expand Up @@ -248,7 +256,7 @@ pub fn list_news(news_list: &Result<Vec<NewsEntry>>, 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!();
Expand Down
34 changes: 21 additions & 13 deletions src/display/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<PathBuf>) {
match cfg_path {
Expand Down Expand Up @@ -85,18 +85,26 @@ pub fn list_ing(ing_list: &Result<Vec<(IngEntry, Vec<IngCommentEntry>)>>, 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)
};
Expand Down
4 changes: 2 additions & 2 deletions src/infra/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 76c7548

Please sign in to comment.