src/config/commands.rs

376 lines · 15656 bytes

12 symbols in this file
  1use std::collections::HashMap;
  2
  3use serde::{Deserialize, Serialize};
  4use typemap::Key;
  5
  6use crate::global::traits::ConfigTrait;
  7
  8/// Stores combinations of label and commands
  9#[derive(Clone)]
 10pub struct CommandsConfig {
 11    pub launch_command: String,
 12    pub video: Vec<(String, String)>,
 13    pub saved_video: Vec<(String, String)>,
 14    pub playlist: Vec<(String, String)>,
 15    pub saved_playlist: Vec<(String, String)>,
 16    pub channel: Vec<(String, String)>,
 17}
 18
 19impl Key for CommandsConfig {
 20    type Value = Self;
 21}
 22
 23impl From<CommandsConfigSerde> for CommandsConfig {
 24    fn from(original: CommandsConfigSerde) -> Self {
 25        Self {
 26            launch_command: original.launch_command,
 27            video: original
 28                .video
 29                .into_iter()
 30                .map(|hashmap| hashmap.into_iter().last().unwrap())
 31                .collect(),
 32            saved_video: original
 33                .saved_video
 34                .into_iter()
 35                .map(|hashmap| hashmap.into_iter().last().unwrap())
 36                .collect(),
 37            playlist: original
 38                .playlist
 39                .into_iter()
 40                .map(|hashmap| hashmap.into_iter().last().unwrap())
 41                .collect(),
 42            saved_playlist: original
 43                .saved_playlist
 44                .into_iter()
 45                .map(|hashmap| hashmap.into_iter().last().unwrap())
 46                .collect(),
 47            channel: original
 48                .channel
 49                .into_iter()
 50                .map(|hashmap| hashmap.into_iter().last().unwrap())
 51                .collect(),
 52        }
 53    }
 54}
 55
 56/// Hashmaps are better formatted in YAML, impls `Into<CommandsConfig>`
 57// uses vector to keep the ordering of the commands, and hashmap to have that key - value pair look
 58#[derive(Serialize, Deserialize, Clone)]
 59pub struct CommandsConfigSerde {
 60    #[serde(default = "launch_command_default")]
 61    pub launch_command: String,
 62    #[serde(default = "video_default")]
 63    pub video: Vec<HashMap<String, String>>,
 64    #[serde(default = "saved_video_default")]
 65    pub saved_video: Vec<HashMap<String, String>>,
 66    #[serde(default = "playlist_default")]
 67    pub playlist: Vec<HashMap<String, String>>,
 68    #[serde(default = "saved_playlist_default")]
 69    pub saved_playlist: Vec<HashMap<String, String>>,
 70    #[serde(default = "channel_default")]
 71    pub channel: Vec<HashMap<String, String>>,
 72}
 73
 74impl ConfigTrait for CommandsConfigSerde {
 75    const LABEL: &'static str = "commands";
 76}
 77
 78impl Default for CommandsConfigSerde {
 79    fn default() -> Self {
 80        Self {
 81            launch_command: launch_command_default(),
 82            video: video_default(),
 83            saved_video: saved_video_default(),
 84            playlist: playlist_default(),
 85            saved_playlist: saved_playlist_default(),
 86            channel: channel_default(),
 87        }
 88    }
 89}
 90
 91// `${label}` will be replaced by the values set in `main.yml` in `env`
 92// Different pages may contain different `env`s (for example `url` is different in each page)
 93
 94fn launch_command_default() -> String {
 95    String::from(
 96        "loadpage library ;; flush ;; history clear ;; key Esc 0 ;; key Up 0 ;; key Up 0 ;; key Left 0 ;; key Enter 0",
 97    )
 98}
 99
100fn video_default() -> Vec<HashMap<String, String>> {
101    #[cfg(target_os = "windows")]
102    let q = "\"";
103    #[cfg(not(target_os = "windows"))]
104    let q = "'";
105
106    #[cfg(target_os = "windows")]
107    let rm_cmd = |pattern: &str| format!("run cmd /c del /q /s \"{pattern}\"");
108    #[cfg(not(target_os = "windows"))]
109    let rm_cmd = |pattern: &str| format!("run rm -rf '{pattern}'");
110
111    vec![
112        HashMap::from([(
113            String::from("Reload updated video"),
114            String::from("rmcache ${id} ;; video ${id}"),
115        )]),
116        HashMap::from([(
117            String::from("Play video"),
118            format!("parrun ${{video-player}} {q}${{embed-url}}{q}"),
119        )]),
120        HashMap::from([(
121            String::from("Play audio"),
122            format!("mpv stop ;; resume ;; mpv sprop loop-file no ;; mpv loadfile {q}${{embed-url}}{q} ;; echo mpv Player started"),
123        )]),
124        HashMap::from([(
125            String::from("Play audio (loop)"),
126            format!("mpv stop ;; resume ;; mpv sprop loop-file inf ;; mpv loadfile {q}${{embed-url}}{q} ;; echo mpv Player started"),
127        )]),
128        HashMap::from([(
129            String::from("View channel"),
130            String::from("channel ${channel-id}"),
131        )]),
132        HashMap::from([(
133            String::from("Subscribe to channel"),
134            String::from("sync ${channel-id}"),
135        )]),
136        HashMap::from([(
137            String::from("Open in browser"),
138            format!("parrun ${{browser}} {q}${{url}}{q}"),
139        )]),
140        HashMap::from([(
141            String::from("Toggle bookmark"),
142            String::from("togglemark ${id}")
143        )]),
144        HashMap::from([(
145            String::from("Save video to library"),
146            format!("bookmark ${{id}} ;; {} ;; parrun ${{terminal-emulator}} ${{youtube-downloader}} {q}${{embed-url}}{q} -o {q}${{save-path}}%(title)s[${{id}}].%(ext)s{q}", rm_cmd("${save-path}${id}.*"))
147        )]),
148        HashMap::from([(
149            String::from("Save audio to library"),
150            format!("bookmark ${{id}} ;; {} ;; parrun ${{terminal-emulator}} ${{youtube-downloader}} {q}${{embed-url}}{q} -x -o {q}${{save-path}}%(title)s[${{id}}].%(ext)s{q}", rm_cmd("${save-path}${id}.*"))
151        )]),
152        HashMap::from([(
153            String::from("Mode: ${provider}"),
154            String::from("switchprovider"),
155        )]),
156    ]
157}
158
159fn saved_video_default() -> Vec<HashMap<String, String>> {
160    #[cfg(target_os = "windows")]
161    let q = "\"";
162    #[cfg(not(target_os = "windows"))]
163    let q = "'";
164
165    #[cfg(target_os = "windows")]
166    let rm_cmd = |pattern: &str| format!("run cmd /c del /q /s \"{pattern}\"");
167    #[cfg(not(target_os = "windows"))]
168    let rm_cmd = |pattern: &str| format!("run rm {pattern}");
169
170    vec![
171        HashMap::from([(
172            String::from("Reload updated video"),
173            String::from("rmcache ${id} ;; video ${id}"),
174        )]),
175        HashMap::from([(
176            String::from("[Offline] Play saved file"),
177            format!("parrun ${{video-player}} {q}${{offline-path}}{q} --force-window"),
178        )]),
179        HashMap::from([(
180            String::from("[Offline] Play saved file (audio)"),
181            format!("mpv stop ;; resume ;; mpv sprop loop-file no ;; mpv loadfile {q}${{offline-path}}{q} ;; echo mpv Player started"),
182        )]),
183        HashMap::from([(
184            String::from("[Offline] Play saved file (audio loop)"),
185            format!("mpv stop ;; resume ;; mpv sprop loop-file inf ;; mpv loadfile {q}${{offline-path}}{q} ;; echo mpv Player started"),
186        )]),
187        HashMap::from([(
188            String::from("View channel"),
189            String::from("channel ${channel-id}"),
190        )]),
191        HashMap::from([(
192            String::from("Subscribe to channel"),
193            String::from("sync ${channel-id}"),
194        )]),
195        HashMap::from([(
196            String::from("Open in browser"),
197            format!("parrun ${{browser}} {q}${{url}}{q}"),
198        )]),
199        HashMap::from([(
200            String::from("Toggle bookmark"),
201            String::from("togglemark ${id}")
202        )]),
203        HashMap::from([(
204            String::from("Redownload video to library"),
205            format!("bookmark ${{id}} ;; {} ;; parrun ${{terminal-emulator}} ${{youtube-downloader}} ${{embed-url}} -o {q}${{save-path}}%(title)s[${{id}}].%(ext)s{q}", rm_cmd("${save-path}*${id}*.*")),
206        )]),
207        HashMap::from([(
208            String::from("Redownload audio to library"),
209            format!("bookmark ${{id}} ;; {} ;; parrun ${{terminal-emulator}} ${{youtube-downloader}} ${{embed-url}} -x -o {q}${{save-path}}%(title)s[${{id}}].%(ext)s{q}", rm_cmd("${save-path}*${id}*.*"))
210        )]),
211        HashMap::from([(
212            String::from("Delete saved file"),
213            rm_cmd("${save-path}*${id}*.*")
214        )]),
215    ]
216}
217
218fn playlist_default() -> Vec<HashMap<String, String>> {
219    #[cfg(target_os = "windows")]
220    let q = "\"";
221    #[cfg(not(target_os = "windows"))]
222    let q = "'";
223
224    vec![
225        HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]),
226        HashMap::from([(
227            String::from("Reload updated playlist"),
228            String::from("rmcache ${id} ;; playlist ${id}"),
229        )]),
230        HashMap::from([(
231            String::from("Play all (videos)"),
232            String::from("parrun ${video-player} ${url}"),
233        )]),
234        HashMap::from([(
235            String::from("Play all (audio)"),
236            String::from("mpv stop ;; resume ;; ${mpv-queuelist} ;; mpv sprop loop-playlist no ;; mpv playlist-play-index 0 ;; echo mpv Player started"),
237        )]),
238        HashMap::from([(
239            String::from("Shuffle play all (audio loop)"),
240            String::from("mpv stop ;; resume ;; ${mpv-queuelist} ;; mpv sprop loop-playlist yes ;; mpv playlist-shuffle ;; mpv playlist-play-index 0 ;; echo mpv Player started"),
241        )]),
242        HashMap::from([(
243            String::from("View channel"),
244            String::from("channel ${channel-id}"),
245        )]),
246        HashMap::from([(
247            String::from("Subscribe to channel"),
248            String::from("sync ${channel-id}"),
249        )]),
250        HashMap::from([(
251            String::from("Open in browser"),
252            format!("parrun ${{browser}} {q}${{url}}{q}"),
253        )]),
254        HashMap::from([(
255            String::from("Toggle bookmark"),
256            String::from("togglemark ${id}")
257        )]),
258        HashMap::from([(
259            String::from("Save playlist videos to library"),
260            {
261                #[cfg(target_os = "windows")]
262                { String::from("bookmark ${id} ;; run cmd /c rd /s /q \"${save-path}*${id}*\" ;; parrun ${terminal-emulator} ${youtube-downloader} ${all-videos} -o \"${save-path}${title}[${id}]/%(title)s[%(id)s].%(ext)s\"") }
263                #[cfg(not(target_os = "windows"))]
264                { String::from("bookmark ${id} ;; run rm -rf '${save-path}*${id}*' ;; parrun ${terminal-emulator} bash -c \"${youtube-downloader} ${all-videos} -o '\"'${save-path}${title}[${id}]/%(title)s[%(id)s].%(ext)s'\"'\"") }
265            }
266        )]),
267        HashMap::from([(
268            String::from("Save playlist audio to library"),
269            {
270                #[cfg(target_os = "windows")]
271                { String::from("bookmark ${id} ;; run cmd /c rd /s /q \"${save-path}*${id}*\" ;; parrun ${terminal-emulator} ${youtube-downloader} ${all-videos} -x -o \"${save-path}${title}[${id}]/%(title)s[%(id)s].%(ext)s\"") }
272                #[cfg(not(target_os = "windows"))]
273                { String::from("bookmark ${id} ;; run rm -rf '${save-path}*${id}*' ;; parrun ${terminal-emulator} bash -c \"${youtube-downloader} ${all-videos} -x -o '\"'${save-path}${title}[${id}]/%(title)s[%(id)s].%(ext)s'\"'\"") }
274            }
275        )]),
276        HashMap::from([(
277            String::from("Mode: ${provider}"),
278            String::from("switchprovider"),
279        )]),
280    ]
281}
282
283fn saved_playlist_default() -> Vec<HashMap<String, String>> {
284    #[cfg(target_os = "windows")]
285    let q = "\"";
286    #[cfg(not(target_os = "windows"))]
287    let q = "'";
288
289    vec![
290        HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]),
291        HashMap::from([(
292            String::from("Reload updated playlist"),
293            String::from("rmcache ${id} ;; playlist ${id}"),
294        )]),
295        HashMap::from([(
296            String::from("[Offline] Play all (videos)"),
297            String::from("parrun ${video-player} ${save-path}*${id}*/* --force-window"),
298        )]),
299        HashMap::from([(
300            String::from("[Offline] Play all (audio)"),
301            String::from("mpv stop ;; resume ;; ${offline-queuelist} ;; mpv sprop loop-playlist no ;; mpv playlist-play-index 0 ;; echo mpv Player started"),
302        )]),
303        HashMap::from([(
304            String::from("[Offline] Shuffle play all (audio loop)"),
305            String::from("mpv stop ;; resume ;; ${offline-queuelist} ;; mpv sprop loop-playlist yes ;; mpv playlist-shuffle ;; mpv playlist-play-index 0 ;; echo mpv Player started"),
306        )]),
307        HashMap::from([(
308            String::from("View channel"),
309            String::from("channel ${channel-id}"),
310        )]),
311        HashMap::from([(
312            String::from("Subscribe to channel"),
313            String::from("sync ${channel-id}"),
314        )]),
315        HashMap::from([(
316            String::from("Open in browser"),
317            format!("parrun ${{browser}} {q}${{url}}{q}"),
318        )]),
319        HashMap::from([(
320            String::from("Toggle bookmark"),
321            String::from("togglemark ${id}")
322        )]),
323        HashMap::from([(
324            String::from("Redownload playlist videos to library"),
325            {
326                #[cfg(target_os = "windows")]
327                { String::from("bookmark ${id} ;; run cmd /c rd /s /q \"${save-path}*${id}*\" ;; parrun ${terminal-emulator} ${youtube-downloader} ${all-videos} -o \"${save-path}${title}[${id}]/%(title)s[%(id)s].%(ext)s\"") }
328                #[cfg(not(target_os = "windows"))]
329                { String::from("bookmark ${id} ;; run rm -rf ${save-path}*${id}* ;; parrun ${terminal-emulator} bash -c \"${youtube-downloader} ${all-videos} -o '\"'${save-path}${title}[${id}]/%(title)s[%(id)s].%(ext)s'\"'\"") }
330            }
331        )]),
332        HashMap::from([(
333            String::from("Redownload playlist audio to library"),
334            {
335                #[cfg(target_os = "windows")]
336                { String::from("bookmark ${id} ;; run cmd /c rd /s /q \"${save-path}*${id}*\" ;; parrun ${terminal-emulator} ${youtube-downloader} ${all-videos} -x -o \"${save-path}${title}[${id}]/%(title)s[%(id)s].%(ext)s\"") }
337                #[cfg(not(target_os = "windows"))]
338                { String::from("bookmark ${id} ;; run rm -rf ${save-path}*${id}* ;; parrun ${terminal-emulator} bash -c \"${youtube-downloader} ${all-videos} -x -o '\"'${save-path}${title}[${id}]/%(title)s[%(id)s].%(ext)s'\"'\"") }
339            }
340        )]),
341        HashMap::from([(
342            String::from("Delete saved files"),
343            {
344                #[cfg(target_os = "windows")]
345                { String::from("run cmd /c rd /s /q \"${save-path}*${id}*\"") }
346                #[cfg(not(target_os = "windows"))]
347                { String::from("run rm -rf ${save-path}*${id}*") }
348            }
349        )]),
350    ]
351}
352
353fn channel_default() -> Vec<HashMap<String, String>> {
354    vec![
355        HashMap::from([(
356            String::from("Reload updated channel"),
357            String::from("rmcache ${id} ;; channel ${id}"),
358        )]),
359        HashMap::from([(
360            String::from("Subscribe to channel"),
361            String::from("sync ${id}"),
362        )]),
363        HashMap::from([(
364            String::from("Play all (videos)"),
365            String::from("parrun ${video-player} ${url}"),
366        )]),
367        HashMap::from([(
368            String::from("Play all (audio)"),
369            String::from("mpv stop ;; resume ;; mpv loadfile ${url} ;; mpv sprop loop-playlist no ;; mpv playlist-play-index 0 ;; echo mpv Player started"),
370        )]),
371        HashMap::from([(
372            String::from("Shuffle play all (audio loop)"),
373            String::from("mpv stop ;; resume ;; mpv loadfile ${url} ;; mpv sprop loop-playlist yes ;; mpv playlist-shuffle ;; mpv playlist-play-index 0 ;; echo mpv Player started"),
374        )]),
375    ]
376}