1
0
mirror of https://github.com/jht5945/finding.git synced 2025-12-28 05:30:04 +08:00

add local_util.rs

This commit is contained in:
2019-08-03 23:18:53 +08:00
parent 38b9bba23d
commit 2593d3391c
2 changed files with 64 additions and 55 deletions

View File

@@ -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<usize> {
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<String> {
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 {