diff --git a/television/screen/keybindings.rs b/television/screen/keybindings.rs index eb76b9c..3f1ea46 100644 --- a/television/screen/keybindings.rs +++ b/television/screen/keybindings.rs @@ -1,8 +1,9 @@ use rustc_hash::FxHashMap; use std::fmt::Display; -use crate::screen::colors::Colorscheme; +use crate::action::Action; use crate::television::Mode; +use crate::{config::KeyBindings, screen::colors::Colorscheme}; use ratatui::{ layout::Constraint, style::{Color, Style}, @@ -10,6 +11,153 @@ use ratatui::{ widgets::{Cell, Row, Table}, }; +impl KeyBindings { + pub fn to_displayable(&self) -> FxHashMap { + // channel mode keybindings + let channel_bindings: FxHashMap> = + FxHashMap::from_iter(vec![ + ( + DisplayableAction::ResultsNavigation, + serialized_keys_for_actions( + self, + &[ + Action::SelectPrevEntry, + Action::SelectNextEntry, + Action::SelectPrevPage, + Action::SelectNextPage, + ], + ), + ), + ( + DisplayableAction::PreviewNavigation, + serialized_keys_for_actions( + self, + &[ + Action::ScrollPreviewHalfPageUp, + Action::ScrollPreviewHalfPageDown, + ], + ), + ), + ( + DisplayableAction::SelectEntry, + serialized_keys_for_actions( + self, + &[ + Action::ConfirmSelection, + Action::ToggleSelectionDown, + Action::ToggleSelectionUp, + ], + ), + ), + ( + DisplayableAction::CopyEntryToClipboard, + serialized_keys_for_actions( + self, + &[Action::CopyEntryToClipboard], + ), + ), + ( + DisplayableAction::SendToChannel, + serialized_keys_for_actions( + self, + &[Action::ToggleSendToChannel], + ), + ), + ( + DisplayableAction::ToggleRemoteControl, + serialized_keys_for_actions( + self, + &[Action::ToggleRemoteControl], + ), + ), + ( + DisplayableAction::ToggleHelpBar, + serialized_keys_for_actions(self, &[Action::ToggleHelp]), + ), + ]); + + // remote control mode keybindings + let remote_control_bindings: FxHashMap< + DisplayableAction, + Vec, + > = FxHashMap::from_iter(vec![ + ( + DisplayableAction::ResultsNavigation, + serialized_keys_for_actions( + self, + &[Action::SelectPrevEntry, Action::SelectNextEntry], + ), + ), + ( + DisplayableAction::SelectEntry, + serialized_keys_for_actions(self, &[Action::ConfirmSelection]), + ), + ( + DisplayableAction::ToggleRemoteControl, + serialized_keys_for_actions( + self, + &[Action::ToggleRemoteControl], + ), + ), + ]); + + // send to channel mode keybindings + let send_to_channel_bindings: FxHashMap< + DisplayableAction, + Vec, + > = FxHashMap::from_iter(vec![ + ( + DisplayableAction::ResultsNavigation, + serialized_keys_for_actions( + self, + &[Action::SelectPrevEntry, Action::SelectNextEntry], + ), + ), + ( + DisplayableAction::SelectEntry, + serialized_keys_for_actions(self, &[Action::ConfirmSelection]), + ), + ( + DisplayableAction::Cancel, + serialized_keys_for_actions( + self, + &[Action::ToggleSendToChannel], + ), + ), + ]); + + FxHashMap::from_iter(vec![ + (Mode::Channel, DisplayableKeybindings::new(channel_bindings)), + ( + Mode::RemoteControl, + DisplayableKeybindings::new(remote_control_bindings), + ), + ( + Mode::SendToChannel, + DisplayableKeybindings::new(send_to_channel_bindings), + ), + ]) + } +} + +fn serialized_keys_for_actions( + keybindings: &KeyBindings, + actions: &[Action], +) -> Vec { + actions + .iter() + .map(|a| { + keybindings + .get(&Mode::Channel) + .unwrap() + .get(a) + .unwrap() + .clone() + .to_string() + }) + .collect() +} + #[derive(Debug, Clone)] pub struct DisplayableKeybindings { bindings: FxHashMap>, diff --git a/television/television.rs b/television/television.rs index 29e0e48..ac5404f 100644 --- a/television/television.rs +++ b/television/television.rs @@ -5,20 +5,19 @@ use crate::channels::{ remote_control::{load_builtin_channels, RemoteControl}, OnAir, TelevisionChannel, UnitChannel, }; -use crate::config::{Config, KeyBindings, Theme}; +use crate::config::{Config, Theme}; use crate::draw::{ChannelState, Ctx, TvState}; use crate::input::convert_action_to_input_request; use crate::picker::Picker; use crate::preview::{PreviewState, Previewer}; use crate::screen::colors::Colorscheme; -use crate::screen::keybindings::{DisplayableAction, DisplayableKeybindings}; use crate::screen::layout::InputPosition; use crate::screen::spinner::{Spinner, SpinnerState}; use crate::utils::metadata::AppMetadata; use crate::utils::strings::EMPTY_STRING; use anyhow::Result; use copypasta::{ClipboardContext, ClipboardProvider}; -use rustc_hash::{FxBuildHasher, FxHashMap, FxHashSet}; +use rustc_hash::{FxBuildHasher, FxHashSet}; use serde::{Deserialize, Serialize}; use std::collections::HashSet; use tokio::sync::mpsc::{Receiver, Sender, UnboundedSender}; @@ -601,146 +600,3 @@ impl Television { }) } } - -impl KeyBindings { - pub fn to_displayable(&self) -> FxHashMap { - // channel mode keybindings - let channel_bindings: FxHashMap> = - FxHashMap::from_iter(vec![ - ( - DisplayableAction::ResultsNavigation, - serialized_keys_for_actions( - self, - &[ - Action::SelectPrevEntry, - Action::SelectNextEntry, - Action::SelectPrevPage, - Action::SelectNextPage, - ], - ), - ), - ( - DisplayableAction::PreviewNavigation, - serialized_keys_for_actions( - self, - &[ - Action::ScrollPreviewHalfPageUp, - Action::ScrollPreviewHalfPageDown, - ], - ), - ), - ( - DisplayableAction::SelectEntry, - serialized_keys_for_actions( - self, - &[Action::ConfirmSelection], - ), - ), - ( - DisplayableAction::CopyEntryToClipboard, - serialized_keys_for_actions( - self, - &[Action::CopyEntryToClipboard], - ), - ), - ( - DisplayableAction::SendToChannel, - serialized_keys_for_actions( - self, - &[Action::ToggleSendToChannel], - ), - ), - ( - DisplayableAction::ToggleRemoteControl, - serialized_keys_for_actions( - self, - &[Action::ToggleRemoteControl], - ), - ), - ( - DisplayableAction::ToggleHelpBar, - serialized_keys_for_actions(self, &[Action::ToggleHelp]), - ), - ]); - - // remote control mode keybindings - let remote_control_bindings: FxHashMap< - DisplayableAction, - Vec, - > = FxHashMap::from_iter(vec![ - ( - DisplayableAction::ResultsNavigation, - serialized_keys_for_actions( - self, - &[Action::SelectPrevEntry, Action::SelectNextEntry], - ), - ), - ( - DisplayableAction::SelectEntry, - serialized_keys_for_actions(self, &[Action::ConfirmSelection]), - ), - ( - DisplayableAction::ToggleRemoteControl, - serialized_keys_for_actions( - self, - &[Action::ToggleRemoteControl], - ), - ), - ]); - - // send to channel mode keybindings - let send_to_channel_bindings: FxHashMap< - DisplayableAction, - Vec, - > = FxHashMap::from_iter(vec![ - ( - DisplayableAction::ResultsNavigation, - serialized_keys_for_actions( - self, - &[Action::SelectPrevEntry, Action::SelectNextEntry], - ), - ), - ( - DisplayableAction::SelectEntry, - serialized_keys_for_actions(self, &[Action::ConfirmSelection]), - ), - ( - DisplayableAction::Cancel, - serialized_keys_for_actions( - self, - &[Action::ToggleSendToChannel], - ), - ), - ]); - - FxHashMap::from_iter(vec![ - (Mode::Channel, DisplayableKeybindings::new(channel_bindings)), - ( - Mode::RemoteControl, - DisplayableKeybindings::new(remote_control_bindings), - ), - ( - Mode::SendToChannel, - DisplayableKeybindings::new(send_to_channel_bindings), - ), - ]) - } -} - -fn serialized_keys_for_actions( - keybindings: &KeyBindings, - actions: &[Action], -) -> Vec { - actions - .iter() - .map(|a| { - keybindings - .get(&Mode::Channel) - .unwrap() - .get(a) - .unwrap() - .clone() - .to_string() - }) - .collect() -}