src/config/commandbindings.rs
265 lines · 11404 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}
176
177fn search_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
178 HashMap::from([
179 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
180 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
181 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
182 ])
183}
184
185fn channel_main_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
186 HashMap::default()
187}
188
189fn channel_playlists_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
190 HashMap::from([
191 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
192 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
193 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
194 ])
195}
196
197fn channel_videos_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
198 HashMap::from([
199 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
200 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
201 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
202 ])
203}
204
205fn playlist_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 popular_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
214 HashMap::from([
215 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
216 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
217 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))])),
218 ])
219}
220
221fn trending_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 ])
227}
228
229fn video_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
230 HashMap::default()
231}
232
233fn watchhistory_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 feed_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
242 HashMap::from([
243 (
244 KeyCodeSerde::Char('a'),
245 HashMap::from([(
246 2,
247 String::from("parrun ${terminal-emulator} mpv '${hover-video-url}' --no-video"),
248 )]),
249 ),
250 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-channel-url}/videos' --no-video --loop-playlist=inf --shuffle"))])),
251 (KeyCodeSerde::Char('P'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-channel-url}/videos' --no-video --loop-playlist=inf --shuffle"))])),
252 (
253 KeyCodeSerde::Char('p'),
254 HashMap::from([(2, String::from("parrun mpv '${hover-video-url}'"))]),
255 ),
256 ])
257}
258
259fn library_default() -> HashMap<KeyCodeSerde, HashMap<u8, String>> {
260 HashMap::from([
261 (KeyCodeSerde::Char('a'), HashMap::from([(2, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video"))])),
262 (KeyCodeSerde::Char('A'), HashMap::from([(1, String::from("parrun ${terminal-emulator} mpv '${hover-url}' --no-video --loop-playlist=inf --shuffle"))])),
263 (KeyCodeSerde::Char('p'), HashMap::from([(2, String::from("parrun mpv '${hover-url}'"))]))
264 ])
265}