From 02b631bf8394906c6f5c36321a2399cb748d7979 Mon Sep 17 00:00:00 2001 From: Arijit Basu Date: Thu, 2 Jun 2022 10:51:34 +0530 Subject: [PATCH] Strict NO_COLOR compliance and some cleanups --- src/ui.rs | 53 ++++++++++++++++++----------------------------------- 1 file changed, 18 insertions(+), 35 deletions(-) diff --git a/src/ui.rs b/src/ui.rs index c508036..3d73cb0 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -5,7 +5,6 @@ use crate::config::PanelUiConfig; use crate::lua; use crate::permissions::Permissions; use ansi_to_tui_forked::ansi_to_text; -use anyhow::Result; use indexmap::IndexSet; use lazy_static::lazy_static; use mlua::Lua; @@ -40,6 +39,15 @@ fn read_only_indicator(app: &app::App) -> &str { } } +fn string_to_text<'a>(string: String) -> Text<'a> { + if *NO_COLOR { + Text::raw(string) + } else { + ansi_to_text(string.bytes()) + .unwrap_or_else(|e| Text::raw(format!("{:?}", e))) + } +} + #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)] #[serde(deny_unknown_fields)] pub struct LayoutOptions { @@ -648,18 +656,9 @@ fn draw_table( .iter() .filter_map(|c| { c.format.as_ref().map(|f| { - let out: Result = - lua::call(lua, f, v.clone()); - match out { - Ok(o) => ansi_to_text(o.bytes()) - .unwrap_or_else(|e| { - Text::raw(format!( - "{:?}", - e - )) - }), - Err(e) => Text::raw(e.to_string()), - } + let out = lua::call(lua, f, v.clone()) + .unwrap_or_else(|e| e.to_string()); + string_to_text(out) }) }) .collect::>() @@ -1054,8 +1053,7 @@ pub fn draw_custom_content( match body { ContentBody::StaticParagraph { render } => { - let render = ansi_to_text(render.bytes()) - .unwrap_or_else(|e| Text::raw(e.to_string())); + let render = string_to_text(render); let content = Paragraph::new(render).block(block( config, title.map(|t| format!(" {} ", t)).unwrap_or_default(), @@ -1077,8 +1075,7 @@ pub fn draw_custom_content( }) .unwrap_or_else(|e| e.to_string()); - let render = ansi_to_text(render.bytes()) - .unwrap_or_else(|e| Text::raw(e.to_string())); + let render = string_to_text(render); let content = Paragraph::new(render).block(block( config, @@ -1090,10 +1087,7 @@ pub fn draw_custom_content( ContentBody::StaticList { render } => { let items = render .into_iter() - .map(|item| { - ansi_to_text(item.bytes()) - .unwrap_or_else(|e| Text::raw(e.to_string())) - }) + .map(string_to_text) .map(ListItem::new) .collect::>(); @@ -1118,10 +1112,7 @@ pub fn draw_custom_content( }) .unwrap_or_else(|e| vec![e.to_string()]) .into_iter() - .map(|item| { - ansi_to_text(item.bytes()) - .unwrap_or_else(|e| Text::raw(e.to_string())) - }) + .map(string_to_text) .map(ListItem::new) .collect::>(); @@ -1142,11 +1133,7 @@ pub fn draw_custom_content( .map(|cols| { Row::new( cols.into_iter() - .map(|item| { - ansi_to_text(item.bytes()).unwrap_or_else(|e| { - Text::raw(e.to_string()) - }) - }) + .map(string_to_text) .map(Cell::from) .collect::>(), ) @@ -1190,11 +1177,7 @@ pub fn draw_custom_content( .map(|cols| { Row::new( cols.into_iter() - .map(|item| { - ansi_to_text(item.bytes()).unwrap_or_else(|e| { - Text::raw(e.to_string()) - }) - }) + .map(string_to_text) .map(Cell::from) .collect::>(), )