diff --git a/src/modes/common.rs b/src/modes/common.rs index 6f99eff..a7b73b0 100644 --- a/src/modes/common.rs +++ b/src/modes/common.rs @@ -3,13 +3,13 @@ use crate::compression_engine::CompressionType; use crate::meta_plugin::MetaPluginType; use clap::Command; use clap::error::ErrorKind; +use comfy_table::{Table, ContentArrangement}; use log::debug; use regex::Regex; use std::collections::HashMap; use std::env; use std::str::FromStr; use strum::IntoEnumIterator; -use termsize; #[derive(Debug, Clone, strum::EnumString, strum::Display, PartialEq)] #[strum(ascii_case_insensitive)] pub enum OutputFormat { @@ -142,3 +142,20 @@ pub fn settings_output_format(settings: &config::Settings) -> OutputFormat { .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 +} + diff --git a/src/modes/info.rs b/src/modes/info.rs index e3a333c..2b8d41e 100644 --- a/src/modes/info.rs +++ b/src/modes/info.rs @@ -10,9 +10,7 @@ use std::path::PathBuf; use crate::services::item_service::ItemService; use chrono::prelude::*; use is_terminal::IsTerminal; -use comfy_table::{Table, ContentArrangement, Cell, Attribute}; -use comfy_table::presets::UTF8_FULL; -use comfy_table::modifiers::UTF8_ROUND_CORNERS; +use comfy_table::{Cell, Attribute}; pub fn mode_info( cmd: &mut Command, @@ -68,15 +66,7 @@ fn show_item( let item_id = item.id.unwrap(); let item_tags: Vec = item_with_meta.tags.iter().map(|t| t.name.clone()).collect(); - let mut table = Table::new(); - 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(); - } + let mut table = crate::modes::common::create_table(true); // Add all the rows table.add_row(vec![ diff --git a/src/modes/list.rs b/src/modes/list.rs index f246880..2d09995 100644 --- a/src/modes/list.rs +++ b/src/modes/list.rs @@ -2,7 +2,7 @@ use crate::config; use crate::services::item_service::ItemService; use crate::services::types::ItemWithMeta; 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 log::debug; use comfy_table::{Table, ContentArrangement, Cell, Row, Color, Attribute}; @@ -55,11 +55,8 @@ pub fn mode_list( let mut table = Table::new(); table .load_preset(NOTHING) - .set_content_arrangement(ContentArrangement::Dynamic); - - if !stdout().is_terminal() { - table.force_no_tty(); - } + .set_content_arrangement(ContentArrangement::Dynamic) + .force_no_tty_if(!stdout().is_terminal()); // Create header row let mut header_cells = Vec::new(); diff --git a/src/modes/status.rs b/src/modes/status.rs index 324ff73..3063549 100644 --- a/src/modes/status.rs +++ b/src/modes/status.rs @@ -9,24 +9,14 @@ use crate::config; use crate::common::status::StatusInfo; use serde_json; use serde_yaml; -use comfy_table::{Table, ContentArrangement, Cell, Attribute}; -use comfy_table::presets::UTF8_FULL; -use comfy_table::modifiers::UTF8_ROUND_CORNERS; +use comfy_table::{Cell, Attribute}; use crate::common::status::PathInfo; use crate::meta_plugin::MetaPluginType; use crate::meta_plugin::get_meta_plugin; fn build_path_table(path_info: &PathInfo) -> Table { - let mut path_table = Table::new(); - 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(); - } + let mut path_table = crate::modes::common::create_table(true); path_table.set_header(vec![ 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 { - let mut config_table = Table::new(); - 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(); - } + let mut config_table = crate::modes::common::create_table(true); config_table.set_header(vec![ Cell::new("Setting").add_attribute(Attribute::Bold), @@ -82,15 +64,7 @@ fn build_meta_plugins_configured_table(status_info: &StatusInfo) -> Option) -> Table { - let mut meta_plugin_table = Table::new(); - 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(); - } + let mut meta_plugin_table = crate::modes::common::create_table(true); meta_plugin_table.set_header(vec![ Cell::new("Plugin Name").add_attribute(Attribute::Bold), @@ -121,15 +111,7 @@ fn build_meta_plugin_table(meta_plugin_info: &std::collections::HashMap) -> Table { - let mut compression_table = Table::new(); - 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(); - } + let mut compression_table = crate::modes::common::create_table(true); compression_table.set_header(vec![ Cell::new("Type").add_attribute(Attribute::Bold), @@ -161,15 +143,7 @@ fn build_compression_table(compression_info: &Vec) -> Table { } fn build_filter_plugin_table(filter_plugins: &Vec) -> Table { - let mut filter_plugin_table = Table::new(); - 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(); - } + let mut filter_plugin_table = crate::modes::common::create_table(true); filter_plugin_table.set_header(vec![ Cell::new("Plugin Name").add_attribute(Attribute::Bold),