1
0
mirror of https://github.com/jht5945/rust_util.git synced 2025-12-27 15:40:03 +08:00
This commit is contained in:
2020-04-12 00:55:37 +08:00
parent b8da734669
commit 7811e29921
3 changed files with 11 additions and 21 deletions

View File

@@ -2,8 +2,5 @@
use std::env;
pub fn is_env_on(var: &str) -> bool {
match env::var(var) {
Err(_) => false,
Ok(v) => (v == "TRUE" || v == "true" || v =="YES" || v == "yes" || v == "1"),
}
env::var(var).map(|v| v.to_lowercase()).map(|v| (v == "true" || v == "yes" || v == "1")).unwrap_or(false)
}

View File

@@ -6,6 +6,7 @@ use std::{
};
use super::{
iff,
util_os,
util_io,
new_box_ioerror,
@@ -13,11 +14,7 @@ use super::{
};
pub fn get_home_str() -> Option<String> {
if util_os::is_macos_or_linux() {
env::var("HOME").ok()
} else {
None
}
iff!(util_os::is_macos_or_linux(), env::var("HOME").ok(), None)
}
pub fn get_home_path() -> Option<PathBuf> {
@@ -25,12 +22,11 @@ pub fn get_home_path() -> Option<PathBuf> {
}
pub fn get_absolute_path(path: &str) -> Option<PathBuf> {
if path == "~" {
return Some(PathBuf::from(get_home_str()?));
} else if path.starts_with("~/") {
return Some(PathBuf::from(&format!("{}/{}", get_home_str()?, &path[2..])));
match path {
"~" => Some(PathBuf::from(get_home_str()?)),
path if path.starts_with("~/") => Some(PathBuf::from(&format!("{}/{}", get_home_str()?, &path[2..]))),
path => fs::canonicalize(path).ok(),
}
fs::canonicalize(path).ok()
}
pub fn read_file_content(file: &str) -> XResult<String> {
@@ -41,10 +37,7 @@ pub fn read_file_content(file: &str) -> XResult<String> {
}
pub fn is_symlink(path: &Path) -> bool {
match path.symlink_metadata() {
Err(_) => false,
Ok(meta) => meta.file_type().is_symlink(),
}
path.symlink_metadata().map(|meta| meta.file_type().is_symlink()).unwrap_or(false)
}
pub fn walk_dir<FError, FProcess, FFilter>(dir: &Path,

View File

@@ -8,7 +8,7 @@ lazy_static! {
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) };
stdout_fileno != 0
}
@@ -83,9 +83,9 @@ pub fn get_term_width_message(message: &str, left: usize) -> String {
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(&message[0..find_char_boundary(&message, w - 10 - 5 - left)]);
s.push_str("[...]");
s.push_str(&message[find_char_boundary(&message, len-10)..]);
s.push_str(&message[find_char_boundary(&message, len - 10)..]);
s
},
}