src/config/commands.rs

303 lines · 12540 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}
 17
 18impl Key for CommandsConfig {
 19    type Value = Self;
 20}
 21
 22impl From<CommandsConfigSerde> for CommandsConfig {
 23    fn from(original: CommandsConfigSerde) -> Self {
 24        Self {
 25            launch_command: original.launch_command,
 26            video: original
 27                .video
 28                .into_iter()
 29                .map(|hashmap| hashmap.into_iter().last().unwrap())
 30                .collect(),
 31            saved_video: original
 32                .saved_video
 33                .into_iter()
 34                .map(|hashmap| hashmap.into_iter().last().unwrap())
 35                .collect(),
 36            playlist: original
 37                .playlist
 38                .into_iter()
 39                .map(|hashmap| hashmap.into_iter().last().unwrap())
 40                .collect(),
 41            saved_playlist: original
 42                .saved_playlist
 43                .into_iter()
 44                .map(|hashmap| hashmap.into_iter().last().unwrap())
 45                .collect(),
 46        }
 47    }
 48}
 49
 50/// Hashmaps are better formatted in YAML, impls `Into<CommandsConfig>`
 51// uses vector to keep the ordering of the commands, and hashmap to have that key - value pair look
 52#[derive(Serialize, Deserialize, Clone)]
 53pub struct CommandsConfigSerde {
 54    #[serde(default = "launch_command_default")]
 55    pub launch_command: String,
 56    #[serde(default = "video_default")]
 57    pub video: Vec<HashMap<String, String>>,
 58    #[serde(default = "saved_video_default")]
 59    pub saved_video: Vec<HashMap<String, String>>,
 60    #[serde(default = "playlist_default")]
 61    pub playlist: Vec<HashMap<String, String>>,
 62    #[serde(default = "saved_playlist_default")]
 63    pub saved_playlist: Vec<HashMap<String, String>>,
 64}
 65
 66impl ConfigTrait for CommandsConfigSerde {
 67    const LABEL: &'static str = "commands";
 68}
 69
 70impl Default for CommandsConfigSerde {
 71    fn default() -> Self {
 72        Self {
 73            launch_command: launch_command_default(),
 74            video: video_default(),
 75            saved_video: saved_video_default(),
 76            playlist: playlist_default(),
 77            saved_playlist: saved_playlist_default(),
 78        }
 79    }
 80}
 81
 82// `${label}` will be replaced by the values set in `main.yml` in `env`
 83// Different pages may contain different `env`s (for example `url` is different in each page)
 84
 85fn launch_command_default() -> String {
 86    String::from(
 87        "loadpage library ;; flush ;; history clear ;; key Esc 0 ;; key Up 0 ;; key Up 0 ;; key Left 0 ;; key Enter 0",
 88    )
 89}
 90
 91fn video_default() -> Vec<HashMap<String, String>> {
 92    vec![
 93        HashMap::from([(
 94            String::from("Reload updated video"),
 95            String::from("run rm '~/.cache/youtube-tui/info/${id}.json' ;; video ${id}"),
 96        )]),
 97        HashMap::from([(
 98            String::from("Play video"),
 99            String::from("parrun ${video-player} '${embed-url}'"),
100        )]),
101        HashMap::from([(
102            String::from("Play audio"),
103            String::from("mpv stop ;; resume ;; mpv sprop loop-file no ;; mpv loadfile '${embed-url}' ;; echo mpv Player started"),
104        )]),
105        HashMap::from([(
106            String::from("Play audio (loop)"),
107            String::from("mpv stop ;; resume ;; mpv sprop loop-file inf ;; mpv loadfile '${embed-url}' ;; echo mpv Player started"),
108        )]),
109        // HashMap::from([(
110        //     String::from("Add to queue"),
111        //     String::from("mpv loadfile '${embed-url}' appendplay;; echo mpv Added to queue"),
112        // )]),
113        HashMap::from([(
114            String::from("View channel"),
115            String::from("channel ${channel-id}"),
116        )]),
117        HashMap::from([(
118            String::from("Subscribe to channel"),
119            String::from("sync ${channel-id}"),
120        )]),
121        HashMap::from([(
122            String::from("Open in browser"),
123            String::from("parrun ${browser} '${url}'"),
124        )]),
125        HashMap::from([(
126            String::from("Toggle bookmark"),
127            String::from("togglemark ${id}")
128        )]),
129        HashMap::from([(
130            String::from("Save video to library"),
131            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'")
132        )]),
133        HashMap::from([(
134            String::from("Save audio to library"),
135            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'")
136        )]),
137        HashMap::from([(
138            String::from("Mode: ${provider}"),
139            String::from("switchprovider"),
140        )]),
141    ]
142}
143
144fn saved_video_default() -> Vec<HashMap<String, String>> {
145    vec![
146        HashMap::from([(
147            String::from("Reload updated video"),
148            String::from("run rm '~/.cache/youtube-tui/info/${id}.json' ;; video ${id}"),
149        )]),
150        HashMap::from([(
151            String::from("[Offline] Play saved file"),
152            String::from("parrun ${video-player} '${offline-path}' --force-window"),
153        )]),
154        HashMap::from([(
155            String::from("[Offline] Play saved file (audio)"),
156            String::from("mpv stop ;; resume ;; mpv sprop loop-file no ;; mpv loadfile '${offline-path}' ;; echo mpv Player started"),
157        )]),
158        HashMap::from([(
159            String::from("[Offline] Play saved file (audio loop)"),
160            String::from("mpv stop ;; resume ;; mpv sprop loop-file inf ;; mpv loadfile '${offline-path}' ;; echo mpv Player started"),
161        )]),
162        // HashMap::from([(
163        //     String::from("[Offline] Add to queue"),
164        //     String::from("mpv loadfile '${offline-path}' appendplay ;; echo mpv Added to queue"),
165        // )]),
166        HashMap::from([(
167            String::from("View channel"),
168            String::from("channel ${channel-id}"),
169        )]),
170        HashMap::from([(
171            String::from("Subscribe to channel"),
172            String::from("sync ${channel-id}"),
173        )]),
174        HashMap::from([(
175            String::from("Open in browser"),
176            String::from("parrun ${browser} '${url}'"),
177        )]),
178        HashMap::from([(
179            String::from("Toggle bookmark"),
180            String::from("togglemark ${id}")
181        )]),
182        HashMap::from([(
183            String::from("Redownload video to library"),
184            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'"),
185        )]),
186        HashMap::from([(
187            String::from("Redownload audio to library"),
188            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'")
189        )]),
190        HashMap::from([(
191            String::from("Delete saved file"),
192            String::from("run rm ${save-path}*${id}*.*")
193        )]),
194    ]
195}
196
197fn playlist_default() -> Vec<HashMap<String, String>> {
198    vec![
199        HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]),
200        HashMap::from([(
201            String::from("Reload updated playlist"),
202            String::from("run rm ~/.cache/youtube-tui/info/${id}.json ;; reload"),
203        )]),
204        HashMap::from([(
205            String::from("Play all (videos)"),
206            String::from("parrun ${video-player} ${url}"),
207        )]),
208        HashMap::from([(
209            String::from("Play all (audio)"),
210            String::from("mpv stop ;; resume ;; ${mpv-queuelist} ;; mpv sprop loop-playlist no ;; mpv playlist-play-index 0 ;; echo mpv Player started"),
211        )]),
212        HashMap::from([(
213            String::from("Shuffle play all (audio loop)"),
214            String::from("mpv stop ;; resume ;; ${mpv-queuelist} ;; mpv sprop loop-playlist yes ;; mpv playlist-shuffle ;; mpv playlist-play-index 0 ;; echo mpv Player started"),
215        )]),
216        // HashMap::from([(
217        //     String::from("Add all to queue"),
218        //     String::from("${mpv-queuelist} ;; echo mpv Queued playlist"),
219        // )]),
220        HashMap::from([(
221            String::from("View channel"),
222            String::from("channel ${channel-id}"),
223        )]),
224        HashMap::from([(
225            String::from("Subscribe to channel"),
226            String::from("sync ${channel-id}"),
227        )]),
228        HashMap::from([(
229            String::from("Open in browser"),
230            String::from("parrun ${browser} '${url}'"),
231        )]),
232        HashMap::from([(
233            String::from("Toggle bookmark"),
234            String::from("togglemark ${id}")
235        )]),
236        HashMap::from([(
237            String::from("Save playlist videos to library"),
238            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'\"'\"")
239        )]),
240        HashMap::from([(
241            String::from("Save playlist audio to library"),
242            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'\"'\"")
243        )]),
244        HashMap::from([(
245            String::from("Mode: ${provider}"),
246            String::from("switchprovider"),
247        )]),
248    ]
249}
250
251fn saved_playlist_default() -> Vec<HashMap<String, String>> {
252    vec![
253        HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]),
254        HashMap::from([(
255            String::from("Reload updated playlist"),
256            String::from("run rm ~/.cache/youtube-tui/info/${id}.json ;; reload"),
257        )]),
258        HashMap::from([(
259            String::from("[Offline] Play all (videos)"),
260            String::from("parrun ${video-player} ${save-path}*${id}*/* --force-window"),
261        )]),
262        HashMap::from([(
263            String::from("[Offline] Play all (audio)"),
264            String::from("mpv stop ;; resume ;; ${offline-queuelist} ;; mpv sprop loop-playlist no ;; mpv playlist-play-index 0 ;; echo mpv Player started"),
265        )]),
266        HashMap::from([(
267            String::from("[Offline] Shuffle play all (audio loop)"),
268            String::from("mpv stop ;; resume ;; ${offline-queuelist} ;; mpv sprop loop-playlist yes ;; mpv playlist-shuffle ;; mpv playlist-play-index 0 ;; echo mpv Player started"),
269        )]),
270        // HashMap::from([(
271        //     String::from("[Offline] Add all to queue"),
272        //     String::from("${offline-queuelist} ;; echo mpv Queued playlist"),
273        // )]),
274        HashMap::from([(
275            String::from("View channel"),
276            String::from("channel ${channel-id}"),
277        )]),
278        HashMap::from([(
279            String::from("Subscribe to channel"),
280            String::from("sync ${channel-id}"),
281        )]),
282        HashMap::from([(
283            String::from("Open in browser"),
284            String::from("parrun ${browser} '${url}'"),
285        )]),
286        HashMap::from([(
287            String::from("Toggle bookmark"),
288            String::from("togglemark ${id}")
289        )]),
290        HashMap::from([(
291            String::from("Redownload playlist videos to library"),
292            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'\"'\"")
293        )]),
294        HashMap::from([(
295            String::from("Redownload playlist audio to library"),
296            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'\"'\"")
297        )]),
298        HashMap::from([(
299            String::from("Delete saved files"),
300            String::from("run rm -rf ${save-path}*${id}*")
301        )]),
302    ]
303}