use crate::output::{conditional_eprintln, should_echo};
use crate::style::{BOLD_CYAN, BOLD_UNDERLINE, BRIGHT_BLACK};
use std::path::Path;
fn echo_operation(op: &str, details: &str) {
if should_echo() {
let styled_fs = format!(
" {BRIGHT_BLACK}{}:fs{BRIGHT_BLACK:#}",
env!("CARGO_PKG_NAME")
);
let styled_op = format!("{BOLD_CYAN}{op}{BOLD_CYAN:#}");
let styled_details = format!("{BOLD_UNDERLINE}{details}{BOLD_UNDERLINE:#}");
conditional_eprintln(format_args!(
"{} {} {}",
styled_fs, styled_op, styled_details
));
}
}
pub fn copy(from: impl AsRef<Path>, to: impl AsRef<Path>) -> std::io::Result<u64> {
let from = from.as_ref();
let to = to.as_ref();
echo_operation("copy", &format!("{} -> {}", from.display(), to.display()));
std::fs::copy(from, to)
}
pub fn create_dir(path: impl AsRef<Path>) -> std::io::Result<()> {
let path = path.as_ref();
echo_operation("create_dir", &path.display().to_string());
std::fs::create_dir(path)
}
pub fn create_dir_all(path: impl AsRef<Path>) -> std::io::Result<()> {
let path = path.as_ref();
echo_operation("create_dir_all", &path.display().to_string());
std::fs::create_dir_all(path)
}
pub fn hard_link(original: impl AsRef<Path>, link: impl AsRef<Path>) -> std::io::Result<()> {
let original = original.as_ref();
let link = link.as_ref();
echo_operation(
"hard_link",
&format!("{} -> {}", original.display(), link.display()),
);
std::fs::hard_link(original, link)
}
pub fn metadata(path: impl AsRef<Path>) -> std::io::Result<std::fs::Metadata> {
let path = path.as_ref();
echo_operation("metadata", &path.display().to_string());
std::fs::metadata(path)
}
pub fn read(path: impl AsRef<Path>) -> std::io::Result<Vec<u8>> {
let path = path.as_ref();
echo_operation("read", &path.display().to_string());
std::fs::read(path)
}
pub fn read_dir(path: impl AsRef<Path>) -> std::io::Result<std::fs::ReadDir> {
let path = path.as_ref();
echo_operation("read_dir", &path.display().to_string());
std::fs::read_dir(path)
}
pub fn read_to_string(path: impl AsRef<Path>) -> std::io::Result<String> {
let path = path.as_ref();
echo_operation("read_to_string", &path.display().to_string());
std::fs::read_to_string(path)
}
pub fn remove_dir(path: impl AsRef<Path>) -> std::io::Result<()> {
let path = path.as_ref();
echo_operation("remove_dir", &path.display().to_string());
std::fs::remove_dir(path)
}
pub fn remove_dir_all(path: impl AsRef<Path>) -> std::io::Result<()> {
let path = path.as_ref();
echo_operation("remove_dir_all", &path.display().to_string());
std::fs::remove_dir_all(path)
}
pub fn remove_file(path: impl AsRef<Path>) -> std::io::Result<()> {
let path = path.as_ref();
echo_operation("remove_file", &path.display().to_string());
std::fs::remove_file(path)
}
pub fn rename(from: impl AsRef<Path>, to: impl AsRef<Path>) -> std::io::Result<()> {
let from = from.as_ref();
let to = to.as_ref();
echo_operation("rename", &format!("{} -> {}", from.display(), to.display()));
std::fs::rename(from, to)
}
pub fn set_permissions(path: impl AsRef<Path>, perm: std::fs::Permissions) -> std::io::Result<()> {
let path = path.as_ref();
echo_operation("set_permissions", &path.display().to_string());
std::fs::set_permissions(path, perm)
}
pub fn symlink_metadata(path: impl AsRef<Path>) -> std::io::Result<std::fs::Metadata> {
let path = path.as_ref();
echo_operation("symlink_metadata", &path.display().to_string());
std::fs::symlink_metadata(path)
}
pub fn write(path: impl AsRef<Path>, contents: impl AsRef<[u8]>) -> std::io::Result<()> {
let path = path.as_ref();
let contents = contents.as_ref();
echo_operation(
"write",
&format!("{} bytes -> {}", contents.len(), path.display()),
);
std::fs::write(path, contents)
}