Skip to content

Commit 5be3e5a

Browse files
committed
refactor: simplify impl
1 parent f66f939 commit 5be3e5a

File tree

5 files changed

+12
-11
lines changed

5 files changed

+12
-11
lines changed

src/args/parser.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> {
396396
.into_some()
397397
}
398398

399-
// TODO
399+
// TODO: fix warn
400400
#[allow(clippy::type_complexity)]
401401
pub fn update_post(
402402
args: &Args,

src/display/colorful.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::args::TimeStyle;
1111
use crate::infra::iter::IteratorExt;
1212
use crate::infra::result::IntoResult;
1313
use crate::infra::str::StrExt;
14+
use crate::infra::terminal::get_term_width;
1415
use crate::infra::time::display_cnb_time;
1516
use anyhow::Result;
1617
use colored::Colorize;
@@ -111,9 +112,7 @@ pub fn list_ing(
111112
writeln!(buf, " {} {}", "#".dimmed(), ing.id.to_string().dimmed())?;
112113
let content = if align {
113114
let user_name_width = ing.user_name.width_cjk();
114-
let term_width =
115-
terminal_size().expect("Can not get terminal size").0 .0 as usize;
116-
let left_width = term_width.saturating_sub(user_name_width + 3);
115+
let left_width = get_term_width().saturating_sub(user_name_width + 3);
117116
fmt_content(&ing.content)
118117
.width_split(left_width)
119118
.map_or_else(
@@ -271,7 +270,6 @@ pub fn list_post(result: &Result<(Vec<PostEntry>, usize)>, rev: bool) -> Result<
271270
};
272271

273272
entry_list.iter().dyn_rev(rev).try_fold(
274-
//TODO: formatln! macro
275273
format!("{}/{}\n", entry_list.len(), total_count),
276274
|mut buf, entry| try {
277275
{

src/display/normal.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use crate::args::TimeStyle;
1111
use crate::infra::iter::IteratorExt;
1212
use crate::infra::result::IntoResult;
1313
use crate::infra::str::StrExt;
14+
use crate::infra::terminal::get_term_width;
1415
use crate::infra::time::display_cnb_time;
1516
use anyhow::{Context, Result};
1617
use std::fmt::{Display, Write};
@@ -109,11 +110,7 @@ pub fn list_ing(
109110
writeln!(buf, " # {}", ing.id)?;
110111
let content = if align {
111112
let user_name_width = ing.user_name.width_cjk();
112-
let term_width = terminal_size()
113-
.with_context(|| "Can not get terminal size")?
114-
.0
115-
.0 as usize;
116-
let left_width = term_width.saturating_sub(user_name_width + 3);
113+
let left_width = get_term_width().saturating_sub(user_name_width + 3);
117114
fmt_content(&ing.content)
118115
.width_split(left_width)
119116
.map_or_else(
@@ -265,7 +262,6 @@ pub fn list_post(result: &Result<(Vec<PostEntry>, usize)>, rev: bool) -> Result<
265262
};
266263

267264
entry_list.iter().dyn_rev(rev).try_fold(
268-
//TODO: formatln! macro
269265
format!("{}/{}\n", entry_list.len(), total_count),
270266
|mut buf, entry| try {
271267
{

src/infra/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ pub mod json;
55
pub mod option;
66
pub mod result;
77
pub mod str;
8+
pub mod terminal;
89
pub mod time;
910
pub mod vec;

src/infra/terminal.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use terminal_size::{terminal_size, Width};
2+
3+
pub fn get_term_width() -> usize {
4+
let (Width(width), _) = terminal_size().expect("Can not get terminal size");
5+
width as usize
6+
}

0 commit comments

Comments
 (0)