linting fixes

This commit is contained in:
Alexandre Pasmantier 2024-10-13 23:25:05 +02:00
parent 9b0bce9a67
commit f1625de91d
4 changed files with 96 additions and 94 deletions

View File

@ -1,53 +1,55 @@
/// NOTE: outdated /**
///
/// The general idea The general idea
/// ┌──────────────────────────────────────────────────────────────────────────────────────────────────────┐
/// │ │
/// │ rendering thread event thread main thread │ rendering thread event thread main thread
/// │ │
/// │ │ │ │ │
/// │ │
/// │ │ │ │ │
/// │ │
/// │ │ │ │ │
/// │ ┌───────┴───────┐ │
/// │ │ │ │ │ │
/// │ │ receive event │ │ receive event
/// │ │ │ │ │ │
/// │ └───────┬───────┘ │
/// │ │ │ │ │
/// │ ▼ │
/// │ │ ┌──────────────────┐ ┌──────────┴─────────┐ │
/// │ │ │ │ │ │
/// │ │ │ send on event_rx ├────────────►│ receive event_rx │ │ send on `event_rx` receive `event_rx`
/// │ │ │ │ │ │
/// │ │ └──────────────────┘ └──────────┬─────────┘ │
/// │ │ │
/// │ │ ▼ │
/// │ ┌────────────────────┐ │
/// │ │ │ map to action │ │ map to action
/// │ └──────────┬─────────┘ │
/// │ │ ▼ │
/// │ ┌────────────────────┐ │
/// │ │ │ send on action_tx │ │ send on `action_tx`
/// │ └──────────┬─────────┘ │
/// │ │ │
/// │ │
/// │ │ ┌──────────┴─────────┐ │
/// │ │ receive action_rx │ │ receive `action_rx`
/// │ │ └──────────┬─────────┘ │
/// │ ┌───────────┴────────────┐ ▼ │
/// │ │ │ ┌────────────────────┐ │
/// │ │ receive render_rx │◄────────────────────────────────────────────────┤ dispatch action │ │ receive `render_rx` dispatch action
/// │ │ │ └──────────┬─────────┘ │
/// │ └───────────┬────────────┘ │ │
/// │ │ │ │
/// │ ▼ ▼ │
/// │ ┌────────────────────────┐ ┌────────────────────┐ │
/// │ │ render components │ │ update components │ │ render components update components
/// │ └────────────────────────┘ └────────────────────┘ │
/// │ │
/// └──────────────────────────────────────────────────────────────────────────────────────────────────────┘
*/
use std::sync::Arc; use std::sync::Arc;
use color_eyre::Result; use color_eyre::Result;
@ -211,7 +213,7 @@ impl App {
Event::Resize(x, y) => Action::Resize(x, y), Event::Resize(x, y) => Action::Resize(x, y),
Event::FocusGained => Action::Resume, Event::FocusGained => Action::Resume,
Event::FocusLost => Action::Suspend, Event::FocusLost => Action::Suspend,
_ => Action::NoOp, Event::Closed => Action::NoOp,
} }
} }
@ -243,13 +245,13 @@ impl App {
.get_selected_entry()); .get_selected_entry());
} }
Action::ClearScreen => { Action::ClearScreen => {
self.render_tx.send(RenderingTask::ClearScreen)? self.render_tx.send(RenderingTask::ClearScreen)?;
} }
Action::Resize(w, h) => { Action::Resize(w, h) => {
self.render_tx.send(RenderingTask::Resize(w, h))? self.render_tx.send(RenderingTask::Resize(w, h))?;
} }
Action::Render => { Action::Render => {
self.render_tx.send(RenderingTask::Render)? self.render_tx.send(RenderingTask::Render)?;
} }
_ => {} _ => {}
} }

View File

@ -18,6 +18,7 @@ use crate::{
//const CONFIG: &str = include_str!("../.config/config.json5"); //const CONFIG: &str = include_str!("../.config/config.json5");
const CONFIG: &str = include_str!("../../.config/config.toml"); const CONFIG: &str = include_str!("../../.config/config.toml");
#[allow(dead_code, clippy::module_name_repetitions)]
#[derive(Clone, Debug, Deserialize, Default)] #[derive(Clone, Debug, Deserialize, Default)]
pub struct AppConfig { pub struct AppConfig {
#[serde(default)] #[serde(default)]
@ -28,6 +29,7 @@ pub struct AppConfig {
#[derive(Clone, Debug, Default, Deserialize)] #[derive(Clone, Debug, Default, Deserialize)]
pub struct Config { pub struct Config {
#[allow(clippy::struct_field_names)]
#[serde(default, flatten)] #[serde(default, flatten)]
pub config: AppConfig, pub config: AppConfig,
#[serde(default)] #[serde(default)]
@ -73,7 +75,7 @@ impl Config {
.required(false); .required(false);
builder = builder.add_source(source); builder = builder.add_source(source);
if config_dir.join(file).exists() { if config_dir.join(file).exists() {
found_config = true found_config = true;
} }
} }
if !found_config { if !found_config {
@ -84,13 +86,13 @@ impl Config {
for (mode, default_bindings) in default_config.keybindings.iter() { for (mode, default_bindings) in default_config.keybindings.iter() {
let user_bindings = cfg.keybindings.entry(*mode).or_default(); 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()); user_bindings.entry(*key).or_insert_with(|| cmd.clone());
} }
} }
for (mode, default_styles) in default_config.styles.iter() { for (mode, default_styles) in default_config.styles.iter() {
let user_styles = cfg.styles.entry(*mode).or_default(); 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); user_styles.entry(style_key.clone()).or_insert(*style);
} }
} }
@ -219,8 +221,7 @@ fn parse_key_code_with_modifiers(
"f11" => KeyCode::F(11), "f11" => KeyCode::F(11),
"f12" => KeyCode::F(12), "f12" => KeyCode::F(12),
"space" => KeyCode::Char(' '), "space" => KeyCode::Char(' '),
"hyphen" => KeyCode::Char('-'), "hyphen" | "minus" => KeyCode::Char('-'),
"minus" => KeyCode::Char('-'),
"tab" => KeyCode::Tab, "tab" => KeyCode::Tab,
c if c.len() == 1 => { c if c.len() == 1 => {
let mut c = c.chars().next().unwrap(); let mut c = c.chars().next().unwrap();
@ -234,6 +235,7 @@ fn parse_key_code_with_modifiers(
Ok(KeyEvent::new(c, modifiers)) Ok(KeyEvent::new(c, modifiers))
} }
#[allow(dead_code)]
pub fn key_event_to_string(key_event: &KeyEvent) -> String { pub fn key_event_to_string(key_event: &KeyEvent) -> String {
let char; let char;
let key_code = match key_event.code { let key_code = match key_event.code {
@ -261,16 +263,16 @@ pub fn key_event_to_string(key_event: &KeyEvent) -> String {
&char &char
} }
KeyCode::Esc => "esc", KeyCode::Esc => "esc",
KeyCode::Null => "", KeyCode::Null
KeyCode::CapsLock => "", | KeyCode::CapsLock
KeyCode::Menu => "", | KeyCode::Menu
KeyCode::ScrollLock => "", | KeyCode::ScrollLock
KeyCode::Media(_) => "", | KeyCode::Media(_)
KeyCode::NumLock => "", | KeyCode::NumLock
KeyCode::PrintScreen => "", | KeyCode::PrintScreen
KeyCode::Pause => "", | KeyCode::Pause
KeyCode::KeypadBegin => "", | KeyCode::KeypadBegin
KeyCode::Modifier(_) => "", | KeyCode::Modifier(_) => "",
}; };
let mut modifiers = Vec::with_capacity(3); let mut modifiers = Vec::with_capacity(3);
@ -301,13 +303,13 @@ pub fn parse_key(raw: &str) -> Result<Key, String> {
if raw.chars().filter(|c| *c == '>').count() if raw.chars().filter(|c| *c == '>').count()
!= 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 = if raw.contains("><") {
let raw = raw.strip_prefix('<').unwrap_or(raw);
let raw = raw.strip_suffix('>').unwrap_or(raw);
raw raw
} else { } else {
let raw = raw.strip_prefix('<').unwrap_or(raw);
let raw = raw.strip_suffix('>').unwrap_or(raw);
raw raw
}; };
let key_event = parse_key_event(raw)?; let key_event = parse_key_event(raw)?;
@ -392,6 +394,7 @@ fn process_color_string(color_str: &str) -> (String, Modifier) {
(color, modifiers) (color, modifiers)
} }
#[allow(clippy::cast_possible_truncation)]
fn parse_color(s: &str) -> Option<Color> { fn parse_color(s: &str) -> Option<Color> {
let s = s.trim_start(); let s = s.trim_start();
let s = s.trim_end(); let s = s.trim_end();

View File

@ -8,7 +8,7 @@ use std::{
use crossterm::event::{ use crossterm::event::{
KeyCode::{ KeyCode::{
BackTab, Backspace, Char, Delete, Down, End, Enter, Esc, Home, Insert, 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, KeyEvent, KeyModifiers,
}; };
@ -61,6 +61,7 @@ pub enum Key {
Tab, Tab,
} }
#[allow(clippy::module_name_repetitions)]
pub struct EventLoop { pub struct EventLoop {
pub rx: mpsc::UnboundedReceiver<Event<Key>>, pub rx: mpsc::UnboundedReceiver<Event<Key>>,
//tx: mpsc::UnboundedSender<Event<Key>>, //tx: mpsc::UnboundedSender<Event<Key>>,
@ -100,7 +101,7 @@ async fn poll_event(timeout: Duration) -> bool {
impl EventLoop { impl EventLoop {
pub fn new(tick_rate: f64, init: bool) -> Self { pub fn new(tick_rate: f64, init: bool) -> Self {
let (tx, rx) = mpsc::unbounded_channel(); let (tx, rx) = mpsc::unbounded_channel();
let _tx = tx.clone(); let tx_c = tx.clone();
let tick_interval = let tick_interval =
tokio::time::Duration::from_secs_f64(1.0 / tick_rate); tokio::time::Duration::from_secs_f64(1.0 / tick_rate);
@ -117,32 +118,32 @@ impl EventLoop {
tokio::select! { tokio::select! {
// if we receive a message on the abort channel, stop the event loop // if we receive a message on the abort channel, stop the event loop
_ = abort_recv.recv() => { _ = abort_recv.recv() => {
_tx.send(Event::Closed).unwrap_or_else(|_| warn!("Unable to send Closed event")); tx_c.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::Tick).unwrap_or_else(|_| warn!("Unable to send Tick event"));
break; break;
}, },
// if `delay` completes, pass to the next event "frame" // if `delay` completes, pass to the next event "frame"
_ = delay => { () = delay => {
_tx.send(Event::Tick).unwrap_or_else(|_| warn!("Unable to send Tick event")); tx_c.send(Event::Tick).unwrap_or_else(|_| warn!("Unable to send Tick event"));
}, },
// if the receiver dropped the channel, stop the event loop // if the receiver dropped the channel, stop the event loop
_ = _tx.closed() => break, () = tx_c.closed() => break,
// if an event was received, process it // if an event was received, process it
_ = event_available => { _ = event_available => {
let maybe_event = crossterm::event::read(); let maybe_event = crossterm::event::read();
match maybe_event { match maybe_event {
Ok(crossterm::event::Event::Key(key)) => { Ok(crossterm::event::Event::Key(key)) => {
let key = convert_raw_event_to_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) => { 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) => { 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)) => { 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, BackTab => Key::BackTab,
Insert => Key::Insert, Insert => Key::Insert,
F(k) => Key::F(k), F(k) => Key::F(k),
Null => Key::Null,
Esc => Key::Esc, Esc => Key::Esc,
Char(c) => match event.modifiers { Char(c) => match event.modifiers {
KeyModifiers::NONE | KeyModifiers::SHIFT => Key::Char(c), KeyModifiers::NONE | KeyModifiers::SHIFT => Key::Char(c),

View File

@ -57,6 +57,7 @@ pub struct Television {
const EMPTY_STRING: &str = ""; const EMPTY_STRING: &str = "";
impl Television { impl Television {
#[must_use]
pub fn new(cli_channel: CliTvChannel) -> Self { pub fn new(cli_channel: CliTvChannel) -> Self {
let mut tv_channel = cli_channel.to_channel(); let mut tv_channel = cli_channel.to_channel();
tv_channel.find(EMPTY_STRING); tv_channel.find(EMPTY_STRING);
@ -84,6 +85,9 @@ impl Television {
self.channel.find(pattern); 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<Entry> { pub fn get_selected_entry(&self) -> Option<Entry> {
self.picker_state self.picker_state
.selected() .selected()
@ -240,7 +244,7 @@ impl Television {
Pane::Results | Pane::Input => { Pane::Results | Pane::Input => {
self.current_pane = Pane::Preview; self.current_pane = Pane::Preview;
} }
_ => {} Pane::Preview => {}
} }
} }
@ -259,23 +263,16 @@ impl Television {
} }
} }
#[must_use]
pub fn is_input_focused(&self) -> bool { pub fn is_input_focused(&self) -> bool {
Pane::Input == self.current_pane Pane::Input == self.current_pane
} }
} }
// UI size
const UI_WIDTH_PERCENT: u16 = 95;
const UI_HEIGHT_PERCENT: u16 = 95;
// Misc // Misc
const FOUR_SPACES: &str = " "; const FOUR_SPACES: &str = " ";
// Styles // 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 // input
const DEFAULT_INPUT_FG: Color = Color::Rgb(200, 200, 200); const DEFAULT_INPUT_FG: Color = Color::Rgb(200, 200, 200);
const DEFAULT_RESULTS_COUNT_FG: Color = Color::Rgb(150, 150, 150); const DEFAULT_RESULTS_COUNT_FG: Color = Color::Rgb(150, 150, 150);