src/config/commands.rs

333 lines · 13755 bytes

  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    vec![
102        HashMap::from([(
103            String::from("Reload updated video"),
104            String::from("run rm '~/.cache/youtube-tui/info/${id}.json' ;; video ${id}"),
105        )]),
106        HashMap::from([(
107            String::from("Play video"),
108            String::from("parrun ${video-player} '${embed-url}'"),
109        )]),
110        HashMap::from([(
111            String::from("Play audio"),
112            String::from("mpv stop ;; resume ;; mpv sprop loop-file no ;; mpv loadfile '${embed-url}' ;; echo mpv Player started"),
113        )]),
114        HashMap::from([(
115            String::from("Play audio (loop)"),
116            String::from("mpv stop ;; resume ;; mpv sprop loop-file inf ;; mpv loadfile '${embed-url}' ;; echo mpv Player started"),
117        )]),
118        // HashMap::from([(
119        //     String::from("Add to queue"),
120        //     String::from("mpv loadfile '${embed-url}' appendplay;; echo mpv Added to queue"),
121        // )]),
122        HashMap::from([(
123            String::from("View channel"),
124            String::from("channel ${channel-id}"),
125        )]),
126        HashMap::from([(
127            String::from("Subscribe to channel"),
128            String::from("sync ${channel-id}"),
129        )]),
130        HashMap::from([(
131            String::from("Open in browser"),
132            String::from("parrun ${browser} '${url}'"),
133        )]),
134        HashMap::from([(
135            String::from("Toggle bookmark"),
136            String::from("togglemark ${id}")
137        )]),
138        HashMap::from([(
139            String::from("Save video to library"),
140            String::from("bookmark ${id} ;; run rm -rf '${save-path}${id}.*' ;; parrun ${terminal-emulator} ${youtube-downloader} '${embed-url}' -o '${save-path}%(title)s[%(id)s].%(ext)s'")
141        )]),
142        HashMap::from([(
143            String::from("Save audio to library"),
144            String::from("bookmark ${id} ;; parrun rm -rf '${save-path}${id}.*' ;; parrun ${terminal-emulator} ${youtube-downloader} '${embed-url}' -x -o '${save-path}%(title)s[%(id)s].%(ext)s'")
145        )]),
146        HashMap::from([(
147            String::from("Mode: ${provider}"),
148            String::from("switchprovider"),
149        )]),
150    ]
151}
152
153fn saved_video_default() -> Vec<HashMap<String, String>> {
154    vec![
155        HashMap::from([(
156            String::from("Reload updated video"),
157            String::from("run rm '~/.cache/youtube-tui/info/${id}.json' ;; video ${id}"),
158        )]),
159        HashMap::from([(
160            String::from("[Offline] Play saved file"),
161            String::from("parrun ${video-player} '${offline-path}' --force-window"),
162        )]),
163        HashMap::from([(
164            String::from("[Offline] Play saved file (audio)"),
165            String::from("mpv stop ;; resume ;; mpv sprop loop-file no ;; mpv loadfile '${offline-path}' ;; echo mpv Player started"),
166        )]),
167        HashMap::from([(
168            String::from("[Offline] Play saved file (audio loop)"),
169            String::from("mpv stop ;; resume ;; mpv sprop loop-file inf ;; mpv loadfile '${offline-path}' ;; echo mpv Player started"),
170        )]),
171        // HashMap::from([(
172        //     String::from("[Offline] Add to queue"),
173        //     String::from("mpv loadfile '${offline-path}' appendplay ;; echo mpv Added to queue"),
174        // )]),
175        HashMap::from([(
176            String::from("View channel"),
177            String::from("channel ${channel-id}"),
178        )]),
179        HashMap::from([(
180            String::from("Subscribe to channel"),
181            String::from("sync ${channel-id}"),
182        )]),
183        HashMap::from([(
184            String::from("Open in browser"),
185            String::from("parrun ${browser} '${url}'"),
186        )]),
187        HashMap::from([(
188            String::from("Toggle bookmark"),
189            String::from("togglemark ${id}")
190        )]),
191        HashMap::from([(
192            String::from("Redownload video to library"),
193            String::from("bookmark ${id} ;; run rm ${save-path}*${id}*.* ;; parrun ${terminal-emulator} ${youtube-downloader} ${embed-url} -o '${save-path}%(title)s[%(id)s].%(ext)s'"),
194        )]),
195        HashMap::from([(
196            String::from("Redownload audio to library"),
197            String::from("bookmark ${id} ;; run rm ${save-path}*${id}*.* ;; parrun ${terminal-emulator} ${youtube-downloader} ${embed-url} -x -o '${save-path}%(title)s[%(id)s].%(ext)s'")
198        )]),
199        HashMap::from([(
200            String::from("Delete saved file"),
201            String::from("run rm ${save-path}*${id}*.*")
202        )]),
203    ]
204}
205
206fn playlist_default() -> Vec<HashMap<String, String>> {
207    vec![
208        HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]),
209        HashMap::from([(
210            String::from("Reload updated playlist"),
211            String::from("run rm ~/.cache/youtube-tui/info/${id}.json ;; reload"),
212        )]),
213        HashMap::from([(
214            String::from("Play all (videos)"),
215            String::from("parrun ${video-player} ${url}"),
216        )]),
217        HashMap::from([(
218            String::from("Play all (audio)"),
219            String::from("mpv stop ;; resume ;; ${mpv-queuelist} ;; mpv sprop loop-playlist no ;; mpv playlist-play-index 0 ;; echo mpv Player started"),
220        )]),
221        HashMap::from([(
222            String::from("Shuffle play all (audio loop)"),
223            String::from("mpv stop ;; resume ;; ${mpv-queuelist} ;; mpv sprop loop-playlist yes ;; mpv playlist-shuffle ;; mpv playlist-play-index 0 ;; echo mpv Player started"),
224        )]),
225        // HashMap::from([(
226        //     String::from("Add all to queue"),
227        //     String::from("${mpv-queuelist} ;; echo mpv Queued playlist"),
228        // )]),
229        HashMap::from([(
230            String::from("View channel"),
231            String::from("channel ${channel-id}"),
232        )]),
233        HashMap::from([(
234            String::from("Subscribe to channel"),
235            String::from("sync ${channel-id}"),
236        )]),
237        HashMap::from([(
238            String::from("Open in browser"),
239            String::from("parrun ${browser} '${url}'"),
240        )]),
241        HashMap::from([(
242            String::from("Toggle bookmark"),
243            String::from("togglemark ${id}")
244        )]),
245        HashMap::from([(
246            String::from("Save playlist videos to library"),
247            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'\"'\"")
248        )]),
249        HashMap::from([(
250            String::from("Save playlist audio to library"),
251            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'\"'\"")
252        )]),
253        HashMap::from([(
254            String::from("Mode: ${provider}"),
255            String::from("switchprovider"),
256        )]),
257    ]
258}
259
260fn saved_playlist_default() -> Vec<HashMap<String, String>> {
261    vec![
262        HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]),
263        HashMap::from([(
264            String::from("Reload updated playlist"),
265            String::from("run rm ~/.cache/youtube-tui/info/${id}.json ;; reload"),
266        )]),
267        HashMap::from([(
268            String::from("[Offline] Play all (videos)"),
269            String::from("parrun ${video-player} ${save-path}*${id}*/* --force-window"),
270        )]),
271        HashMap::from([(
272            String::from("[Offline] Play all (audio)"),
273            String::from("mpv stop ;; resume ;; ${offline-queuelist} ;; mpv sprop loop-playlist no ;; mpv playlist-play-index 0 ;; echo mpv Player started"),
274        )]),
275        HashMap::from([(
276            String::from("[Offline] Shuffle play all (audio loop)"),
277            String::from("mpv stop ;; resume ;; ${offline-queuelist} ;; mpv sprop loop-playlist yes ;; mpv playlist-shuffle ;; mpv playlist-play-index 0 ;; echo mpv Player started"),
278        )]),
279        // HashMap::from([(
280        //     String::from("[Offline] Add all to queue"),
281        //     String::from("${offline-queuelist} ;; echo mpv Queued playlist"),
282        // )]),
283        HashMap::from([(
284            String::from("View channel"),
285            String::from("channel ${channel-id}"),
286        )]),
287        HashMap::from([(
288            String::from("Subscribe to channel"),
289            String::from("sync ${channel-id}"),
290        )]),
291        HashMap::from([(
292            String::from("Open in browser"),
293            String::from("parrun ${browser} '${url}'"),
294        )]),
295        HashMap::from([(
296            String::from("Toggle bookmark"),
297            String::from("togglemark ${id}")
298        )]),
299        HashMap::from([(
300            String::from("Redownload playlist videos to library"),
301            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'\"'\"")
302        )]),
303        HashMap::from([(
304            String::from("Redownload playlist audio to library"),
305            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'\"'\"")
306        )]),
307        HashMap::from([(
308            String::from("Delete saved files"),
309            String::from("run rm -rf ${save-path}*${id}*")
310        )]),
311    ]
312}
313
314fn channel_default() -> Vec<HashMap<String, String>> {
315    vec![
316        HashMap::from([(
317            String::from("Subscribe to channel"),
318            String::from("sync ${id}"),
319        )]),
320        HashMap::from([(
321            String::from("Play all (videos)"),
322            String::from("parrun ${video-player} ${url}"),
323        )]),
324        HashMap::from([(
325            String::from("Play all (audio)"),
326            String::from("mpv stop ;; resume ;; mpv loadfile ${url} ;; mpv sprop loop-playlist no ;; mpv playlist-play-index 0 ;; echo mpv Player started"),
327        )]),
328        HashMap::from([(
329            String::from("Shuffle play all (audio loop)"),
330            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"),
331        )]),
332    ]
333}