summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/utils.rs5
-rw-r--r--src/utils/display.rs26
-rw-r--r--src/utils/fs.rs40
3 files changed, 69 insertions, 2 deletions
diff --git a/src/utils.rs b/src/utils.rs
index ae10d39..3a181a3 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -1,6 +1,7 @@
-pub mod build_env_logger;
pub mod display;
+pub mod env;
+pub mod fs;
pub mod input;
-pub mod lang_selector;
+pub mod logger;
pub mod md_colored;
pub mod socket_addr_helper;
diff --git a/src/utils/display.rs b/src/utils/display.rs
index 533d46d..09efe97 100644
--- a/src/utils/display.rs
+++ b/src/utils/display.rs
@@ -1,3 +1,6 @@
+use colored::*;
+use regex::Regex;
+
pub struct SimpleTable {
items: Vec<String>,
line: Vec<Vec<String>>,
@@ -118,6 +121,10 @@ fn display_width(s: &str) -> usize {
width
}
+/// Convert byte size to a human-readable string format
+///
+/// Automatically selects the appropriate unit (B, KB, MB, GB, TB) based on the byte size
+/// and formats it as a string with two decimal places
pub fn size_str(total_size: usize) -> String {
if total_size < 1024 {
format!("{} B", total_size)
@@ -134,3 +141,22 @@ pub fn size_str(total_size: usize) -> String {
)
}
}
+
+// Convert the Markdown formatted text into a format supported by the command line
+pub fn md(text: impl AsRef<str>) -> String {
+ let bold_re = Regex::new(r"\*\*(.*?)\*\*").unwrap();
+ let mut result = bold_re
+ .replace_all(text.as_ref().trim(), |caps: &regex::Captures| {
+ format!("{}", caps[1].bold())
+ })
+ .to_string();
+
+ let italic_re = Regex::new(r"\*(.*?)\*").unwrap();
+ result = italic_re
+ .replace_all(&result, |caps: &regex::Captures| {
+ format!("{}", caps[1].italic())
+ })
+ .to_string();
+
+ result
+}
diff --git a/src/utils/fs.rs b/src/utils/fs.rs
new file mode 100644
index 0000000..0050cf1
--- /dev/null
+++ b/src/utils/fs.rs
@@ -0,0 +1,40 @@
+pub async fn move_across_partitions(
+ source_path: impl AsRef<std::path::Path>,
+ dest_path: impl AsRef<std::path::Path>,
+) -> Result<(), std::io::Error> {
+ let source_path = source_path.as_ref();
+ let dest_path = dest_path.as_ref();
+ if !source_path.exists() {
+ return Err(std::io::Error::new(
+ std::io::ErrorKind::NotFound,
+ "Source file does not exist",
+ ));
+ }
+
+ if let Ok(()) = std::fs::rename(source_path, dest_path) {
+ return Ok(());
+ }
+
+ std::fs::copy(source_path, dest_path)?;
+ std::fs::remove_file(source_path)?;
+
+ Ok(())
+}
+
+pub async fn copy_across_partitions(
+ source_path: impl AsRef<std::path::Path>,
+ dest_path: impl AsRef<std::path::Path>,
+) -> Result<(), std::io::Error> {
+ let source_path = source_path.as_ref();
+ let dest_path = dest_path.as_ref();
+ if !source_path.exists() {
+ return Err(std::io::Error::new(
+ std::io::ErrorKind::NotFound,
+ "Source file does not exist",
+ ));
+ }
+
+ std::fs::copy(source_path, dest_path)?;
+
+ Ok(())
+}