Skip to content

Commit

Permalink
refactor: simplify impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaumy committed Sep 25, 2023
1 parent f66f939 commit 5be3e5a
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/args/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ pub fn create_post(args: &Args) -> Option<(&String, &String, bool)> {
.into_some()
}

// TODO
// TODO: fix warn
#[allow(clippy::type_complexity)]
pub fn update_post(
args: &Args,
Expand Down
6 changes: 2 additions & 4 deletions src/display/colorful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::args::TimeStyle;
use crate::infra::iter::IteratorExt;
use crate::infra::result::IntoResult;
use crate::infra::str::StrExt;
use crate::infra::terminal::get_term_width;
use crate::infra::time::display_cnb_time;
use anyhow::Result;
use colored::Colorize;
Expand Down Expand Up @@ -111,9 +112,7 @@ pub fn list_ing(
writeln!(buf, " {} {}", "#".dimmed(), ing.id.to_string().dimmed())?;
let content = if align {
let user_name_width = ing.user_name.width_cjk();
let term_width =
terminal_size().expect("Can not get terminal size").0 .0 as usize;
let left_width = term_width.saturating_sub(user_name_width + 3);
let left_width = get_term_width().saturating_sub(user_name_width + 3);
fmt_content(&ing.content)
.width_split(left_width)
.map_or_else(
Expand Down Expand Up @@ -271,7 +270,6 @@ pub fn list_post(result: &Result<(Vec<PostEntry>, usize)>, rev: bool) -> Result<
};

entry_list.iter().dyn_rev(rev).try_fold(
//TODO: formatln! macro
format!("{}/{}\n", entry_list.len(), total_count),
|mut buf, entry| try {
{
Expand Down
8 changes: 2 additions & 6 deletions src/display/normal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::args::TimeStyle;
use crate::infra::iter::IteratorExt;
use crate::infra::result::IntoResult;
use crate::infra::str::StrExt;
use crate::infra::terminal::get_term_width;
use crate::infra::time::display_cnb_time;
use anyhow::{Context, Result};
use std::fmt::{Display, Write};
Expand Down Expand Up @@ -109,11 +110,7 @@ pub fn list_ing(
writeln!(buf, " # {}", ing.id)?;
let content = if align {
let user_name_width = ing.user_name.width_cjk();
let term_width = terminal_size()
.with_context(|| "Can not get terminal size")?
.0
.0 as usize;
let left_width = term_width.saturating_sub(user_name_width + 3);
let left_width = get_term_width().saturating_sub(user_name_width + 3);
fmt_content(&ing.content)
.width_split(left_width)
.map_or_else(
Expand Down Expand Up @@ -265,7 +262,6 @@ pub fn list_post(result: &Result<(Vec<PostEntry>, usize)>, rev: bool) -> Result<
};

entry_list.iter().dyn_rev(rev).try_fold(
//TODO: formatln! macro
format!("{}/{}\n", entry_list.len(), total_count),
|mut buf, entry| try {
{
Expand Down
1 change: 1 addition & 0 deletions src/infra/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ pub mod json;
pub mod option;
pub mod result;
pub mod str;
pub mod terminal;
pub mod time;
pub mod vec;
6 changes: 6 additions & 0 deletions src/infra/terminal.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use terminal_size::{terminal_size, Width};

pub fn get_term_width() -> usize {
let (Width(width), _) = terminal_size().expect("Can not get terminal size");
width as usize
}

0 comments on commit 5be3e5a

Please sign in to comment.