src/config/commandbindings.rs
254 lines · 10817 bytes
1use crossterm::event::{KeyCode, KeyEvent};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4use typemap::Key;
5
6use crate::global::{
7 structs::{ChannelDisplayPage, ChannelDisplayPageType, MainMenuPage, Page, SingleItemPage},
8 traits::ConfigTrait,
9};
10
11use super::serde::KeyCodeSerde;
12
13#[derive(Clone)]
14pub struct CommandBindings {
15 pub global: HashMap<KeyCode, HashMap<u8, String>>,
16 pub search: HashMap<KeyCode, HashMap<u8, String>>,
17 pub popular: HashMap<KeyCode, HashMap<u8, String>>,
18 pub trending: HashMap<KeyCode, HashMap<u8, String>>,
19 pub video: HashMap<KeyCode, HashMap<u8, String>>,
20 pub playlist: HashMap<KeyCode, HashMap<u8, String>>,
21 pub channel_main: HashMap<KeyCode, HashMap<u8, String>>,
22 pub channel_videos: HashMap<KeyCode, HashMap<u8, String>>,
23 pub channel_playlists: HashMap<KeyCode, HashMap<u8, String>>,
24 pub watchhistory: HashMap<KeyCode, HashMap<u8, String>>,
25 pub subscriptions: HashMap<KeyCode, HashMap<u8, String>>,
26 pub libray: HashMap<KeyCode, HashMap<u8, String>>,
27}
28
29impl Key for CommandBindings {
30 type Value = Self;
31}
32
33impl CommandBindings {
34 pub fn get_command(&self, key: &KeyEvent, page: &Page) -> String {
35 let mut out = String::new();
36
37 if let Some(command) = get_command(key, &self.global) {
38 out.push_str(command);
39 }
40
41 let command = match page {
42 Page::Search(_) => get_command(key, &self.search),
43 Page::MainMenu(MainMenuPage::Trending) => get_command(key, &self.trending),
44 Page::MainMenu(MainMenuPage::Popular) => get_command(key, &self.popular),
45 Page::MainMenu(MainMenuPage::History) => get_command(key, &self.watchhistory),
46 Page::MainMenu(MainMenuPage::Subscriptions) => get_command(key, &self.subscriptions),
47 Page::MainMenu(MainMenuPage::Library) => get_command(key, &self.libray),
48 Page::SingleItem(SingleItemPage::Video(_)) => get_command(key, &self.video),
49 Page::SingleItem(SingleItemPage::Playlist(_)) => get_command(key, &self.playlist),
50 Page::ChannelDisplay(ChannelDisplayPage {
51 r#type: ChannelDisplayPageType::Main,
52 ..
53 }) => get_command(key, &self.channel_main),
54 Page::ChannelDisplay(ChannelDisplayPage {
55 r#type: ChannelDisplayPageType::Videos,
56 ..
57 }) => get_command(key, &self.channel_videos),
58 Page::ChannelDisplay(ChannelDisplayPage {
59 r#type: ChannelDisplayPageType::Playlists,
60 ..
61 }) => get_command(key, &self.channel_playlists),
62 };
63
64 if let Some(command) = command {
65 if !out.is_empty() {
66 out.push_str("&&");
67 }
68 out.push_str(command);
69 }
70
71 out
72 }
73}
74
75#[derive(Clone, Serialize, Deserialize)]
76pub struct CommandBindingsSerde {
77 #[serde(default = "global_default")]
78 pub global: HashMap<KeyCodeSerde, HashMap<u8, String>>,
79 #[serde(default = "search_default")]
80 pub search: HashMap<KeyCodeSerde, HashMap<u8, String>>,
81 #[serde(default = "popular_default")]
82 pub popular: HashMap<KeyCodeSerde, HashMap<u8, String>>,
83 #[serde(default = "trending_default")]
84 pub trending: HashMap<KeyCodeSerde, HashMap<u8, String>>,
85 #[serde(default = "video_default")]
86 pub video: HashMap<KeyCodeSerde, HashMap<u8, String>>,
87 #[serde(default = "playlist_default")]
88 pub playlist: HashMap<KeyCodeSerde, HashMap<u8, String>>,
89 #[serde(default = "channel_main_default")]
90 pub channel_main: HashMap<KeyCodeSerde, HashMap<u8, String>>,
91 #[serde(default = "channel_videos_default")]
92 pub channel_videos: HashMap<KeyCodeSerde, HashMap<u8, String>>,
93 #[serde(default = "channel_playlists_default")]
94 pub channel_playlists: HashMap<KeyCodeSerde, HashMap<u8, String>>,
95 #[serde(default = "watchhistory_default")]
96 pub watchhistory: HashMap<KeyCodeSerde, HashMap<u8, String>>,
97 #[serde(default = "subscriptions_default")]
98 pub subscriptions: HashMap<KeyCodeSerde, HashMap<u8, String>>,
99 #[serde(default = "library_default")]
100 pub library: HashMap<KeyCodeSerde, HashMap<u8, String>>,
101}
102
103impl ConfigTrait for CommandBindingsSerde {
104 const LABEL: &'static str = "commandbindings";
105}
106
107impl CommandBindingsSerde {
108 pub fn into(self) -> Option<CommandBindings> {
109 Some(CommandBindings {
110 global: de_serde(self.global)?,
111 search: de_serde(self.search)?,
112 popular: de_serde(self.popular)?,
113 channel_main: de_serde(self.channel_main)?,
114 channel_videos: de_serde(self.channel_videos)?,
115 channel_playlists: de_serde(self.channel_playlists)?,
116 playlist: de_serde(self.playlist)?,
117 video: de_serde(self.video)?,
118 trending: de_serde(self.trending)?,
119 watchhistory: de_serde(self.watchhistory)?,
120 subscriptions: de_serde(self.subscriptions)?,
121 libray: de_serde(self.library)?,
122 })
123 }
124}
125
126impl Default for CommandBindingsSerde {
127 fn default() -> Self {
128 Self {
129 global: global_default(),
130 search: search_default(),
131 popular: popular_default(),
132 channel_main: channel_main_default(),
133 channel_videos: channel_videos_default(),
134 channel_playlists: channel_playlists_default(),
135 playlist: playlist_default(),
136 video: video_default(),
137 trending: trending_default(),
138 watchhistory: watchhistory_default(),
139 subscriptions: subscriptions_default(),
140 library: library_default(),
141 }
142 }
143}
144
145fn de_serde(
146 original: HashMap<KeyCodeSerde, HashMap<u8, String>>,
147) -> Option<HashMap<KeyCode, HashMap<u8, String>>> {
148 let mut out = HashMap::new();
149
150 // simply loops over the serde hashmap and insert them into the new one
151 for (keycodeserde, map) in original.into_iter() {
152 out.insert(keycodeserde.to_keycode()?, map);
153 }
154
155 Some(out)
156}
157
158fn get_command<'a>(
159 key: &'a KeyEvent,
160 map: &'a HashMap<KeyCode, HashMap<u8, String>>,
161) -> Option<&'a str> {
162 Some(map.get(&key.code)?.get(&key.modifiers.bits())?)
163}
164
165// default functions
166
167fn global_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
168 HashMap::from([
169 (
170 KeyCodeSerde::Char('f'),
171 HashMap::from([(2, String::from("run ${browser} '${url}'"))]),
172 ),
173 (
174 KeyCodeSerde::Char('c'),
175 HashMap::from([(2, String::from("cp ${url}"))]),
176 ),
177 ])
178}
179
180fn search_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
181 HashMap::from([
182 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
183 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
184 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("run mpv '${hover-url}'"))])),
185 ])
186}
187
188fn channel_main_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
189 HashMap::default()
190}
191
192fn channel_playlists_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
193 HashMap::from([
194 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
195 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
196 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("run mpv '${hover-url}'"))])),
197 ])
198}
199
200fn channel_videos_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
201 HashMap::from([
202 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
203 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
204 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("run mpv '${hover-url}'"))])),
205 ])
206}
207
208fn playlist_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
209 HashMap::from([
210 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
211 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
212 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("run mpv '${hover-url}'"))])),
213 ])
214}
215
216fn popular_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
217 HashMap::from([
218 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
219 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
220 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("run mpv '${hover-url}'"))])),
221 ])
222}
223
224fn trending_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
225 HashMap::from([
226 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
227 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
228 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("run mpv '${hover-url}'"))])),
229 ])
230}
231
232fn video_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
233 HashMap::default()
234}
235
236fn watchhistory_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
237 HashMap::from([
238 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
239 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
240 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("run mpv '${hover-url}'"))]))
241 ])
242}
243
244fn subscriptions_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
245 HashMap::default()
246}
247
248fn library_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
249 HashMap::from([
250 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
251 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("run ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
252 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("run mpv '${hover-url}'"))]))
253 ])
254}