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