mirror of
https://github.com/jht5945/rust_util.git
synced 2026-01-12 15:20:05 +08:00
Compare commits
7 Commits
d75c331fa0
...
7bfffc7bb6
| Author | SHA1 | Date | |
|---|---|---|---|
| 7bfffc7bb6 | |||
| d7ffd4ca6c | |||
| 7853214680 | |||
| 57c757d770 | |||
| 46528c566c | |||
| 2f7d7d238e | |||
| dd0a754631 |
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "rust_util"
|
name = "rust_util"
|
||||||
version = "0.2.2"
|
version = "0.2.3"
|
||||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "Hatter's Rust Util"
|
description = "Hatter's Rust Util"
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ pub mod util_os;
|
|||||||
pub mod util_env;
|
pub mod util_env;
|
||||||
pub mod util_cmd;
|
pub mod util_cmd;
|
||||||
pub mod util_msg;
|
pub mod util_msg;
|
||||||
|
pub mod util_str;
|
||||||
pub mod util_size;
|
pub mod util_size;
|
||||||
pub mod util_file;
|
pub mod util_file;
|
||||||
pub mod util_time;
|
pub mod util_time;
|
||||||
|
|||||||
@@ -17,6 +17,17 @@ pub fn get_home_str() -> Option<String> {
|
|||||||
iff!(util_os::is_macos_or_linux(), env::var("HOME").ok(), None)
|
iff!(util_os::is_macos_or_linux(), env::var("HOME").ok(), None)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn resolve_file_path(path: &str) -> String {
|
||||||
|
let home_path = match get_home_str() {
|
||||||
|
Some(p) => p, None => return path.to_owned(),
|
||||||
|
};
|
||||||
|
match path {
|
||||||
|
"~" => home_path,
|
||||||
|
p if p.starts_with("~/") => home_path + &path.chars().skip(1).collect::<String>(),
|
||||||
|
p => p.to_owned(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_home_path() -> Option<PathBuf> {
|
pub fn get_home_path() -> Option<PathBuf> {
|
||||||
Some(PathBuf::from(get_home_str()?))
|
Some(PathBuf::from(get_home_str()?))
|
||||||
}
|
}
|
||||||
@@ -31,7 +42,7 @@ pub fn get_absolute_path(path: &str) -> Option<PathBuf> {
|
|||||||
|
|
||||||
pub fn read_file_content(file: &str) -> XResult<String> {
|
pub fn read_file_content(file: &str) -> XResult<String> {
|
||||||
match get_absolute_path(file) {
|
match get_absolute_path(file) {
|
||||||
None => return Err(new_box_ioerror(&format!("File not found: {}", file))),
|
None => Err(new_box_ioerror(&format!("File not found: {}", file))),
|
||||||
Some(p) => util_io::read_to_string(&mut fs::File::open(p)?),
|
Some(p) => util_io::read_to_string(&mut fs::File::open(p)?),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,14 +89,12 @@ fn walk_dir_with_depth_check<FError, FProcess, FFilter>(depth: &mut u32, dir: &P
|
|||||||
let sub_dir = path_buf.as_path();
|
let sub_dir = path_buf.as_path();
|
||||||
if sub_dir.is_file() {
|
if sub_dir.is_file() {
|
||||||
func_process_file(&sub_dir);
|
func_process_file(&sub_dir);
|
||||||
} else if sub_dir.is_dir() {
|
} else if sub_dir.is_dir() && func_filter_dir(&sub_dir) {
|
||||||
if func_filter_dir(&sub_dir) {
|
|
||||||
*depth += 1;
|
*depth += 1;
|
||||||
if let Err(err) = walk_dir_with_depth_check(depth, &sub_dir, func_walk_error, func_process_file, func_filter_dir) {
|
if let Err(err) = walk_dir_with_depth_check(depth, &sub_dir, func_walk_error, func_process_file, func_filter_dir) {
|
||||||
func_walk_error(&sub_dir, err);
|
func_walk_error(&sub_dir, err);
|
||||||
}
|
}
|
||||||
*depth -= 1;
|
*depth -= 1;
|
||||||
}
|
|
||||||
} // should process else ? not file, dir
|
} // should process else ? not file, dir
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
|
fs::File,
|
||||||
io::{self,
|
io::{self,
|
||||||
ErrorKind,
|
ErrorKind,
|
||||||
prelude::*,
|
prelude::*,
|
||||||
@@ -7,13 +8,25 @@ use std::{
|
|||||||
time::{SystemTime, Duration},
|
time::{SystemTime, Duration},
|
||||||
};
|
};
|
||||||
|
|
||||||
use super::XResult;
|
use super::{ XResult, new_box_ioerror, };
|
||||||
use super::util_size::get_display_size;
|
use super::util_size::get_display_size;
|
||||||
use super::util_msg::print_lastline;
|
use super::util_msg::print_lastline;
|
||||||
|
use super::util_file::resolve_file_path;
|
||||||
|
|
||||||
pub const DEFAULT_BUF_SIZE: usize = 8 * 1024;
|
pub const DEFAULT_BUF_SIZE: usize = 8 * 1024;
|
||||||
|
|
||||||
|
|
||||||
|
pub fn get_read_stdin_or_file(file: &str) -> XResult<Box<dyn Read>> {
|
||||||
|
if file.is_empty() {
|
||||||
|
Ok(Box::new(io::stdin()))
|
||||||
|
} else {
|
||||||
|
match File::open(&resolve_file_path(file)) {
|
||||||
|
Ok(f) => Ok(Box::new(f)),
|
||||||
|
Err(err) => Err(new_box_ioerror(&format!("Open file {}, erorr: {}", file, err))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn read_to_string(read: &mut dyn Read) -> XResult<String> {
|
pub fn read_to_string(read: &mut dyn Read) -> XResult<String> {
|
||||||
let mut buffer = String::new();
|
let mut buffer = String::new();
|
||||||
read.read_to_string(&mut buffer)?;
|
read.read_to_string(&mut buffer)?;
|
||||||
|
|||||||
@@ -5,9 +5,10 @@ use std::{
|
|||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref IS_ATTY: bool = is_atty();
|
pub static ref IS_ATTY: bool = is_atty();
|
||||||
static ref PRINT_MESSAGE_LOCK: Arc<Mutex<usize>> = Arc::new(Mutex::new(0));
|
static ref PRINT_MESSAGE_LOCK: Arc<Mutex<()>> = Arc::new(Mutex::new(()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
pub enum MessageType { INFO, OK, WARN, ERROR, DEBUG, }
|
pub enum MessageType { INFO, OK, WARN, ERROR, DEBUG, }
|
||||||
|
|
||||||
pub fn is_atty() -> bool {
|
pub fn is_atty() -> bool {
|
||||||
@@ -40,9 +41,15 @@ pub fn print_message_ex(color: Option<term::color::Color>, h: &str, message: &st
|
|||||||
let mut lock = PRINT_MESSAGE_LOCK.lock().unwrap();
|
let mut lock = PRINT_MESSAGE_LOCK.lock().unwrap();
|
||||||
print_color(color, true, h);
|
print_color(color, true, h);
|
||||||
println!(" {}", message);
|
println!(" {}", message);
|
||||||
*lock += 1;
|
*lock = ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn print_ok (message: &str) { print_message(MessageType::OK, message); }
|
||||||
|
pub fn print_warn (message: &str) { print_message(MessageType::WARN, message); }
|
||||||
|
pub fn print_error(message: &str) { print_message(MessageType::ERROR, message); }
|
||||||
|
pub fn print_info (message: &str) { print_message(MessageType::INFO, message); }
|
||||||
|
pub fn print_debug(message: &str) { print_message(MessageType::DEBUG, message); }
|
||||||
|
|
||||||
pub fn print_message(mt: MessageType, message: &str) {
|
pub fn print_message(mt: MessageType, message: &str) {
|
||||||
match mt {
|
match mt {
|
||||||
MessageType::OK => print_message_ex(Some(term::color::GREEN), "[OK ]", message),
|
MessageType::OK => print_message_ex(Some(term::color::GREEN), "[OK ]", message),
|
||||||
@@ -53,6 +60,12 @@ pub fn print_message(mt: MessageType, message: &str) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl MessageType {
|
||||||
|
pub fn print(&self, message: &str) {
|
||||||
|
print_message(*self, message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn flush_stdout() {
|
pub fn flush_stdout() {
|
||||||
io::stdout().flush().ok();
|
io::stdout().flush().ok();
|
||||||
}
|
}
|
||||||
|
|||||||
30
src/util_str.rs
Normal file
30
src/util_str.rs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
|
||||||
|
|
||||||
|
pub fn split_kv(s: &str, split: char) -> (String, String) {
|
||||||
|
let mut k = String::new();
|
||||||
|
let mut v = String::new();
|
||||||
|
|
||||||
|
let mut is_splited = false;
|
||||||
|
let cs = s.chars();
|
||||||
|
for c in cs {
|
||||||
|
if is_splited {
|
||||||
|
v.push(c);
|
||||||
|
} else if c == split {
|
||||||
|
is_splited = true;
|
||||||
|
} else {
|
||||||
|
k.push(c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
(k, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_split_kv() {
|
||||||
|
assert_eq!(("".to_owned(), "".to_owned()), split_kv("", '='));
|
||||||
|
assert_eq!(("aaaa".to_owned(), "".to_owned()), split_kv("aaaa", '='));
|
||||||
|
assert_eq!(("".to_owned(), "aaaa".to_owned()), split_kv("=aaaa", '='));
|
||||||
|
assert_eq!(("aa".to_owned(), "bb".to_owned()), split_kv("aa=bb", '='));
|
||||||
|
assert_eq!(("aa".to_owned(), "bb".to_owned()), split_kv("aa:bb", ':'));
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user