Ugh
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
use crate::compression_engine::CompressionType;
|
||||
/// Common utilities shared across different modes in the Keep application.
|
||||
///
|
||||
/// This module provides helper functions for formatting, configuration parsing,
|
||||
@@ -13,11 +14,10 @@
|
||||
/// let format = OutputFormat::from_str("json")?;
|
||||
/// ```
|
||||
use crate::config;
|
||||
use crate::compression_engine::CompressionType;
|
||||
use crate::meta_plugin::MetaPluginType;
|
||||
use clap::Command;
|
||||
use clap::error::ErrorKind;
|
||||
use comfy_table::{Table, ContentArrangement};
|
||||
use comfy_table::{ContentArrangement, Table};
|
||||
use log::debug;
|
||||
use regex::Regex;
|
||||
use std::collections::HashMap;
|
||||
@@ -116,7 +116,7 @@ pub fn format_size(size: u64, human_readable: bool) -> String {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Eq, PartialEq, Clone, strum::EnumIter, strum::Display, strum::EnumString)]
|
||||
#[derive(Debug, Eq, PartialEq, Clone, strum::EnumIter, strum::Display)]
|
||||
#[strum(ascii_case_insensitive)]
|
||||
/// Enum representing column types for table display.
|
||||
///
|
||||
@@ -151,34 +151,20 @@ pub enum ColumnType {
|
||||
Meta,
|
||||
}
|
||||
|
||||
impl ColumnType {
|
||||
/// Parses a string to a ColumnType, handling "meta:<name>" pattern.
|
||||
///
|
||||
/// Supports direct enum variants or "meta:<name>" for metadata columns.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `s` - Input string to parse, e.g., "size" or "meta:hostname".
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// * `Ok(ColumnType)` - Parsed type on success.
|
||||
/// * `Err(anyhow::Error)` - If the string doesn't match any variant.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
/// use keep::modes::common::ColumnType;
|
||||
/// let meta = ColumnType::from_str("meta:hostname").unwrap();
|
||||
/// assert_eq!(meta, ColumnType::Meta);
|
||||
/// ```
|
||||
pub fn from_str(s: &str) -> anyhow::Result<Self> {
|
||||
impl std::str::FromStr for ColumnType {
|
||||
type Err = anyhow::Error;
|
||||
|
||||
fn from_str(s: &str) -> anyhow::Result<Self> {
|
||||
let lower_s = s.to_lowercase();
|
||||
if s.starts_with("meta:") {
|
||||
// Handle meta:<name> pattern - this is still a Meta column type
|
||||
Ok(ColumnType::Meta)
|
||||
} else {
|
||||
// Handle regular column types
|
||||
Ok(Self::try_from(s)?)
|
||||
for variant in ColumnType::iter() {
|
||||
if variant.to_string().to_lowercase() == lower_s {
|
||||
return Ok(variant);
|
||||
}
|
||||
}
|
||||
Err(anyhow::anyhow!("Invalid column type: {}", s))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -199,30 +185,34 @@ impl ColumnType {
|
||||
/// # Panics
|
||||
///
|
||||
/// Exits via Clap error if unknown plugin type specified.
|
||||
pub fn settings_meta_plugin_types(cmd: &mut Command, settings: &config::Settings) -> Vec<MetaPluginType> {
|
||||
pub fn settings_meta_plugin_types(
|
||||
cmd: &mut Command,
|
||||
settings: &config::Settings,
|
||||
) -> Vec<MetaPluginType> {
|
||||
let mut meta_plugin_types = Vec::new();
|
||||
|
||||
|
||||
// Handle comma-separated values in each meta_plugins argument
|
||||
for meta_plugin_names_str in &settings.meta_plugins_names() {
|
||||
let meta_plugin_names: Vec<&str> = meta_plugin_names_str.split(',').collect();
|
||||
|
||||
|
||||
for name in meta_plugin_names {
|
||||
let trimmed_name = name.trim();
|
||||
if trimmed_name.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Try to find the MetaPluginType by meta name
|
||||
let mut found = false;
|
||||
for meta_plugin_type in MetaPluginType::iter() {
|
||||
let meta_plugin = crate::meta_plugin::get_meta_plugin(meta_plugin_type.clone(), None, None);
|
||||
let meta_plugin =
|
||||
crate::meta_plugin::get_meta_plugin(meta_plugin_type.clone(), None, None);
|
||||
if meta_plugin.meta_type().to_string() == trimmed_name {
|
||||
meta_plugin_types.push(meta_plugin_type);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if !found {
|
||||
cmd.error(
|
||||
ErrorKind::InvalidValue,
|
||||
@@ -252,7 +242,10 @@ pub fn settings_meta_plugin_types(cmd: &mut Command, settings: &config::Settings
|
||||
/// # Panics
|
||||
///
|
||||
/// Exits via Clap error if invalid compression specified.
|
||||
pub fn settings_compression_type(cmd: &mut Command, settings: &config::Settings) -> CompressionType {
|
||||
pub fn settings_compression_type(
|
||||
cmd: &mut Command,
|
||||
settings: &config::Settings,
|
||||
) -> CompressionType {
|
||||
let compression_name = settings
|
||||
.compression()
|
||||
.unwrap_or(CompressionType::LZ4.to_string());
|
||||
@@ -261,7 +254,10 @@ pub fn settings_compression_type(cmd: &mut Command, settings: &config::Settings)
|
||||
if compression_type_opt.is_err() {
|
||||
cmd.error(
|
||||
ErrorKind::InvalidValue,
|
||||
format!("Invalid compression algorithm '{}'. Supported algorithms: lz4, gzip, xz, zstd", compression_name),
|
||||
format!(
|
||||
"Invalid compression algorithm '{}'. Supported algorithms: lz4, gzip, xz, zstd",
|
||||
compression_name
|
||||
),
|
||||
)
|
||||
.exit();
|
||||
}
|
||||
@@ -288,7 +284,8 @@ pub fn settings_compression_type(cmd: &mut Command, settings: &config::Settings)
|
||||
/// assert_eq!(format, OutputFormat::Json); // If settings.output_format = Some("json")
|
||||
/// ```
|
||||
pub fn settings_output_format(settings: &config::Settings) -> OutputFormat {
|
||||
settings.output_format
|
||||
settings
|
||||
.output_format
|
||||
.as_ref()
|
||||
.and_then(|s| OutputFormat::from_str(s).ok())
|
||||
.unwrap_or(OutputFormat::Table)
|
||||
@@ -340,7 +337,7 @@ pub fn trim_lines_end(s: &str) -> String {
|
||||
pub fn create_table(use_styling: bool) -> Table {
|
||||
let mut table = Table::new();
|
||||
table.set_content_arrangement(ContentArrangement::Dynamic);
|
||||
|
||||
|
||||
if use_styling {
|
||||
if std::io::stdout().is_terminal() {
|
||||
table
|
||||
@@ -352,7 +349,7 @@ pub fn create_table(use_styling: bool) -> Table {
|
||||
} else {
|
||||
table.load_preset(comfy_table::presets::NOTHING);
|
||||
}
|
||||
|
||||
|
||||
if !std::io::stdout().is_terminal() {
|
||||
table.force_no_tty();
|
||||
}
|
||||
@@ -379,14 +376,20 @@ pub fn create_table(use_styling: bool) -> Table {
|
||||
/// ```
|
||||
pub fn create_table_with_config(table_config: &crate::config::TableConfig) -> Table {
|
||||
let mut table = Table::new();
|
||||
|
||||
|
||||
// Set content arrangement
|
||||
match table_config.content_arrangement {
|
||||
crate::config::ContentArrangement::Dynamic => table.set_content_arrangement(comfy_table::ContentArrangement::Dynamic),
|
||||
crate::config::ContentArrangement::DynamicFullWidth => table.set_content_arrangement(comfy_table::ContentArrangement::DynamicFullWidth),
|
||||
crate::config::ContentArrangement::Disabled => table.set_content_arrangement(comfy_table::ContentArrangement::Disabled),
|
||||
crate::config::ContentArrangement::Dynamic => {
|
||||
table.set_content_arrangement(comfy_table::ContentArrangement::Dynamic)
|
||||
}
|
||||
crate::config::ContentArrangement::DynamicFullWidth => {
|
||||
table.set_content_arrangement(comfy_table::ContentArrangement::DynamicFullWidth)
|
||||
}
|
||||
crate::config::ContentArrangement::Disabled => {
|
||||
table.set_content_arrangement(comfy_table::ContentArrangement::Disabled)
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Set style preset
|
||||
match &table_config.style {
|
||||
crate::config::TableStyle::Ascii => {
|
||||
@@ -414,7 +417,7 @@ pub fn create_table_with_config(table_config: &crate::config::TableConfig) -> Ta
|
||||
// Add more presets as needed
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
// Apply modifiers
|
||||
for modifier in &table_config.modifiers {
|
||||
match modifier.as_str() {
|
||||
@@ -427,16 +430,15 @@ pub fn create_table_with_config(table_config: &crate::config::TableConfig) -> Ta
|
||||
_ => {} // Ignore unknown modifiers
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Set truncation indicator if specified
|
||||
if !table_config.truncation_indicator.is_empty() {
|
||||
table.set_truncation_indicator(&table_config.truncation_indicator);
|
||||
}
|
||||
|
||||
|
||||
if !std::io::stdout().is_terminal() {
|
||||
table.force_no_tty();
|
||||
}
|
||||
|
||||
|
||||
table
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user