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