From 2cd6e36546fd6521a51a6bb8483c9e890d14ef98 Mon Sep 17 00:00:00 2001 From: Xithrius Date: Fri, 19 Jul 2024 01:57:11 -0700 Subject: [PATCH] Implemented NO_COLOR environment variable standard All styles are now statics [pre-commit.ci] auto fixes from pre-commit.com hooks Appeased the linter, removed the vscode crate extension --- .vscode/extensions.json | 5 +- src/emotes/downloader.rs | 2 +- src/handlers/app.rs | 6 +- src/handlers/data.rs | 16 +-- src/ui/components/channel_switcher.rs | 14 +-- src/ui/components/chat.rs | 30 ++--- src/ui/components/dashboard.rs | 13 +-- src/ui/components/debug.rs | 22 ++-- src/ui/components/emote_picker.rs | 18 ++- src/ui/components/error.rs | 13 ++- src/ui/components/help.rs | 9 +- src/ui/components/state_tabs.rs | 4 +- src/ui/components/utils/input_widget.rs | 25 ++-- src/ui/components/utils/search_widget.rs | 22 ++-- src/utils/styles.rs | 140 +++++++++++++++++------ 15 files changed, 210 insertions(+), 129 deletions(-) diff --git a/.vscode/extensions.json b/.vscode/extensions.json index d55911dd..74c151f8 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,6 +1,3 @@ { - "recommendations": [ - "serayuzgur.crates", - "rust-lang.rust-analyzer" - ], + "recommendations": ["rust-lang.rust-analyzer"] } diff --git a/src/emotes/downloader.rs b/src/emotes/downloader.rs index dcfdc4e9..6b470514 100644 --- a/src/emotes/downloader.rs +++ b/src/emotes/downloader.rs @@ -475,7 +475,7 @@ pub async fn get_twitch_emote(name: &str) -> Result<()> { let res = if res.is_err() { client - .get(&url.replace("animated", "static")) + .get(url.replace("animated", "static")) .send() .await? .error_for_status() diff --git a/src/handlers/app.rs b/src/handlers/app.rs index 2ce2e58d..fd83361d 100644 --- a/src/handlers/app.rs +++ b/src/handlers/app.rs @@ -178,7 +178,7 @@ impl App { self.components.chat.scroll_offset.jump_to(0); } - pub fn purge_user_messages(&mut self, user_id: &str) { + pub fn purge_user_messages(&self, user_id: &str) { let messages = self .messages .borrow_mut() @@ -190,7 +190,7 @@ impl App { self.messages.replace(messages); } - pub fn remove_message_with(&mut self, message_id: &str) { + pub fn remove_message_with(&self, message_id: &str) { let index = self .messages .borrow_mut() @@ -217,7 +217,7 @@ impl App { } #[allow(dead_code)] - pub fn rotate_theme(&mut self) { + pub fn rotate_theme(&self) { todo!("Rotate through different themes") } } diff --git a/src/handlers/data.rs b/src/handlers/data.rs index af74c4be..5df06dd8 100644 --- a/src/handlers/data.rs +++ b/src/handlers/data.rs @@ -21,7 +21,8 @@ use crate::{ ZERO_WIDTH_SPACE_STR, }, styles::{ - DATETIME_DARK, DATETIME_LIGHT, HIGHLIGHT_NAME_DARK, HIGHLIGHT_NAME_LIGHT, SYSTEM_CHAT, + DATETIME_DARK_STYLE, DATETIME_LIGHT_STYLE, HIGHLIGHT_NAME_DARK_STYLE, + HIGHLIGHT_NAME_LIGHT_STYLE, SEARCH_STYLE, SYSTEM_CHAT_STYLE, }, text::split_cow_in_place, }, @@ -454,19 +455,18 @@ impl MessageData { Style::default() }; let username_theme = match frontend_config.theme { - Theme::Dark => HIGHLIGHT_NAME_DARK, - _ => HIGHLIGHT_NAME_LIGHT, + Theme::Dark => *HIGHLIGHT_NAME_DARK_STYLE, + _ => *HIGHLIGHT_NAME_LIGHT_STYLE, }; let author_theme = if self.system { - SYSTEM_CHAT + *SYSTEM_CHAT_STYLE } else { Style::default().fg(fg) }; let datetime_theme = match frontend_config.theme { - Theme::Dark => DATETIME_DARK, - _ => DATETIME_LIGHT, + Theme::Dark => *DATETIME_DARK_STYLE, + _ => *DATETIME_LIGHT_STYLE, }; - let search_theme = Style::default().fg(Color::Red).add_modifier(Modifier::BOLD); // All indices to highlight like a user let username_highlight = username_highlight @@ -491,7 +491,7 @@ impl MessageData { }) .unwrap_or_default(); - let search = (&search_highlight as &[usize], search_theme); + let search = (&search_highlight as &[usize], *SEARCH_STYLE); let username = (&username_highlight as &[usize], username_theme); // Message prefix diff --git a/src/ui/components/channel_switcher.rs b/src/ui/components/channel_switcher.rs index 852ba481..80a59558 100644 --- a/src/ui/components/channel_switcher.rs +++ b/src/ui/components/channel_switcher.rs @@ -28,7 +28,10 @@ use crate::{ components::{utils::InputWidget, Component}, statics::{NAME_MAX_CHARACTERS, NAME_RESTRICTION_REGEX}, }, - utils::text::{first_similarity, title_line, TitleStyle}, + utils::{ + styles::TITLE_STYLE, + text::{first_similarity, title_line, TitleStyle}, + }, }; use super::utils::centered_rect; @@ -175,14 +178,12 @@ impl Component for ChannelSwitcherWidget { continue; } - let search_theme = Style::default().fg(Color::Red).add_modifier(Modifier::BOLD); - let line = channel .chars() .enumerate() .map(|(i, c)| { if matched_indices.contains(&i) { - Span::styled(c.to_string(), search_theme) + Span::styled(c.to_string(), *TITLE_STYLE) } else { Span::raw(c.to_string()) } @@ -201,10 +202,7 @@ impl Component for ChannelSwitcherWidget { let list = List::new(items.clone()) .block( Block::default() - .title(title_line( - &title_binding, - Style::default().fg(Color::Red).add_modifier(Modifier::BOLD), - )) + .title(title_line(&title_binding, *TITLE_STYLE)) .borders(Borders::ALL) .border_type(self.config.borrow().frontend.border_type.clone().into()), ) diff --git a/src/ui/components/chat.rs b/src/ui/components/chat.rs index e8169a5d..14bf1560 100644 --- a/src/ui/components/chat.rs +++ b/src/ui/components/chat.rs @@ -28,7 +28,10 @@ use crate::{ following::FollowingWidget, ChannelSwitcherWidget, ChatInputWidget, Component, MessageSearchWidget, }, - utils::text::{title_line, TitleStyle}, + utils::{ + styles::{NO_COLOR, TEXT_DARK_STYLE, TITLE_STYLE}, + text::{title_line, TitleStyle}, + }, }; pub struct ChatWidget { @@ -206,21 +209,22 @@ impl Component for ChatWidget { } else { "Filter" }, - Style::default().add_modifier(Modifier::BOLD).fg( - if self.filters.borrow().enabled() { - Color::Green - } else { - Color::Red - }, - ), + if *NO_COLOR { + Style::default() + } else { + Style::default().add_modifier(Modifier::BOLD).fg( + if self.filters.borrow().enabled() { + Color::Green + } else { + Color::Red + }, + ) + }, )), ]; let chat_title = if self.config.borrow().frontend.title_shown { - Line::from(title_line( - &spans, - Style::default().fg(Color::Red).add_modifier(Modifier::BOLD), - )) + Line::from(title_line(&spans, *TITLE_STYLE)) } else { Line::default() }; @@ -241,7 +245,7 @@ impl Component for ChatWidget { .title(chat_title), ) } - .style(Style::default().fg(Color::White)); + .style(*TEXT_DARK_STYLE); f.render_widget(list, *first_v_chunk); diff --git a/src/ui/components/dashboard.rs b/src/ui/components/dashboard.rs index 9945d776..b27f4803 100644 --- a/src/ui/components/dashboard.rs +++ b/src/ui/components/dashboard.rs @@ -18,7 +18,7 @@ use crate::{ terminal::TerminalAction, twitch::TwitchAction, ui::components::{ChannelSwitcherWidget, Component}, - utils::styles::DASHBOARD_TITLE_COLOR, + utils::styles::{DASHBOARD_SECTION_STYLE, DASHBOARD_TITLE_COLOR_STYLE, TEXT_DARK_STYLE}, }; use super::following::FollowingWidget; @@ -67,7 +67,7 @@ impl DashboardWidget { Span::raw(s), ])) })) - .style(Style::default().fg(Color::White)) + .style(*TEXT_DARK_STYLE) .highlight_style(Style::default().add_modifier(Modifier::ITALIC)) } @@ -78,7 +78,7 @@ impl DashboardWidget { .map(|&s| Line::from(vec![Span::raw(s)])) .collect::>(), ) - .style(DASHBOARD_TITLE_COLOR); + .style(*DASHBOARD_TITLE_COLOR_STYLE); frame.render_widget(w, *v_chunks.next().unwrap()); } @@ -91,8 +91,7 @@ impl DashboardWidget { default_channels: &[String], ) { frame.render_widget( - Paragraph::new("Currently selected channel") - .style(Style::default().fg(Color::LightRed)), + Paragraph::new("Currently selected channel").style(*DASHBOARD_SECTION_STYLE), *v_chunks.next().unwrap(), ); @@ -109,7 +108,7 @@ impl DashboardWidget { frame.render_widget(current_channel_selection, *v_chunks.next().unwrap()); frame.render_widget( - Paragraph::new("Favorite channels").style(Style::default().fg(Color::LightRed)), + Paragraph::new("Favorite channels").style(*DASHBOARD_SECTION_STYLE), *v_chunks.next().unwrap(), ); @@ -122,7 +121,7 @@ impl DashboardWidget { } frame.render_widget( - Paragraph::new("Most recent channels").style(Style::default().fg(Color::LightRed)), + Paragraph::new("Most recent channels").style(*DASHBOARD_SECTION_STYLE), *v_chunks.next().unwrap(), ); diff --git a/src/ui/components/debug.rs b/src/ui/components/debug.rs index 5fa7bacf..60ef3484 100644 --- a/src/ui/components/debug.rs +++ b/src/ui/components/debug.rs @@ -2,7 +2,6 @@ use chrono::{DateTime, Local}; use tui::{ layout::{Constraint, Rect}, prelude::Alignment, - style::{Color, Modifier, Style}, widgets::{block::Position, Block, Borders, Clear, Row, Table}, Frame, }; @@ -14,7 +13,10 @@ use crate::{ }, terminal::TerminalAction, ui::components::Component, - utils::text::{title_line, TitleStyle}, + utils::{ + styles::{BOLD_STYLE, TITLE_STYLE}, + text::{title_line, TitleStyle}, + }, }; #[derive(Debug, Clone)] @@ -67,12 +69,10 @@ impl Component for DebugWidget { let mut inner_rows = if i > 0 { vec![ Row::new::>(vec![]), - Row::new(vec![t.to_string()]) - .style(Style::default().add_modifier(Modifier::BOLD)), + Row::new(vec![t.to_string()]).style(*BOLD_STYLE), ] } else { - vec![Row::new(vec![t.to_string()]) - .style(Style::default().add_modifier(Modifier::BOLD))] + vec![Row::new(vec![t.to_string()]).style(*BOLD_STYLE)] }; for (k, v) in values { @@ -87,10 +87,7 @@ impl Component for DebugWidget { let table = Table::new(rows, &[Constraint::Length(25), Constraint::Length(25)]).block( Block::default() - .title(title_line( - &title_binding, - Style::default().fg(Color::Red).add_modifier(Modifier::BOLD), - )) + .title(title_line(&title_binding, *TITLE_STYLE)) .borders(Borders::ALL) .border_type(self.config.borrow().frontend.border_type.clone().into()), ); @@ -108,10 +105,7 @@ impl Component for DebugWidget { let bottom_block = Block::default() .borders(Borders::BOTTOM | Borders::LEFT | Borders::RIGHT) .border_type(self.config.borrow().frontend.border_type.clone().into()) - .title(title_line( - &title, - Style::default().add_modifier(Modifier::BOLD).fg(Color::Red), - )) + .title(title_line(&title, *TITLE_STYLE)) .title_position(Position::Bottom) .title_alignment(Alignment::Left); diff --git a/src/ui/components/emote_picker.rs b/src/ui/components/emote_picker.rs index 5de41627..68b4e626 100644 --- a/src/ui/components/emote_picker.rs +++ b/src/ui/components/emote_picker.rs @@ -27,6 +27,7 @@ use crate::{ utils::{ colors::u32_to_color, emotes::UnicodePlaceholder, + styles::{NO_COLOR, SEARCH_STYLE, TITLE_STYLE}, text::{first_similarity_iter, title_line, TitleStyle}, }, }; @@ -68,13 +69,11 @@ impl EmotePickerWidget { Some((emotes.clone(), input_suggester)), ); - let search_theme = Style::default().fg(Color::Red).add_modifier(Modifier::BOLD); - Self { config, emotes, input, - search_theme, + search_theme: *SEARCH_STYLE, list_state: ListState::default(), filtered_emotes: vec![], } @@ -212,18 +211,17 @@ impl Component for EmotePickerWidget { let list = List::new::>(list_items) .block( Block::default() - .title(title_line( - &title_binding, - Style::default().fg(Color::Red).add_modifier(Modifier::BOLD), - )) + .title(title_line(&title_binding, *TITLE_STYLE)) .borders(Borders::ALL) .border_type(self.config.borrow().frontend.border_type.clone().into()), ) - .highlight_style( + .highlight_style(if *NO_COLOR { + Style::default() + } else { Style::default() .bg(Color::LightGreen) - .add_modifier(Modifier::BOLD), - ); + .add_modifier(Modifier::BOLD) + }); f.render_widget(Clear, r); f.render_stateful_widget(list, r, &mut self.list_state); diff --git a/src/ui/components/error.rs b/src/ui/components/error.rs index ddf3716f..94af7ffb 100644 --- a/src/ui/components/error.rs +++ b/src/ui/components/error.rs @@ -6,7 +6,10 @@ use tui::{ widgets::{block::Title, Block, Borders, Clear, Paragraph}, }; -use crate::ui::components::Component; +use crate::{ + ui::components::Component, + utils::styles::{NO_COLOR, TEXT_DARK_STYLE}, +}; #[derive(Debug, Clone)] pub struct ErrorWidget { @@ -44,10 +47,14 @@ impl Component for ErrorWidget { .block( Block::default() .borders(Borders::ALL) - .border_style(Style::default().fg(Color::Red)) + .border_style(if *NO_COLOR { + Style::default() + } else { + Style::default().fg(Color::Red) + }) .title(Title::from("[ ERROR ]").alignment(Alignment::Center)), ) - .style(Style::default().fg(Color::White)) + .style(*TEXT_DARK_STYLE) .alignment(Alignment::Center); f.render_widget(Clear, r); diff --git a/src/ui/components/help.rs b/src/ui/components/help.rs index 327d4ece..175b6dff 100644 --- a/src/ui/components/help.rs +++ b/src/ui/components/help.rs @@ -1,6 +1,5 @@ use tui::{ layout::{Constraint, Rect}, - style::{Modifier, Style}, widgets::{Block, Borders, Cell, Row, Table}, Frame, }; @@ -11,7 +10,7 @@ use crate::{ components::Component, statics::{HELP_COLUMN_TITLES, HELP_KEYBINDS}, }, - utils::styles::COLUMN_TITLE, + utils::styles::{BOLD_STYLE, COLUMN_TITLE_STYLE}, }; // Once a solution is found to calculate constraints, this will be removed. @@ -40,10 +39,10 @@ impl Component for HelpWidget { rows.push(Row::new(vec![ if i == 0 { Cell::from((*s).to_string()) - .style(Style::default().add_modifier(Modifier::BOLD)) } else { Cell::from("") - }, + } + .style(*BOLD_STYLE), Cell::from((*key).to_string()), Cell::from((*desc).to_string()), ])); @@ -53,7 +52,7 @@ impl Component for HelpWidget { } let help_table = Table::new(rows, TABLE_CONSTRAINTS) - .header(Row::new(HELP_COLUMN_TITLES.iter().copied()).style(COLUMN_TITLE)) + .header(Row::new(HELP_COLUMN_TITLES.iter().copied()).style(*COLUMN_TITLE_STYLE)) .block( Block::default() .borders(Borders::ALL) diff --git a/src/ui/components/state_tabs.rs b/src/ui/components/state_tabs.rs index 731daab2..53011be9 100644 --- a/src/ui/components/state_tabs.rs +++ b/src/ui/components/state_tabs.rs @@ -9,7 +9,7 @@ use tui::{ use crate::{ handlers::{config::SharedCompleteConfig, state::State}, - utils::text::capitalize_first_char, + utils::{styles::STATE_TABS_STYLE, text::capitalize_first_char}, }; const TABS_TO_RENDER: [State; 3] = [State::Dashboard, State::Normal, State::Help]; @@ -32,7 +32,7 @@ impl StateTabsWidget { let tabs = Tabs::new(tab_titles) .block(Block::default()) - .style(Style::default().fg(Color::Gray).add_modifier(Modifier::DIM)) + .style(*STATE_TABS_STYLE) .highlight_style( Style::default() .fg(Color::Yellow) diff --git a/src/ui/components/utils/input_widget.rs b/src/ui/components/utils/input_widget.rs index 1bb54010..6c348131 100644 --- a/src/ui/components/utils/input_widget.rs +++ b/src/ui/components/utils/input_widget.rs @@ -18,7 +18,10 @@ use crate::{ }, terminal::TerminalAction, ui::{components::Component, statics::LINE_BUFFER_CAPACITY}, - utils::text::{get_cursor_position, title_line, TitleStyle}, + utils::{ + styles::NO_COLOR, + text::{get_cursor_position, title_line, TitleStyle}, + }, }; use super::centered_rect; @@ -165,9 +168,13 @@ impl Component for InputWidget { .border_style(Style::default().fg(status_color)) .title(title_line( &binding, - Style::default() - .fg(status_color) - .add_modifier(Modifier::BOLD), + if *NO_COLOR { + Style::default() + } else { + Style::default() + .fg(status_color) + .add_modifier(Modifier::BOLD) + }, )); let paragraph_lines = Line::from(vec![ @@ -201,9 +208,13 @@ impl Component for InputWidget { let bottom_block = Block::default() .title(title_line( &title, - Style::default() - .fg(status_color) - .add_modifier(Modifier::BOLD), + if *NO_COLOR { + Style::default() + } else { + Style::default() + .fg(status_color) + .add_modifier(Modifier::BOLD) + }, )) .title_position(Position::Bottom) .borders(Borders::BOTTOM | Borders::LEFT | Borders::RIGHT) diff --git a/src/ui/components/utils/search_widget.rs b/src/ui/components/utils/search_widget.rs index 8e7d9f9c..bbd0c87d 100644 --- a/src/ui/components/utils/search_widget.rs +++ b/src/ui/components/utils/search_widget.rs @@ -24,7 +24,10 @@ use crate::{ terminal::TerminalAction, twitch::TwitchAction, ui::components::{Component, ErrorWidget}, - utils::text::{title_line, TitleStyle}, + utils::{ + styles::{NO_COLOR, SEARCH_STYLE, TITLE_STYLE}, + text::{title_line, TitleStyle}, + }, }; use super::{centered_rect, InputWidget}; @@ -182,15 +185,13 @@ where continue; } - let search_theme = Style::default().fg(Color::Red).add_modifier(Modifier::BOLD); - let line = item .to_string() .chars() .enumerate() .map(|(i, c)| { if matched_indices.contains(&i) { - Span::styled(c.to_string(), search_theme) + Span::styled(c.to_string(), *SEARCH_STYLE) } else { Span::raw(c.to_string()) } @@ -209,18 +210,17 @@ where let list = List::new(items.clone()) .block( Block::default() - .title(title_line( - &title_binding, - Style::default().fg(Color::Red).add_modifier(Modifier::BOLD), - )) + .title(title_line(&title_binding, *TITLE_STYLE)) .borders(Borders::ALL) .border_type(self.config.borrow().frontend.border_type.clone().into()), ) - .highlight_style( + .highlight_style(if *NO_COLOR { + Style::default() + } else { Style::default() .bg(Color::LightGreen) - .add_modifier(Modifier::BOLD), - ); + .add_modifier(Modifier::BOLD) + }); f.render_widget(Clear, r); f.render_stateful_widget(list, r, &mut self.list_state); diff --git a/src/utils/styles.rs b/src/utils/styles.rs index 909602a7..0d222192 100644 --- a/src/utils/styles.rs +++ b/src/utils/styles.rs @@ -1,74 +1,148 @@ +use once_cell::sync::Lazy; +use std::env; use tui::style::{Color, Modifier, Style}; +pub static NO_COLOR: Lazy = Lazy::new(|| env::var("NO_COLOR").is_ok()); +pub static BOLD: Lazy = Lazy::new(|| { + if *NO_COLOR { + Modifier::empty() + } else { + Modifier::BOLD + } +}); + +macro_rules! color { + ($color:expr) => { + if *NO_COLOR { + None + } else { + Some($color) + } + }; +} + +pub static BOLD_STYLE: Lazy