diff --git a/crates/television/app.rs b/crates/television/app.rs index cebc454..6bfc65e 100644 --- a/crates/television/app.rs +++ b/crates/television/app.rs @@ -1,53 +1,55 @@ -/// NOTE: outdated -/// -/// The general idea -/// ┌──────────────────────────────────────────────────────────────────────────────────────────────────────┐ -/// │ │ -/// │ rendering thread event thread main thread │ -/// │ │ -/// │ │ │ │ │ -/// │ │ -/// │ │ │ │ │ -/// │ │ -/// │ │ │ │ │ -/// │ ┌───────┴───────┐ │ -/// │ │ │ │ │ │ -/// │ │ receive event │ │ -/// │ │ │ │ │ │ -/// │ └───────┬───────┘ │ -/// │ │ │ │ │ -/// │ ▼ │ -/// │ │ ┌──────────────────┐ ┌──────────┴─────────┐ │ -/// │ │ │ │ │ │ -/// │ │ │ send on event_rx ├────────────►│ receive event_rx │ │ -/// │ │ │ │ │ │ -/// │ │ └──────────────────┘ └──────────┬─────────┘ │ -/// │ │ │ -/// │ │ ▼ │ -/// │ ┌────────────────────┐ │ -/// │ │ │ map to action │ │ -/// │ └──────────┬─────────┘ │ -/// │ │ ▼ │ -/// │ ┌────────────────────┐ │ -/// │ │ │ send on action_tx │ │ -/// │ └──────────┬─────────┘ │ -/// │ │ │ -/// │ │ -/// │ │ ┌──────────┴─────────┐ │ -/// │ │ receive action_rx │ │ -/// │ │ └──────────┬─────────┘ │ -/// │ ┌───────────┴────────────┐ ▼ │ -/// │ │ │ ┌────────────────────┐ │ -/// │ │ receive render_rx │◄────────────────────────────────────────────────┤ dispatch action │ │ -/// │ │ │ └──────────┬─────────┘ │ -/// │ └───────────┬────────────┘ │ │ -/// │ │ │ │ -/// │ ▼ ▼ │ -/// │ ┌────────────────────────┐ ┌────────────────────┐ │ -/// │ │ render components │ │ update components │ │ -/// │ └────────────────────────┘ └────────────────────┘ │ -/// │ │ -/// └──────────────────────────────────────────────────────────────────────────────────────────────────────┘ +/** + + The general idea + ┌──────────────────────────────────────────────────────────────────────────────────────────────────────┐ + │ │ + │ rendering thread event thread main thread │ + │ │ + │ │ │ │ │ + │ │ + │ │ │ │ │ + │ │ + │ │ │ │ │ + │ ┌───────┴───────┐ │ + │ │ │ │ │ │ + │ │ receive event │ │ + │ │ │ │ │ │ + │ └───────┬───────┘ │ + │ │ │ │ │ + │ ▼ │ + │ │ ┌──────────────────┐ ┌──────────┴─────────┐ │ + │ │ │ │ │ │ + │ │ │send on `event_rx`├────────────►│ receive `event_rx` │ │ + │ │ │ │ │ │ + │ │ └──────────────────┘ └──────────┬─────────┘ │ + │ │ │ + │ │ ▼ │ + │ ┌────────────────────┐ │ + │ │ │ map to action │ │ + │ └──────────┬─────────┘ │ + │ │ ▼ │ + │ ┌────────────────────┐ │ + │ │ │ send on `action_tx`│ │ + │ └──────────┬─────────┘ │ + │ │ │ + │ │ + │ │ ┌──────────┴─────────┐ │ + │ │ receive `action_rx`│ │ + │ │ └──────────┬─────────┘ │ + │ ┌───────────┴────────────┐ ▼ │ + │ │ │ ┌────────────────────┐ │ + │ │ receive `render_rx` │◄────────────────────────────────────────────────┤ dispatch action │ │ + │ │ │ └──────────┬─────────┘ │ + │ └───────────┬────────────┘ │ │ + │ │ │ │ + │ ▼ ▼ │ + │ ┌────────────────────────┐ ┌────────────────────┐ │ + │ │ render components │ │ update components │ │ + │ └────────────────────────┘ └────────────────────┘ │ + │ │ + └──────────────────────────────────────────────────────────────────────────────────────────────────────┘ + +*/ use std::sync::Arc; use color_eyre::Result; @@ -211,7 +213,7 @@ impl App { Event::Resize(x, y) => Action::Resize(x, y), Event::FocusGained => Action::Resume, Event::FocusLost => Action::Suspend, - _ => Action::NoOp, + Event::Closed => Action::NoOp, } } @@ -243,13 +245,13 @@ impl App { .get_selected_entry()); } Action::ClearScreen => { - self.render_tx.send(RenderingTask::ClearScreen)? + self.render_tx.send(RenderingTask::ClearScreen)?; } Action::Resize(w, h) => { - self.render_tx.send(RenderingTask::Resize(w, h))? + self.render_tx.send(RenderingTask::Resize(w, h))?; } Action::Render => { - self.render_tx.send(RenderingTask::Render)? + self.render_tx.send(RenderingTask::Render)?; } _ => {} } diff --git a/crates/television/config.rs b/crates/television/config.rs index e227df6..43b1a4a 100644 --- a/crates/television/config.rs +++ b/crates/television/config.rs @@ -18,6 +18,7 @@ use crate::{ //const CONFIG: &str = include_str!("../.config/config.json5"); const CONFIG: &str = include_str!("../../.config/config.toml"); +#[allow(dead_code, clippy::module_name_repetitions)] #[derive(Clone, Debug, Deserialize, Default)] pub struct AppConfig { #[serde(default)] @@ -28,6 +29,7 @@ pub struct AppConfig { #[derive(Clone, Debug, Default, Deserialize)] pub struct Config { + #[allow(clippy::struct_field_names)] #[serde(default, flatten)] pub config: AppConfig, #[serde(default)] @@ -73,7 +75,7 @@ impl Config { .required(false); builder = builder.add_source(source); if config_dir.join(file).exists() { - found_config = true + found_config = true; } } if !found_config { @@ -84,13 +86,13 @@ impl Config { for (mode, default_bindings) in default_config.keybindings.iter() { let user_bindings = cfg.keybindings.entry(*mode).or_default(); - for (key, cmd) in default_bindings.iter() { + for (key, cmd) in default_bindings { user_bindings.entry(*key).or_insert_with(|| cmd.clone()); } } for (mode, default_styles) in default_config.styles.iter() { let user_styles = cfg.styles.entry(*mode).or_default(); - for (style_key, style) in default_styles.iter() { + for (style_key, style) in default_styles { user_styles.entry(style_key.clone()).or_insert(*style); } } @@ -219,8 +221,7 @@ fn parse_key_code_with_modifiers( "f11" => KeyCode::F(11), "f12" => KeyCode::F(12), "space" => KeyCode::Char(' '), - "hyphen" => KeyCode::Char('-'), - "minus" => KeyCode::Char('-'), + "hyphen" | "minus" => KeyCode::Char('-'), "tab" => KeyCode::Tab, c if c.len() == 1 => { let mut c = c.chars().next().unwrap(); @@ -234,6 +235,7 @@ fn parse_key_code_with_modifiers( Ok(KeyEvent::new(c, modifiers)) } +#[allow(dead_code)] pub fn key_event_to_string(key_event: &KeyEvent) -> String { let char; let key_code = match key_event.code { @@ -261,16 +263,16 @@ pub fn key_event_to_string(key_event: &KeyEvent) -> String { &char } KeyCode::Esc => "esc", - KeyCode::Null => "", - KeyCode::CapsLock => "", - KeyCode::Menu => "", - KeyCode::ScrollLock => "", - KeyCode::Media(_) => "", - KeyCode::NumLock => "", - KeyCode::PrintScreen => "", - KeyCode::Pause => "", - KeyCode::KeypadBegin => "", - KeyCode::Modifier(_) => "", + KeyCode::Null + | KeyCode::CapsLock + | KeyCode::Menu + | KeyCode::ScrollLock + | KeyCode::Media(_) + | KeyCode::NumLock + | KeyCode::PrintScreen + | KeyCode::Pause + | KeyCode::KeypadBegin + | KeyCode::Modifier(_) => "", }; let mut modifiers = Vec::with_capacity(3); @@ -301,13 +303,13 @@ pub fn parse_key(raw: &str) -> Result { if raw.chars().filter(|c| *c == '>').count() != raw.chars().filter(|c| *c == '<').count() { - return Err(format!("Unable to parse `{}`", raw)); + return Err(format!("Unable to parse `{raw}`")); } - let raw = if !raw.contains("><") { - let raw = raw.strip_prefix('<').unwrap_or(raw); - let raw = raw.strip_suffix('>').unwrap_or(raw); + let raw = if raw.contains("><") { raw } else { + let raw = raw.strip_prefix('<').unwrap_or(raw); + let raw = raw.strip_suffix('>').unwrap_or(raw); raw }; let key_event = parse_key_event(raw)?; @@ -392,6 +394,7 @@ fn process_color_string(color_str: &str) -> (String, Modifier) { (color, modifiers) } +#[allow(clippy::cast_possible_truncation)] fn parse_color(s: &str) -> Option { let s = s.trim_start(); let s = s.trim_end(); diff --git a/crates/television/event.rs b/crates/television/event.rs index 3d9686e..5fa2010 100644 --- a/crates/television/event.rs +++ b/crates/television/event.rs @@ -8,7 +8,7 @@ use std::{ use crossterm::event::{ KeyCode::{ BackTab, Backspace, Char, Delete, Down, End, Enter, Esc, Home, Insert, - Left, Null, PageDown, PageUp, Right, Tab, Up, F, + Left, PageDown, PageUp, Right, Tab, Up, F, }, KeyEvent, KeyModifiers, }; @@ -61,6 +61,7 @@ pub enum Key { Tab, } +#[allow(clippy::module_name_repetitions)] pub struct EventLoop { pub rx: mpsc::UnboundedReceiver>, //tx: mpsc::UnboundedSender>, @@ -100,7 +101,7 @@ async fn poll_event(timeout: Duration) -> bool { impl EventLoop { pub fn new(tick_rate: f64, init: bool) -> Self { let (tx, rx) = mpsc::unbounded_channel(); - let _tx = tx.clone(); + let tx_c = tx.clone(); let tick_interval = tokio::time::Duration::from_secs_f64(1.0 / tick_rate); @@ -117,32 +118,32 @@ impl EventLoop { tokio::select! { // if we receive a message on the abort channel, stop the event loop _ = abort_recv.recv() => { - _tx.send(Event::Closed).unwrap_or_else(|_| warn!("Unable to send Closed event")); - _tx.send(Event::Tick).unwrap_or_else(|_| warn!("Unable to send Tick event")); + tx_c.send(Event::Closed).unwrap_or_else(|_| warn!("Unable to send Closed event")); + tx_c.send(Event::Tick).unwrap_or_else(|_| warn!("Unable to send Tick event")); break; }, // if `delay` completes, pass to the next event "frame" - _ = delay => { - _tx.send(Event::Tick).unwrap_or_else(|_| warn!("Unable to send Tick event")); + () = delay => { + tx_c.send(Event::Tick).unwrap_or_else(|_| warn!("Unable to send Tick event")); }, // if the receiver dropped the channel, stop the event loop - _ = _tx.closed() => break, + () = tx_c.closed() => break, // if an event was received, process it _ = event_available => { let maybe_event = crossterm::event::read(); match maybe_event { Ok(crossterm::event::Event::Key(key)) => { let key = convert_raw_event_to_key(key); - _tx.send(Event::Input(key)).unwrap_or_else(|_| warn!("Unable to send {:?} event", key)); + tx_c.send(Event::Input(key)).unwrap_or_else(|_| warn!("Unable to send {:?} event", key)); }, Ok(crossterm::event::Event::FocusLost) => { - _tx.send(Event::FocusLost).unwrap_or_else(|_| warn!("Unable to send FocusLost event")); + tx_c.send(Event::FocusLost).unwrap_or_else(|_| warn!("Unable to send FocusLost event")); }, Ok(crossterm::event::Event::FocusGained) => { - _tx.send(Event::FocusGained).unwrap_or_else(|_| warn!("Unable to send FocusGained event")); + tx_c.send(Event::FocusGained).unwrap_or_else(|_| warn!("Unable to send FocusGained event")); }, Ok(crossterm::event::Event::Resize(x, y)) => { - _tx.send(Event::Resize(x, y)).unwrap_or_else(|_| warn!("Unable to send Resize event")); + tx_c.send(Event::Resize(x, y)).unwrap_or_else(|_| warn!("Unable to send Resize event")); }, _ => {} } @@ -201,7 +202,6 @@ pub fn convert_raw_event_to_key(event: KeyEvent) -> Key { BackTab => Key::BackTab, Insert => Key::Insert, F(k) => Key::F(k), - Null => Key::Null, Esc => Key::Esc, Char(c) => match event.modifiers { KeyModifiers::NONE | KeyModifiers::SHIFT => Key::Char(c), diff --git a/crates/television/television.rs b/crates/television/television.rs index 9a87738..07724e1 100644 --- a/crates/television/television.rs +++ b/crates/television/television.rs @@ -57,6 +57,7 @@ pub struct Television { const EMPTY_STRING: &str = ""; impl Television { + #[must_use] pub fn new(cli_channel: CliTvChannel) -> Self { let mut tv_channel = cli_channel.to_channel(); tv_channel.find(EMPTY_STRING); @@ -84,6 +85,9 @@ impl Television { self.channel.find(pattern); } + #[must_use] + /// # Panics + /// This method will panic if the index doesn't fit into a u32. pub fn get_selected_entry(&self) -> Option { self.picker_state .selected() @@ -240,7 +244,7 @@ impl Television { Pane::Results | Pane::Input => { self.current_pane = Pane::Preview; } - _ => {} + Pane::Preview => {} } } @@ -259,23 +263,16 @@ impl Television { } } + #[must_use] pub fn is_input_focused(&self) -> bool { Pane::Input == self.current_pane } } -// UI size -const UI_WIDTH_PERCENT: u16 = 95; -const UI_HEIGHT_PERCENT: u16 = 95; - // Misc const FOUR_SPACES: &str = " "; // Styles -// results -const DEFAULT_RESULT_NAME_FG: Color = Color::Blue; -const DEFAULT_RESULT_PREVIEW_FG: Color = Color::Rgb(150, 150, 150); -const DEFAULT_RESULT_LINE_NUMBER_FG: Color = Color::Yellow; // input const DEFAULT_INPUT_FG: Color = Color::Rgb(200, 200, 200); const DEFAULT_RESULTS_COUNT_FG: Color = Color::Rgb(150, 150, 150);