From 331a971d3d325495e5419df436cb517f60ebced4 Mon Sep 17 00:00:00 2001 From: "Andrew Phillips (aider)" Date: Fri, 9 May 2025 16:20:54 -0300 Subject: [PATCH] refactor: Move common utility functions to src/modes/common.rs and update module structure --- src/main.rs | 1 + src/modes/common.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++ src/modes/mod.rs | 1 + 3 files changed, 50 insertions(+) create mode 100644 src/modes/common.rs create mode 100644 src/modes/mod.rs diff --git a/src/main.rs b/src/main.rs index 0eb032b..12fa26f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,6 +20,7 @@ use strum::IntoEnumIterator; use clap::error::ErrorKind; use clap::*; use log::*; +use crate::modes::common::*; extern crate directories; use directories::ProjectDirs; diff --git a/src/modes/common.rs b/src/modes/common.rs new file mode 100644 index 0000000..2d49c55 --- /dev/null +++ b/src/modes/common.rs @@ -0,0 +1,48 @@ +use anyhow::Result; +use std::env; +use std::collections::HashMap; +use regex::Regex; +use humansize::{FormatSizeOptions, BINARY}; +use log::debug; + +pub fn get_meta_from_env() -> HashMap { + debug!("MAIN: Getting meta from KEEP_META_*"); + let re = Regex::new(r"^KEEP_META_(.+)$").unwrap(); + let mut meta_env: HashMap = HashMap::new(); + for (key, value) in env::vars() { + if let Some(meta_name_caps) = re.captures(key.as_str()) { + let name = String::from(meta_name_caps.get(1).unwrap().as_str()); + debug!("MAIN: Found meta: {}={}", name.clone(), value.clone()); + meta_env.insert(name, value.clone()); + } + } + meta_env +} + +pub fn format_size_human_readable(size: u64) -> String { + let options = FormatSizeOptions::from(BINARY) + .decimal_places(1); + humansize::format_size(size, options) +} + +pub fn format_size(size: u64, human_readable: bool) -> String { + match human_readable { + true => format_size_human_readable(size), + false => size.to_string() + } +} + +pub fn string_column(s: String, column_width: usize) -> String { + if column_width > 0 { + match s.char_indices().nth(column_width) { + None => s.to_string(), + Some((idx, _)) => s[..idx].to_string(), + } + } else { + s.to_string() + } +} + +pub fn size_column(size: u64, human_readable: bool, column_width: usize) -> String { + string_column(format_size(size, human_readable), column_width) +} diff --git a/src/modes/mod.rs b/src/modes/mod.rs new file mode 100644 index 0000000..34994bf --- /dev/null +++ b/src/modes/mod.rs @@ -0,0 +1 @@ +pub mod common;