refactor: Enhance table generation and clean up dependencies

Co-authored-by: aider (openai/andrew/openrouter/deepseek/deepseek-chat-v3.1) <aider@aider.chat>
This commit is contained in:
Andrew Phillips
2025-09-08 18:29:50 -03:00
parent b4046e0b18
commit 26a8712d82
5 changed files with 31 additions and 79 deletions

View File

@@ -3,13 +3,13 @@ use crate::compression_engine::CompressionType;
use crate::meta_plugin::MetaPluginType; use crate::meta_plugin::MetaPluginType;
use clap::Command; use clap::Command;
use clap::error::ErrorKind; use clap::error::ErrorKind;
use comfy_table::{Table, ContentArrangement};
use log::debug; use log::debug;
use regex::Regex; use regex::Regex;
use std::collections::HashMap; use std::collections::HashMap;
use std::env; use std::env;
use std::str::FromStr; use std::str::FromStr;
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
use termsize;
#[derive(Debug, Clone, strum::EnumString, strum::Display, PartialEq)] #[derive(Debug, Clone, strum::EnumString, strum::Display, PartialEq)]
#[strum(ascii_case_insensitive)] #[strum(ascii_case_insensitive)]
pub enum OutputFormat { pub enum OutputFormat {
@@ -142,3 +142,20 @@ pub fn settings_output_format(settings: &config::Settings) -> OutputFormat {
.unwrap_or(OutputFormat::Table) .unwrap_or(OutputFormat::Table)
} }
/// Create a table with consistent styling and terminal detection
pub fn create_table(use_styling: bool) -> Table {
let mut table = Table::new();
table.set_content_arrangement(ContentArrangement::Dynamic);
if use_styling {
table
.load_preset(comfy_table::presets::UTF8_FULL)
.apply_modifier(comfy_table::modifiers::UTF8_ROUND_CORNERS);
} else {
table.load_preset(comfy_table::presets::NOTHING);
}
table.force_no_tty_if(!std::io::stdout().is_terminal());
table
}

View File

@@ -10,9 +10,7 @@ use std::path::PathBuf;
use crate::services::item_service::ItemService; use crate::services::item_service::ItemService;
use chrono::prelude::*; use chrono::prelude::*;
use is_terminal::IsTerminal; use is_terminal::IsTerminal;
use comfy_table::{Table, ContentArrangement, Cell, Attribute}; use comfy_table::{Cell, Attribute};
use comfy_table::presets::UTF8_FULL;
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
pub fn mode_info( pub fn mode_info(
cmd: &mut Command, cmd: &mut Command,
@@ -68,15 +66,7 @@ fn show_item(
let item_id = item.id.unwrap(); let item_id = item.id.unwrap();
let item_tags: Vec<String> = item_with_meta.tags.iter().map(|t| t.name.clone()).collect(); let item_tags: Vec<String> = item_with_meta.tags.iter().map(|t| t.name.clone()).collect();
let mut table = Table::new(); let mut table = crate::modes::common::create_table(true);
table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic);
if !std::io::stdout().is_terminal() {
table.force_no_tty();
}
// Add all the rows // Add all the rows
table.add_row(vec![ table.add_row(vec![

View File

@@ -2,7 +2,7 @@ use crate::config;
use crate::services::item_service::ItemService; use crate::services::item_service::ItemService;
use crate::services::types::ItemWithMeta; use crate::services::types::ItemWithMeta;
use crate::modes::common::ColumnType; use crate::modes::common::ColumnType;
use crate::modes::common::{size_column, string_column, OutputFormat}; use crate::modes::common::{format_size, OutputFormat};
use anyhow::{Result}; use anyhow::{Result};
use log::debug; use log::debug;
use comfy_table::{Table, ContentArrangement, Cell, Row, Color, Attribute}; use comfy_table::{Table, ContentArrangement, Cell, Row, Color, Attribute};
@@ -55,11 +55,8 @@ pub fn mode_list(
let mut table = Table::new(); let mut table = Table::new();
table table
.load_preset(NOTHING) .load_preset(NOTHING)
.set_content_arrangement(ContentArrangement::Dynamic); .set_content_arrangement(ContentArrangement::Dynamic)
.force_no_tty_if(!stdout().is_terminal());
if !stdout().is_terminal() {
table.force_no_tty();
}
// Create header row // Create header row
let mut header_cells = Vec::new(); let mut header_cells = Vec::new();

View File

@@ -9,24 +9,14 @@ use crate::config;
use crate::common::status::StatusInfo; use crate::common::status::StatusInfo;
use serde_json; use serde_json;
use serde_yaml; use serde_yaml;
use comfy_table::{Table, ContentArrangement, Cell, Attribute}; use comfy_table::{Cell, Attribute};
use comfy_table::presets::UTF8_FULL;
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
use crate::common::status::PathInfo; use crate::common::status::PathInfo;
use crate::meta_plugin::MetaPluginType; use crate::meta_plugin::MetaPluginType;
use crate::meta_plugin::get_meta_plugin; use crate::meta_plugin::get_meta_plugin;
fn build_path_table(path_info: &PathInfo) -> Table { fn build_path_table(path_info: &PathInfo) -> Table {
let mut path_table = Table::new(); let mut path_table = crate::modes::common::create_table(true);
path_table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic);
if !std::io::stdout().is_terminal() {
path_table.force_no_tty();
}
path_table.set_header(vec![ path_table.set_header(vec![
Cell::new("Type").add_attribute(Attribute::Bold), Cell::new("Type").add_attribute(Attribute::Bold),
@@ -41,15 +31,7 @@ fn build_path_table(path_info: &PathInfo) -> Table {
fn build_config_table(settings: &config::Settings) -> Table { fn build_config_table(settings: &config::Settings) -> Table {
let mut config_table = Table::new(); let mut config_table = crate::modes::common::create_table(true);
config_table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic);
if !std::io::stdout().is_terminal() {
config_table.force_no_tty();
}
config_table.set_header(vec![ config_table.set_header(vec![
Cell::new("Setting").add_attribute(Attribute::Bold), Cell::new("Setting").add_attribute(Attribute::Bold),
@@ -82,15 +64,7 @@ fn build_meta_plugins_configured_table(status_info: &StatusInfo) -> Option<Table
let mut sorted_meta_plugins = meta_plugins.clone(); let mut sorted_meta_plugins = meta_plugins.clone();
sorted_meta_plugins.sort_by(|a, b| a.name.cmp(&b.name)); sorted_meta_plugins.sort_by(|a, b| a.name.cmp(&b.name));
let mut table = Table::new(); let mut table = crate::modes::common::create_table(true);
table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic);
if !std::io::stdout().is_terminal() {
table.force_no_tty();
}
table.set_header(vec![ table.set_header(vec![
Cell::new("Plugin Name").add_attribute(Attribute::Bold), Cell::new("Plugin Name").add_attribute(Attribute::Bold),

View File

@@ -43,24 +43,14 @@ use crate::modes::common::OutputFormat;
use crate::config; use crate::config;
use serde_json; use serde_json;
use serde_yaml; use serde_yaml;
use comfy_table::{Table, ContentArrangement, Cell, Color, Attribute}; use comfy_table::{Cell, Color, Attribute};
use comfy_table::presets::UTF8_FULL;
use comfy_table::modifiers::UTF8_ROUND_CORNERS;
use crate::meta_plugin::{MetaPluginType, get_meta_plugin}; use crate::meta_plugin::{MetaPluginType, get_meta_plugin};
use crate::common::status::{MetaPluginInfo, CompressionInfo}; use crate::common::status::{MetaPluginInfo, CompressionInfo};
fn build_meta_plugin_table(meta_plugin_info: &std::collections::HashMap<String, MetaPluginInfo>) -> Table { fn build_meta_plugin_table(meta_plugin_info: &std::collections::HashMap<String, MetaPluginInfo>) -> Table {
let mut meta_plugin_table = Table::new(); let mut meta_plugin_table = crate::modes::common::create_table(true);
meta_plugin_table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic);
if !std::io::stdout().is_terminal() {
meta_plugin_table.force_no_tty();
}
meta_plugin_table.set_header(vec![ meta_plugin_table.set_header(vec![
Cell::new("Plugin Name").add_attribute(Attribute::Bold), Cell::new("Plugin Name").add_attribute(Attribute::Bold),
@@ -121,15 +111,7 @@ fn build_meta_plugin_table(meta_plugin_info: &std::collections::HashMap<String,
} }
fn build_compression_table(compression_info: &Vec<CompressionInfo>) -> Table { fn build_compression_table(compression_info: &Vec<CompressionInfo>) -> Table {
let mut compression_table = Table::new(); let mut compression_table = crate::modes::common::create_table(true);
compression_table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic);
if !std::io::stdout().is_terminal() {
compression_table.force_no_tty();
}
compression_table.set_header(vec![ compression_table.set_header(vec![
Cell::new("Type").add_attribute(Attribute::Bold), Cell::new("Type").add_attribute(Attribute::Bold),
@@ -161,15 +143,7 @@ fn build_compression_table(compression_info: &Vec<CompressionInfo>) -> Table {
} }
fn build_filter_plugin_table(filter_plugins: &Vec<crate::common::status::FilterPluginInfo>) -> Table { fn build_filter_plugin_table(filter_plugins: &Vec<crate::common::status::FilterPluginInfo>) -> Table {
let mut filter_plugin_table = Table::new(); let mut filter_plugin_table = crate::modes::common::create_table(true);
filter_plugin_table
.load_preset(UTF8_FULL)
.apply_modifier(UTF8_ROUND_CORNERS)
.set_content_arrangement(ContentArrangement::Dynamic);
if !std::io::stdout().is_terminal() {
filter_plugin_table.force_no_tty();
}
filter_plugin_table.set_header(vec![ filter_plugin_table.set_header(vec![
Cell::new("Plugin Name").add_attribute(Attribute::Bold), Cell::new("Plugin Name").add_attribute(Attribute::Bold),