src/config/commands.rs

287 lines · 11497 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("loadpage library ;; flush ;; history clear")
 87}
 88
 89fn video_default() -> Vec<HashMap<String, String>> {
 90    vec![
 91        HashMap::from([(
 92            String::from("Reload updated video"),
 93            String::from("run rm '~/.cache/youtube-tui/info/${id}.json' ;; video ${id}"),
 94        )]),
 95        HashMap::from([(
 96            String::from("Play video"),
 97            String::from("parrun ${video-player} '${embed-url}'"),
 98        )]),
 99        HashMap::from([(
100            String::from("Play audio"),
101            String::from("parrun ${terminal-emulator} ${video-player} '${embed-url}' --no-video"),
102        )]),
103        HashMap::from([(
104            String::from("Play audio (loop)"),
105            String::from(
106                "parrun ${terminal-emulator} ${video-player} '${embed-url}' --no-video --loop-file=inf",
107            ),
108        )]),
109        HashMap::from([(
110            String::from("View channel"),
111            String::from("channel ${channel-id}"),
112        )]),
113        HashMap::from([(
114            String::from("Subscribe to channel"),
115            String::from("sync ${channel-id}"),
116        )]),
117        HashMap::from([(
118            String::from("Open in browser"),
119            String::from("parrun ${browser} '${url}'"),
120        )]),
121        HashMap::from([(
122            String::from("Toggle bookmark"),
123            String::from("togglemark ${id}")
124        )]),
125        HashMap::from([(
126            String::from("Save video to library"),
127            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'")
128        )]),
129        HashMap::from([(
130            String::from("Save audio to library"),
131            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'")
132        )]),
133        HashMap::from([(
134            String::from("Mode: ${provider}"),
135            String::from("switchprovider"),
136        )]),
137    ]
138}
139
140fn saved_video_default() -> Vec<HashMap<String, String>> {
141    vec![
142        HashMap::from([(
143            String::from("Reload updated video"),
144            String::from("run rm '~/.cache/youtube-tui/info/${id}.json' ;; video ${id}"),
145        )]),
146        HashMap::from([(
147            String::from("[Offline] Play saved file"),
148            String::from("parrun ${video-player} ${save-path}*${id}* --force-window"),
149        )]),
150        HashMap::from([(
151            String::from("[Offline] Play saved file (audio)"),
152            String::from("parrun ${terminal-emulator} ${video-player} ${save-path}*${id}* --no-video"),
153        )]),
154        HashMap::from([(
155            String::from("[Offline] Play saved file (audio loop)"),
156            String::from("parrun ${terminal-emulator} ${video-player} ${save-path}*${id}* --no-video --loop"),
157        )]),
158        HashMap::from([(
159            String::from("View channel"),
160            String::from("channel ${channel-id}"),
161        )]),
162        HashMap::from([(
163            String::from("Subscribe to channel"),
164            String::from("sync ${channel-id}"),
165        )]),
166        HashMap::from([(
167            String::from("Open in browser"),
168            String::from("parrun ${browser} '${url}'"),
169        )]),
170        HashMap::from([(
171            String::from("Toggle bookmark"),
172            String::from("togglemark ${id}")
173        )]),
174        HashMap::from([(
175            String::from("Redownload video to library"),
176            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'"),
177        )]),
178        HashMap::from([(
179            String::from("Redownload audio to library"),
180            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'")
181        )]),
182        HashMap::from([(
183            String::from("Delete saved file"),
184            String::from("run rm ${save-path}*${id}*.*")
185        )]),
186    ]
187}
188
189fn playlist_default() -> Vec<HashMap<String, String>> {
190    vec![
191        HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]),
192        HashMap::from([(
193            String::from("Reload updated playlist"),
194            String::from("run rm ~/.cache/youtube-tui/info/${id}.json ;; reload"),
195        )]),
196        HashMap::from([(
197            String::from("Play all (videos)"),
198            String::from("parrun ${video-player} ${all-videos}"),
199        )]),
200        HashMap::from([(
201            String::from("Play all (audio)"),
202            String::from("parrun ${terminal-emulator} ${video-player} ${all-videos} --no-video"),
203        )]),
204        HashMap::from([(
205            String::from("Shuffle play all (audio loop)"),
206            String::from("parrun ${terminal-emulator} ${video-player} ${all-videos} --no-video --shuffle --loop-playlist=inf"),
207        )]),
208        HashMap::from([(
209            String::from("View channel"),
210            String::from("channel ${channel-id}"),
211        )]),
212        HashMap::from([(
213            String::from("Subscribe to channel"),
214            String::from("sync ${channel-id}"),
215        )]),
216        HashMap::from([(
217            String::from("Open in browser"),
218            String::from("parrun ${browser} '${url}'"),
219        )]),
220        HashMap::from([(
221            String::from("Toggle bookmark"),
222            String::from("togglemark ${id}")
223        )]),
224        HashMap::from([(
225            String::from("Save playlist videos to library"),
226            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'\"'\"")
227        )]),
228        HashMap::from([(
229            String::from("Save playlist audio to library"),
230            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'\"'\"")
231        )]),
232        HashMap::from([(
233            String::from("Mode: ${provider}"),
234            String::from("switchprovider"),
235        )]),
236    ]
237}
238
239fn saved_playlist_default() -> Vec<HashMap<String, String>> {
240    vec![
241        HashMap::from([(String::from("Switch view"), String::from("%switch-view%"))]),
242        HashMap::from([(
243            String::from("Reload updated playlist"),
244            String::from("run rm ~/.cache/youtube-tui/info/${id}.json ;; reload"),
245        )]),
246        HashMap::from([(
247            String::from("[Offline] Play all (videos)"),
248            String::from("parrun ${terminal-emulator} bash -c '${video-player} ${save-path}*${id}*/* --force-window'"),
249        )]),
250        HashMap::from([(
251            String::from("[Offline] Play all (audio)"),
252            String::from("parrun ${terminal-emulator} bash -c '${video-player} ${save-path}*${id}*/* --no-video'"),
253        )]),
254        HashMap::from([(
255            String::from("[Offline] Shuffle play all (audio loop)"),
256            String::from("parrun ${terminal-emulator} bash -c '${video-player} ${save-path}*${id}*/* --no-video --loop-playlist=inf --shuffle'"),
257        )]),
258        HashMap::from([(
259            String::from("View channel"),
260            String::from("channel ${channel-id}"),
261        )]),
262        HashMap::from([(
263            String::from("Subscribe to channel"),
264            String::from("sync ${channel-id}"),
265        )]),
266        HashMap::from([(
267            String::from("Open in browser"),
268            String::from("parrun ${browser} '${url}'"),
269        )]),
270        HashMap::from([(
271            String::from("Toggle bookmark"),
272            String::from("togglemark ${id}")
273        )]),
274        HashMap::from([(
275            String::from("Redownload playlist videos to library"),
276            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'\"'\"")
277        )]),
278        HashMap::from([(
279            String::from("Redownload playlist audio to library"),
280            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'\"'\"")
281        )]),
282        HashMap::from([(
283            String::from("Delete saved files"),
284            String::from("run rm -rf ${save-path}*${id}*")
285        )]),
286    ]
287}