From 2593d3391cdfdefb9336c237d37e01ce87848cc6 Mon Sep 17 00:00:00 2001 From: "Hatter Jiang@Pixelbook" Date: Sat, 3 Aug 2019 23:18:53 +0800 Subject: [PATCH] add local_util.rs --- src/local_util.rs | 62 +++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 57 ++----------------------------------------- 2 files changed, 64 insertions(+), 55 deletions(-) create mode 100644 src/local_util.rs diff --git a/src/local_util.rs b/src/local_util.rs new file mode 100644 index 0000000..1822f51 --- /dev/null +++ b/src/local_util.rs @@ -0,0 +1,62 @@ + +use std:: { + fs::File, + path::Path, + io::prelude::*, +}; + +use rust_util::*; + +pub fn get_term_width() -> Option { + match term_size::dimensions() { + None => None, + Some((w, _h)) => Some(w), + } +} + +// thanks https://blog.csdn.net/star_xiong/article/details/89401149 +pub fn find_char_boundary(s: &str, index: usize) -> usize { + if s.len() <= index { + return index; + } + let mut new_index = index; + while !s.is_char_boundary(new_index) { + new_index += 1; + } + new_index +} + +pub fn get_term_width_message(message: &str, left: usize) -> String { + match get_term_width() { + None => message.to_string(), + Some(w) => { + let len = message.len(); + if w > len { + return message.to_string(); + } + let mut s = String::new(); + s.push_str(&message[0..find_char_boundary(&message, w-10-5-left)]); + s.push_str("[...]"); + s.push_str(&message[find_char_boundary(&message, len-10)..]); + s + }, + } +} + +pub fn read_file_content(file: &Path, large_file_len: u64) -> XResult { + if ! file.exists() { + return Err(new_box_error(&format!("File not exists: {:?}", file))); + } + if ! file.is_file() { + return Err(new_box_error(&format!("File is not file: {:?}", file))); + } + let file_len = file.metadata()?.len(); + if file_len > large_file_len { + return Err(new_box_error(&format!("File too large: {:?}, len: {}", file, file_len))); + } + let mut f = File::open(file)?; + let mut content = String::new(); + f.read_to_string(&mut content)?; + + Ok(content) +} diff --git a/src/main.rs b/src/main.rs index 939ba31..a8311ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,17 +4,17 @@ extern crate term_size; extern crate rust_util; mod opt; +mod local_util; use std::{ cell::RefCell, - fs::File, path::Path, - io::prelude::*, time::SystemTime, }; use opt::*; use rust_util::*; +use local_util::*; const VERSION: &str = env!("CARGO_PKG_VERSION"); const GIT_HASH: &str = env!("GIT_HASH"); @@ -28,42 +28,6 @@ Written by Hatter Jiang "#, VERSION, &GIT_HASH[0..7]); } -fn get_term_width() -> Option { - match term_size::dimensions() { - None => None, - Some((w, _h)) => Some(w), - } -} - -// thanks https://blog.csdn.net/star_xiong/article/details/89401149 -fn find_char_boundary(s: &str, index: usize) -> usize { - if s.len() <= index { - return index; - } - let mut new_index = index; - while !s.is_char_boundary(new_index) { - new_index += 1; - } - new_index -} - -fn get_term_width_message(message: &str, left: usize) -> String { - match get_term_width() { - None => message.to_string(), - Some(w) => { - let len = message.len(); - if w > len { - return message.to_string(); - } - let mut s = String::new(); - s.push_str(&message[0..find_char_boundary(&message, w-10-5-left)]); - s.push_str("[...]"); - s.push_str(&message[find_char_boundary(&message, len-10)..]); - s - }, - } -} - fn find_huge_files(options: &Options, dir_path: &Path) { let total_file_count_cell = RefCell::new(0u64); let huge_file_count_cell = RefCell::new(0u64); @@ -119,23 +83,6 @@ fn find_huge_files(options: &Options, dir_path: &Path) { get_display_size(huge_file_size_cell.into_inner() as i64))); } -fn read_file_content(file: &Path, large_file_len: u64) -> XResult { - if ! file.exists() { - return Err(new_box_error(&format!("File not exists: {:?}", file))); - } - if ! file.is_file() { - return Err(new_box_error(&format!("File is not file: {:?}", file))); - } - let file_len = file.metadata()?.len(); - if file_len > large_file_len { - return Err(new_box_error(&format!("File too large: {:?}, len: {}", file, file_len))); - } - let mut f = File::open(file)?; - let mut content = String::new(); - f.read_to_string(&mut content)?; - - Ok(content) -} #[derive(Debug)] struct MatchLine {