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