mirror of
https://github.com/jht5945/rust_util.git
synced 2026-01-12 07:10:05 +08:00
Compare commits
2 Commits
0e0714f7cd
...
7811e29921
| Author | SHA1 | Date | |
|---|---|---|---|
| 7811e29921 | |||
| b8da734669 |
@@ -2,8 +2,5 @@
|
|||||||
use std::env;
|
use std::env;
|
||||||
|
|
||||||
pub fn is_env_on(var: &str) -> bool {
|
pub fn is_env_on(var: &str) -> bool {
|
||||||
match env::var(var) {
|
env::var(var).map(|v| v.to_lowercase()).map(|v| (v == "true" || v == "yes" || v == "1")).unwrap_or(false)
|
||||||
Err(_) => false,
|
|
||||||
Ok(v) => (v == "TRUE" || v == "true" || v =="YES" || v == "yes" || v == "1"),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,17 +6,15 @@ use std::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use super::{
|
use super::{
|
||||||
|
iff,
|
||||||
util_os,
|
util_os,
|
||||||
|
util_io,
|
||||||
new_box_ioerror,
|
new_box_ioerror,
|
||||||
XResult,
|
XResult,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn get_home_str() -> Option<String> {
|
pub fn get_home_str() -> Option<String> {
|
||||||
if util_os::is_macos_or_linux() {
|
iff!(util_os::is_macos_or_linux(), env::var("HOME").ok(), None)
|
||||||
env::var("HOME").ok()
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_home_path() -> Option<PathBuf> {
|
pub fn get_home_path() -> Option<PathBuf> {
|
||||||
@@ -24,19 +22,22 @@ pub fn get_home_path() -> Option<PathBuf> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_absolute_path(path: &str) -> Option<PathBuf> {
|
pub fn get_absolute_path(path: &str) -> Option<PathBuf> {
|
||||||
if path == "~" {
|
match path {
|
||||||
return Some(PathBuf::from(get_home_str()?));
|
"~" => Some(PathBuf::from(get_home_str()?)),
|
||||||
} else if path.starts_with("~/") {
|
path if path.starts_with("~/") => Some(PathBuf::from(&format!("{}/{}", get_home_str()?, &path[2..]))),
|
||||||
return Some(PathBuf::from(&format!("{}/{}", get_home_str()?, &path[2..])));
|
path => fs::canonicalize(path).ok(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn read_file_content(file: &str) -> XResult<String> {
|
||||||
|
match get_absolute_path(file) {
|
||||||
|
None => return Err(new_box_ioerror(&format!("File not found: {}", file))),
|
||||||
|
Some(p) => util_io::read_to_string(&mut fs::File::open(p)?),
|
||||||
}
|
}
|
||||||
fs::canonicalize(path).ok()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_symlink(path: &Path) -> bool {
|
pub fn is_symlink(path: &Path) -> bool {
|
||||||
match path.symlink_metadata() {
|
path.symlink_metadata().map(|meta| meta.file_type().is_symlink()).unwrap_or(false)
|
||||||
Err(_) => false,
|
|
||||||
Ok(meta) => meta.file_type().is_symlink(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn walk_dir<FError, FProcess, FFilter>(dir: &Path,
|
pub fn walk_dir<FError, FProcess, FFilter>(dir: &Path,
|
||||||
@@ -60,19 +61,17 @@ fn walk_dir_with_depth_check<FError, FProcess, FFilter>(depth: &mut u32, dir: &P
|
|||||||
return Err(new_box_ioerror(&format!("Depth exceed, depth: {}, path: {:?}", *depth, dir)));
|
return Err(new_box_ioerror(&format!("Depth exceed, depth: {}, path: {:?}", *depth, dir)));
|
||||||
}
|
}
|
||||||
let read_dir = match dir.read_dir() {
|
let read_dir = match dir.read_dir() {
|
||||||
Err(err) => {
|
Ok(rd) => rd, Err(err) => {
|
||||||
func_walk_error(&dir, Box::new(err));
|
func_walk_error(&dir, Box::new(err));
|
||||||
return Ok(());
|
return Ok(());
|
||||||
},
|
},
|
||||||
Ok(rd) => rd,
|
|
||||||
};
|
};
|
||||||
for dir_entry_item in read_dir {
|
for dir_entry_item in read_dir {
|
||||||
let dir_entry = match dir_entry_item {
|
let dir_entry = match dir_entry_item {
|
||||||
Err(err) => {
|
Ok(item) => item, Err(err) => {
|
||||||
func_walk_error(&dir, Box::new(err));
|
func_walk_error(&dir, Box::new(err));
|
||||||
continue; // Ok?
|
continue; // Ok?
|
||||||
},
|
},
|
||||||
Ok(item) => item,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let path_buf = dir_entry.path();
|
let path_buf = dir_entry.path();
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ lazy_static! {
|
|||||||
|
|
||||||
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 {
|
||||||
let stdout_fileno = unsafe { libc::isatty(libc::STDOUT_FILENO as i32) };
|
let stdout_fileno = unsafe { libc::isatty(libc::STDOUT_FILENO as i32) };
|
||||||
stdout_fileno != 0
|
stdout_fileno != 0
|
||||||
}
|
}
|
||||||
@@ -83,9 +83,9 @@ pub fn get_term_width_message(message: &str, left: usize) -> String {
|
|||||||
return message.to_string();
|
return message.to_string();
|
||||||
}
|
}
|
||||||
let mut s = String::new();
|
let mut s = String::new();
|
||||||
s.push_str(&message[0..find_char_boundary(&message, w-10-5-left)]);
|
s.push_str(&message[0..find_char_boundary(&message, w - 10 - 5 - left)]);
|
||||||
s.push_str("[...]");
|
s.push_str("[...]");
|
||||||
s.push_str(&message[find_char_boundary(&message, len-10)..]);
|
s.push_str(&message[find_char_boundary(&message, len - 10)..]);
|
||||||
s
|
s
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user