From b3c7744c9aedce2d421360fb8a36b441eac2f93b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Fri, 4 Oct 2024 12:56:20 +0300 Subject: [PATCH] fix(tui): query the terminal background once --- examples/demo.rs | 2 +- src/args.rs | 5 +++++ src/lib.rs | 2 +- src/main.rs | 16 +++++++++++++++- src/tui/state.rs | 14 ++------------ website/src/content/docs/extras/library.md | 2 +- 6 files changed, 25 insertions(+), 16 deletions(-) diff --git a/examples/demo.rs b/examples/demo.rs index cc831b5..7582902 100644 --- a/examples/demo.rs +++ b/examples/demo.rs @@ -19,7 +19,7 @@ fn main() -> Result<()> { file_data.as_slice(), )?; let analyzer = Analyzer::new(file_info, 15, vec![])?; - let mut state = State::new(analyzer)?; + let mut state = State::new(analyzer, None)?; let (sender, receiver) = mpsc::channel(); state.analyzer.extract_strings(sender.clone()); diff --git a/src/args.rs b/src/args.rs index 72d86b9..d06531c 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,4 +1,5 @@ use clap::Parser; +use ratatui::style::Color; use std::path::PathBuf; use crate::tui::ui::Tab; @@ -31,6 +32,10 @@ pub struct Args { /// The initial application tab to open. #[arg(env, short = 't', long = "tab", default_value = "general")] pub tab: Tab, + + /// Accent color of the application. + #[arg(env, long, value_name = "COLOR")] + pub accent_color: Option, } #[cfg(test)] diff --git a/src/lib.rs b/src/lib.rs index cd36569..c488196 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -67,7 +67,7 @@ pub fn run(mut args: Args) -> Result<()> { /// Starts the terminal user interface. pub fn start_tui(analyzer: Analyzer, args: Args) -> Result<()> { // Create an application. - let mut state = State::new(analyzer)?; + let mut state = State::new(analyzer, args.accent_color)?; // Change tab depending on cli arguments. state.set_tab(args.tab); diff --git a/src/main.rs b/src/main.rs index 045a7f4..f9dc1a8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,24 @@ use binsider::args::Args; use binsider::error::Result; use clap::Parser; +use ratatui::style::Color; use std::process; +use std::time::Duration; +use termbg::Theme; fn main() -> Result<()> { - let args = Args::parse(); + let mut args = Args::parse(); + if args.accent_color.is_none() { + args.accent_color = termbg::theme(Duration::from_millis(10)) + .map(|theme| { + if theme == Theme::Dark { + Color::White + } else { + Color::Black + } + }) + .ok(); + } match binsider::run(args) { Ok(_) => process::exit(0), Err(e) => { diff --git a/src/tui/state.rs b/src/tui/state.rs index 8276bc0..98c25c1 100644 --- a/src/tui/state.rs +++ b/src/tui/state.rs @@ -1,6 +1,5 @@ use std::path::PathBuf; use std::sync::mpsc; -use std::time::Duration; use crate::error::{Error, Result}; use crate::prelude::Analyzer; @@ -12,7 +11,6 @@ use crate::tui::widgets::logo::Logo; use ansi_to_tui::IntoText; use heh::windows::Window; use ratatui::style::Color; -use termbg::Theme; use tui_input::backend::crossterm::EventHandler; use tui_input::Input; @@ -59,7 +57,7 @@ pub struct State<'a> { impl<'a> State<'a> { /// Constructs a new instance of [`State`]. - pub fn new(analyzer: Analyzer<'a>) -> Result { + pub fn new(analyzer: Analyzer<'a>, accent_color: Option) -> Result { let mut state = Self { running: true, tab: Tab::default(), @@ -77,15 +75,7 @@ impl<'a> State<'a> { general_scroll_index: 0, notes_scroll_index: 0, headers_scroll_index: 0, - accent_color: termbg::theme(Duration::from_millis(10)) - .map(|theme| { - if theme == Theme::Dark { - Color::White - } else { - Color::Black - } - }) - .unwrap_or(Color::White), + accent_color: accent_color.unwrap_or(Color::White), logo: Logo::default(), }; state.handle_tab()?; diff --git a/website/src/content/docs/extras/library.md b/website/src/content/docs/extras/library.md index 39bd83d..f91ddea 100644 --- a/website/src/content/docs/extras/library.md +++ b/website/src/content/docs/extras/library.md @@ -21,7 +21,7 @@ Then you can create a `Analyzer` struct and `State` for managing the TUI state: use binsider::prelude::*; let analyzer = Analyzer::new(file_info, 15, vec![])?; -let mut state = State::new(analyzer)?; +let mut state = State::new(analyzer, None)?; ``` To render a frame: