mirror of
https://github.com/jht5945/rust_util.git
synced 2025-12-27 15:40:03 +08:00
add get_home(), get_absolute_path()
This commit is contained in:
16
src/lib.rs
16
src/lib.rs
@@ -1,7 +1,10 @@
|
||||
extern crate term;
|
||||
|
||||
use std::{
|
||||
env,
|
||||
fs,
|
||||
io::{self, Write, Error, ErrorKind},
|
||||
path::PathBuf,
|
||||
process::Command,
|
||||
};
|
||||
|
||||
@@ -34,6 +37,19 @@ pub fn is_macos_or_linux() -> bool {
|
||||
is_macos() || is_linux()
|
||||
}
|
||||
|
||||
pub fn get_home() -> Option<String> {
|
||||
env::var("HOME").ok()
|
||||
}
|
||||
|
||||
pub fn get_absolute_path(path: &str) -> Option<PathBuf> {
|
||||
if path == "~" {
|
||||
return Some(PathBuf::from(get_home()?));
|
||||
} else if path.starts_with("~/") {
|
||||
return Some(PathBuf::from(&format!("{}/{}", get_home()?, &path[2..])));
|
||||
}
|
||||
fs::canonicalize(path).ok()
|
||||
}
|
||||
|
||||
pub enum MessageType { INFO, OK, WARN, ERROR, }
|
||||
|
||||
pub fn print_message_ex(color: Option<term::color::Color>, h: &str, message: &str) {
|
||||
|
||||
Reference in New Issue
Block a user