Files
keep/src/modes/common.rs
2025-05-10 12:38:57 -03:00

67 lines
2.2 KiB
Rust

use humansize::{FormatSizeOptions, BINARY};
use log::debug;
use regex::Regex;
use std::collections::HashMap;
use std::env;
pub fn get_meta_from_env() -> HashMap<String, String> {
debug!("MAIN: Getting meta from KEEP_META_*");
let re = Regex::new(r"^KEEP_META_(.+)$").unwrap();
let mut meta_env: HashMap<String, String> = 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)
}
pub const FORMAT_BOX_CHARS_NO_BORDER_LINE_SEPARATOR: prettytable::format::TableFormat =
prettytable::format::FormatBuilder::new()
.column_separator('│')
.borders('│')
.separators(
&[prettytable::format::LinePosition::Top],
prettytable::format::LineSeparator::new('─', '┬', '┌', '┐')
)
.separators(
&[prettytable::format::LinePosition::Title],
prettytable::format::LineSeparator::new('─', '┼', '├', '┤')
)
.separators(
&[prettytable::format::LinePosition::Bottom],
prettytable::format::LineSeparator::new('─', '┴', '└', '┘')
)
.padding(1, 1)
.build();
}