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