src/config/commandbindings.rs
299 lines · 12634 bytes
23 symbols in this file
CommandBindingsValueget_commandCommandBindingsSerdeLABELintodefaultde_serdeget_commandQQglobal_defaultsearch_defaultchannel_main_defaultchannel_playlists_defaultchannel_videos_defaultplaylist_defaultpopular_defaulttrending_defaultvideo_defaultwatchhistory_defaultfeed_defaultlibrary_default
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
164/// Returns the appropriate quote character for shell commands on the current platform.
165#[cfg(target_os = "windows")]
166const Q: &str = "\"";
167#[cfg(not(target_os = "windows"))]
168const Q: &str = "'";
169
170fn global_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
171 HashMap::from([
172 (
173 KeyCodeSerde::Char('f'),
174 HashMap::from([(2, format!("run ${{browser}} {Q}${{url}}{Q}"))]),
175 ),
176 (
177 KeyCodeSerde::Char('c'),
178 HashMap::from([(2, String::from("cp ${url}"))]),
179 ),
180 (
181 KeyCodeSerde::Char('y'),
182 HashMap::from([(0, String::from("cp ${url}"))]),
183 ),
184 (
185 KeyCodeSerde::Char(' '),
186 HashMap::from([(0, String::from("tpause ;; echo mpv Player pause toggled"))]),
187 ),
188 (
189 KeyCodeSerde::KeyVariants(super::serde::KeyVariantsSerde::Left),
190 HashMap::from([
191 (
192 2,
193 String::from("mpv seek -5 ;; echo mpv Rewinded 5 seconds"),
194 ),
195 (3, String::from("back ;; echo mpv Unskipped")),
196 ]),
197 ),
198 (
199 KeyCodeSerde::KeyVariants(super::serde::KeyVariantsSerde::Right),
200 HashMap::from([
201 (
202 2,
203 String::from("mpv seek 5 ;; echo mpv Fast forwarded 5 seconds"),
204 ),
205 (3, String::from("next ;; echo mpv Skipped")),
206 ]),
207 ),
208 ])
209}
210
211fn search_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
212 HashMap::from([
213 (KeyCodeSerde::Char('a'), HashMap::from([(2, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video"))])),
214 (KeyCodeSerde::Char('A'), HashMap::from([(1, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video --loop-playlist=inf --shuffle"))])),
215 (KeyCodeSerde::Char('p'), HashMap::from([(2, format!("parrun mpv {Q}${{hover-url}}{Q}"))])),
216 ])
217}
218
219fn channel_main_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
220 HashMap::default()
221}
222
223fn channel_playlists_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
224 HashMap::from([
225 (KeyCodeSerde::Char('a'), HashMap::from([(2, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video"))])),
226 (KeyCodeSerde::Char('A'), HashMap::from([(1, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video --loop-playlist=inf --shuffle"))])),
227 (KeyCodeSerde::Char('p'), HashMap::from([(2, format!("parrun mpv {Q}${{hover-url}}{Q}"))])),
228 ])
229}
230
231fn channel_videos_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
232 HashMap::from([
233 (KeyCodeSerde::Char('a'), HashMap::from([(2, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video"))])),
234 (KeyCodeSerde::Char('A'), HashMap::from([(1, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video --loop-playlist=inf --shuffle"))])),
235 (KeyCodeSerde::Char('p'), HashMap::from([(2, format!("parrun mpv {Q}${{hover-url}}{Q}"))])),
236 ])
237}
238
239fn playlist_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
240 HashMap::from([
241 (KeyCodeSerde::Char('a'), HashMap::from([(2, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video"))])),
242 (KeyCodeSerde::Char('A'), HashMap::from([(1, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video --loop-playlist=inf --shuffle"))])),
243 (KeyCodeSerde::Char('p'), HashMap::from([(2, format!("parrun mpv {Q}${{hover-url}}{Q}"))])),
244 ])
245}
246
247fn popular_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
248 HashMap::from([
249 (KeyCodeSerde::Char('a'), HashMap::from([(2, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video"))])),
250 (KeyCodeSerde::Char('A'), HashMap::from([(1, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video --loop-playlist=inf --shuffle"))])),
251 (KeyCodeSerde::Char('p'), HashMap::from([(2, format!("parrun mpv {Q}${{hover-url}}{Q}"))])),
252 ])
253}
254
255fn trending_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
256 HashMap::from([
257 (KeyCodeSerde::Char('a'), HashMap::from([(2, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video"))])),
258 (KeyCodeSerde::Char('A'), HashMap::from([(1, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video --loop-playlist=inf --shuffle"))])),
259 (KeyCodeSerde::Char('p'), HashMap::from([(2, format!("parrun mpv {Q}${{hover-url}}{Q}"))])),
260 ])
261}
262
263fn video_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
264 HashMap::new()
265}
266
267fn watchhistory_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
268 HashMap::from([
269 (KeyCodeSerde::Char('a'), HashMap::from([(2, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video"))])),
270 (KeyCodeSerde::Char('A'), HashMap::from([(1, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video --loop-playlist=inf --shuffle"))])),
271 (KeyCodeSerde::Char('p'), HashMap::from([(2, format!("parrun mpv {Q}${{hover-url}}{Q}"))])),
272 ])
273}
274
275fn feed_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
276 HashMap::from([
277 (
278 KeyCodeSerde::Char('a'),
279 HashMap::from([(
280 2,
281 format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-video-url}}{Q} --no-video"),
282 )]),
283 ),
284 (KeyCodeSerde::Char('A'), HashMap::from([(1, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-channel-url}}/videos{Q} --no-video --loop-playlist=inf --shuffle"))])),
285 (KeyCodeSerde::Char('P'), HashMap::from([(1, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-channel-url}}/videos{Q} --no-video --loop-playlist=inf --shuffle"))])),
286 (
287 KeyCodeSerde::Char('p'),
288 HashMap::from([(2, format!("parrun mpv {Q}${{hover-video-url}}{Q}"))]),
289 ),
290 ])
291}
292
293fn library_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
294 HashMap::from([
295 (KeyCodeSerde::Char('a'), HashMap::from([(2, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video"))])),
296 (KeyCodeSerde::Char('A'), HashMap::from([(1, format!("parrun ${{terminal-emulator}} mpv {Q}${{hover-url}}{Q} --no-video --loop-playlist=inf --shuffle"))])),
297 (KeyCodeSerde::Char('p'), HashMap::from([(2, format!("parrun mpv {Q}${{hover-url}}{Q}"))])),
298 ])
299}