Strict NO_COLOR compliance and some cleanups

pull/485/head
Arijit Basu 2 years ago committed by Arijit Basu
parent 61d3b1635e
commit 02b631bf83

@ -5,7 +5,6 @@ use crate::config::PanelUiConfig;
use crate::lua; use crate::lua;
use crate::permissions::Permissions; use crate::permissions::Permissions;
use ansi_to_tui_forked::ansi_to_text; use ansi_to_tui_forked::ansi_to_text;
use anyhow::Result;
use indexmap::IndexSet; use indexmap::IndexSet;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use mlua::Lua; 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)] #[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
pub struct LayoutOptions { pub struct LayoutOptions {
@ -648,18 +656,9 @@ fn draw_table<B: Backend>(
.iter() .iter()
.filter_map(|c| { .filter_map(|c| {
c.format.as_ref().map(|f| { c.format.as_ref().map(|f| {
let out: Result<String> = let out = lua::call(lua, f, v.clone())
lua::call(lua, f, v.clone()); .unwrap_or_else(|e| e.to_string());
match out { string_to_text(out)
Ok(o) => ansi_to_text(o.bytes())
.unwrap_or_else(|e| {
Text::raw(format!(
"{:?}",
e
))
}),
Err(e) => Text::raw(e.to_string()),
}
}) })
}) })
.collect::<Vec<Text>>() .collect::<Vec<Text>>()
@ -1054,8 +1053,7 @@ pub fn draw_custom_content<B: Backend>(
match body { match body {
ContentBody::StaticParagraph { render } => { ContentBody::StaticParagraph { render } => {
let render = ansi_to_text(render.bytes()) let render = string_to_text(render);
.unwrap_or_else(|e| Text::raw(e.to_string()));
let content = Paragraph::new(render).block(block( let content = Paragraph::new(render).block(block(
config, config,
title.map(|t| format!(" {} ", t)).unwrap_or_default(), title.map(|t| format!(" {} ", t)).unwrap_or_default(),
@ -1077,8 +1075,7 @@ pub fn draw_custom_content<B: Backend>(
}) })
.unwrap_or_else(|e| e.to_string()); .unwrap_or_else(|e| e.to_string());
let render = ansi_to_text(render.bytes()) let render = string_to_text(render);
.unwrap_or_else(|e| Text::raw(e.to_string()));
let content = Paragraph::new(render).block(block( let content = Paragraph::new(render).block(block(
config, config,
@ -1090,10 +1087,7 @@ pub fn draw_custom_content<B: Backend>(
ContentBody::StaticList { render } => { ContentBody::StaticList { render } => {
let items = render let items = render
.into_iter() .into_iter()
.map(|item| { .map(string_to_text)
ansi_to_text(item.bytes())
.unwrap_or_else(|e| Text::raw(e.to_string()))
})
.map(ListItem::new) .map(ListItem::new)
.collect::<Vec<ListItem>>(); .collect::<Vec<ListItem>>();
@ -1118,10 +1112,7 @@ pub fn draw_custom_content<B: Backend>(
}) })
.unwrap_or_else(|e| vec![e.to_string()]) .unwrap_or_else(|e| vec![e.to_string()])
.into_iter() .into_iter()
.map(|item| { .map(string_to_text)
ansi_to_text(item.bytes())
.unwrap_or_else(|e| Text::raw(e.to_string()))
})
.map(ListItem::new) .map(ListItem::new)
.collect::<Vec<ListItem>>(); .collect::<Vec<ListItem>>();
@ -1142,11 +1133,7 @@ pub fn draw_custom_content<B: Backend>(
.map(|cols| { .map(|cols| {
Row::new( Row::new(
cols.into_iter() cols.into_iter()
.map(|item| { .map(string_to_text)
ansi_to_text(item.bytes()).unwrap_or_else(|e| {
Text::raw(e.to_string())
})
})
.map(Cell::from) .map(Cell::from)
.collect::<Vec<Cell>>(), .collect::<Vec<Cell>>(),
) )
@ -1190,11 +1177,7 @@ pub fn draw_custom_content<B: Backend>(
.map(|cols| { .map(|cols| {
Row::new( Row::new(
cols.into_iter() cols.into_iter()
.map(|item| { .map(string_to_text)
ansi_to_text(item.bytes()).unwrap_or_else(|e| {
Text::raw(e.to_string())
})
})
.map(Cell::from) .map(Cell::from)
.collect::<Vec<Cell>>(), .collect::<Vec<Cell>>(),
) )

Loading…
Cancel
Save