src/config/main.rs
327 lines · 8956 bytes
1use crate::global::traits::{ConfigTrait, SearchProviderTrait};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4use typemap::Key;
5
6/// `main.yml`, the main config file
7#[derive(Serialize, Deserialize, Clone)]
8pub struct MainConfig {
9 #[serde(default = "mouse_support_default")]
10 pub mouse_support: bool,
11 #[serde(default = "invidious_instance_default")]
12 pub invidious_instance: String,
13 #[serde(default = "write_to_config_default")]
14 pub write_config: WriteConfig,
15 #[serde(default = "allow_unicode_default")]
16 pub allow_unicode: bool,
17 #[serde(default = "message_bar_default_default")]
18 pub message_bar_default: String,
19 #[serde(default = "images_default")]
20 pub images: Images,
21 #[serde(default = "refresh_after_modifying_search_filters_default")]
22 pub refresh_after_modifying_search_filters: bool,
23 #[serde(default = "sync_config_default")]
24 pub syncing: SyncConfig,
25 #[serde(default)]
26 pub limits: Limits,
27 #[serde(default = "textbar_scroll_behaviour_default")]
28 pub textbar_scroll_behaviour: TextbarScrollBehaviour,
29 #[serde(default = "image_index_default")]
30 // The image to download from the array of images provided by the invidious api
31 // 0 is usually `maxres` and 3 (default) is good enough for normal uses without having huge files sizes
32 // Check `https://{any invidious instance}/api/v1/videos/{any valid video id}` property videoThumbnails in Firefox dev tools to see it for yourself
33 pub image_index: usize,
34 #[serde(default = "provider_default")]
35 pub provider: Provider,
36 #[serde(default = "search_provider_default")]
37 pub search_provider: SearchProvider,
38 #[serde(default = "api_key_default")]
39 pub api_key: String,
40 #[serde(default = "shell_default")]
41 pub shell: String,
42 #[serde(default = "legacy_input_handling_default")]
43 pub legacy_input_handling: bool,
44 #[serde(default = "default_env")]
45 pub env: HashMap<String, String>,
46}
47
48impl Key for MainConfig {
49 type Value = Self;
50}
51
52impl Default for MainConfig {
53 fn default() -> Self {
54 Self {
55 write_config: write_to_config_default(),
56 mouse_support: mouse_support_default(),
57 invidious_instance: invidious_instance_default(),
58 allow_unicode: allow_unicode_default(),
59 message_bar_default: message_bar_default_default(),
60 images: images_default(),
61 image_index: image_index_default(),
62 refresh_after_modifying_search_filters: refresh_after_modifying_search_filters_default(
63 ),
64 textbar_scroll_behaviour: textbar_scroll_behaviour_default(),
65 limits: Limits::default(),
66 syncing: sync_config_default(),
67 provider: provider_default(),
68 search_provider: search_provider_default(),
69 api_key: api_key_default(),
70 shell: shell_default(),
71 legacy_input_handling: legacy_input_handling_default(),
72
73 env: default_env(),
74 }
75 }
76}
77
78impl ConfigTrait for MainConfig {
79 const LABEL: &'static str = "main";
80}
81
82#[derive(Clone, Copy, Serialize, Deserialize)]
83pub struct SyncConfig {
84 #[serde(default = "download_images_default")]
85 pub download_images: bool,
86 #[serde(default = "sync_channel_info_default")]
87 pub sync_channel_info: bool,
88 #[serde(default = "sync_channel_cooldown_secs_default")]
89 pub sync_channel_cooldown_secs: u64,
90 #[serde(default = "sync_videos_cooldown_secs_default")]
91 pub sync_videos_cooldown_secs: u64,
92}
93
94const fn sync_config_default() -> SyncConfig {
95 SyncConfig {
96 download_images: download_images_default(),
97 sync_channel_info: sync_channel_info_default(),
98 sync_channel_cooldown_secs: sync_channel_cooldown_secs_default(),
99 sync_videos_cooldown_secs: sync_videos_cooldown_secs_default(),
100 }
101}
102
103#[derive(Clone, Copy, Serialize, Deserialize)]
104pub struct Limits {
105 #[serde(default = "watch_history_default")]
106 pub watch_history: usize,
107 #[serde(default = "search_history_default")]
108 pub search_history: usize,
109 #[serde(default = "commands_history_default")]
110 pub commands_history: usize,
111}
112
113impl Default for Limits {
114 fn default() -> Self {
115 Self {
116 watch_history: watch_history_default(),
117 search_history: search_history_default(),
118 commands_history: commands_history_default(),
119 }
120 }
121}
122
123/// how images are handled/displayed
124#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
125pub enum Images {
126 r#None,
127 HalfBlocks,
128 #[serde(alias = "Sixels")]
129 Sixel,
130}
131
132impl Images {
133 pub fn display(&self) -> bool {
134 !(self == &Self::None)
135 }
136
137 pub fn use_sixels(&self) -> bool {
138 self == &Self::Sixel
139 }
140}
141
142#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
143pub enum Provider {
144 YouTube,
145 Invidious,
146}
147
148#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
149pub enum SearchProvider {
150 #[cfg(feature = "invidious")]
151 Invidious,
152 #[cfg(feature = "rustypipe")]
153 RustyPipe,
154}
155
156impl SearchProvider {
157 pub fn create(&self) -> Box<dyn SearchProviderTrait> {
158 match self {
159 #[cfg(feature = "invidious")]
160 Self::Invidious => Box::new(crate::global::structs::InvidiousClient::new(
161 unsafe { crate::MAIN_CONFIG.get() }
162 .unwrap()
163 .invidious_instance
164 .clone(),
165 )),
166 #[cfg(feature = "rustypipe")]
167 Self::RustyPipe => Box::new(crate::global::structs::RustyPipeWrapper::default()),
168 }
169 }
170}
171
172impl SearchProvider {
173 pub fn as_str(&self) -> &str {
174 match self {
175 #[cfg(feature = "invidious")]
176 Self::Invidious => "Invidious",
177 #[cfg(feature = "rustypipe")]
178 Self::RustyPipe => "RustyPipe",
179 }
180 }
181}
182
183#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
184pub enum WriteConfig {
185 Must,
186 Try,
187 Dont,
188}
189
190impl Provider {
191 pub fn as_str(&self) -> &str {
192 match self {
193 Self::YouTube => "YouTube",
194 Self::Invidious => "Invidious",
195 }
196 }
197
198 pub fn rotate(&mut self) {
199 *self = match self {
200 Self::YouTube => Self::Invidious,
201 Self::Invidious => Self::YouTube,
202 };
203 }
204}
205
206// default functions
207
208fn invidious_instance_default() -> String {
209 String::from("https://invidious.f5.si")
210}
211
212fn message_bar_default_default() -> String {
213 String::from("All good :)")
214}
215
216const fn images_default() -> Images {
217 Images::Sixel
218}
219
220const fn image_index_default() -> usize {
221 4
222}
223
224const fn allow_unicode_default() -> bool {
225 true
226}
227
228const fn refresh_after_modifying_search_filters_default() -> bool {
229 true
230}
231
232const fn provider_default() -> Provider {
233 Provider::YouTube
234}
235
236#[cfg(not(any(feature = "rustypipe", feature = "invidious")))]
237compile_error!(
238 "You must include a search provider in your feature list, they are `rustypipe` and `invidious`"
239);
240
241#[cfg(feature = "rustypipe")]
242const fn search_provider_default() -> SearchProvider {
243 SearchProvider::RustyPipe
244}
245
246#[cfg(all(feature = "invidious", not(feature = "rustypipe")))]
247const fn search_provider_default() -> SearchProvider {
248 SearchProvider::Invidious
249}
250
251const fn search_history_default() -> usize {
252 75
253}
254
255const fn commands_history_default() -> usize {
256 75
257}
258
259const fn watch_history_default() -> usize {
260 50
261}
262
263const fn mouse_support_default() -> bool {
264 true
265}
266
267const fn write_to_config_default() -> WriteConfig {
268 WriteConfig::Try
269}
270
271fn default_env() -> HashMap<String, String> {
272 HashMap::from([
273 (String::from("video-player"), String::from("mpv")),
274 (String::from("browser"), String::from("xdg-open")),
275 (
276 String::from("terminal-emulator"),
277 String::from("konsole -e"),
278 ),
279 (String::from("youtube-downloader"), String::from("yt-dlp")),
280 (
281 String::from("download-path"),
282 String::from("~/Downloads/%(title)s-%(id)s.%(ext)s"),
283 ),
284 (
285 String::from("save-path"),
286 String::from("~/.local/share/youtube-tui/saved/"),
287 ),
288 ])
289}
290
291fn shell_default() -> String {
292 String::from("sh")
293}
294
295fn api_key_default() -> String {
296 String::from("YOUR API KEY HERE")
297}
298
299const fn download_images_default() -> bool {
300 true
301}
302
303const fn sync_channel_info_default() -> bool {
304 true
305}
306
307const fn sync_channel_cooldown_secs_default() -> u64 {
308 86400
309}
310
311const fn sync_videos_cooldown_secs_default() -> u64 {
312 600
313}
314
315const fn legacy_input_handling_default() -> bool {
316 false
317}
318
319#[derive(Serialize, Deserialize, Clone, Copy)]
320pub enum TextbarScrollBehaviour {
321 Character,
322 Word,
323 History,
324}
325const fn textbar_scroll_behaviour_default() -> TextbarScrollBehaviour {
326 TextbarScrollBehaviour::History
327}