src/config/commandbindings.rs
281 lines · 12537 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}
180
181fn search_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
182 HashMap::from([
183 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
184 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
185 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
186 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
187 ])
188}
189
190fn channel_main_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
191 HashMap::default()
192}
193
194fn channel_playlists_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
195 HashMap::from([
196 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
197 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
198 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
199 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
200 ])
201}
202
203fn channel_videos_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
204 HashMap::from([
205 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
206 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
207 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
208 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
209 ])
210}
211
212fn playlist_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
213 HashMap::from([
214 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
215 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
216 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
217 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
218 ])
219}
220
221fn popular_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
222 HashMap::from([
223 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
224 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
225 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
226 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
227 ])
228}
229
230fn trending_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
231 HashMap::from([
232 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
233 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
234 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
235 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
236 ])
237}
238
239fn video_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
240 HashMap::from([(
241 KeyCodeSerde::Char(' '),
242 HashMap::from([(0, String::from("parrun mpv '${embed-url}'"))]),
243 )])
244}
245
246fn watchhistory_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
247 HashMap::from([
248 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
249 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
250 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
251 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
252 ])
253}
254
255fn feed_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
256 HashMap::from([
257 (
258 KeyCodeSerde::Char('a'),
259 HashMap::from([(
260 2,
261 String::from("parrun ${terminal-emulator} mpv '${hover-video-url}' --no-video"),
262 )]),
263 ),
264 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-channel-url}/videos' --no-video --loop-playlist=inf --shuffle"))])),
265 (KeyCodeSerde::Char('P'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-channel-url}/videos' --no-video --loop-playlist=inf --shuffle"))])),
266 (
267 KeyCodeSerde::Char('p'),
268 HashMap::from([(2, String::from("parrun mpv '${hover-video-url}'"))]),
269 ),
270 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
271 ])
272}
273
274fn library_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
275 HashMap::from([
276 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
277 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
278 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
279 (KeyCodeSerde::Char(' '), HashMap::from([(0, String::from("parrun mpv '${hover-url}'"))])),
280 ])
281}