src/config/main.rs
326 lines · 8928 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 Sixels,
129}
130
131impl Images {
132 pub fn display(&self) -> bool {
133 !(self == &Self::None)
134 }
135
136 pub fn use_sixels(&self) -> bool {
137 self == &Self::Sixels
138 }
139}
140
141#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
142pub enum Provider {
143 YouTube,
144 Invidious,
145}
146
147#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
148pub enum SearchProvider {
149 #[cfg(feature = "invidious")]
150 Invidious,
151 #[cfg(feature = "rustypipe")]
152 RustyPipe,
153}
154
155impl SearchProvider {
156 pub fn create(&self) -> Box<dyn SearchProviderTrait> {
157 match self {
158 #[cfg(feature = "invidious")]
159 Self::Invidious => Box::new(crate::global::structs::InvidiousClient::new(
160 unsafe { crate::MAIN_CONFIG.get() }
161 .unwrap()
162 .invidious_instance
163 .clone(),
164 )),
165 #[cfg(feature = "rustypipe")]
166 Self::RustyPipe => Box::new(crate::global::structs::RustyPipeWrapper::default()),
167 }
168 }
169}
170
171impl SearchProvider {
172 pub fn as_str(&self) -> &str {
173 match self {
174 #[cfg(feature = "invidious")]
175 Self::Invidious => "Invidious",
176 #[cfg(feature = "rustypipe")]
177 Self::RustyPipe => "RustyPipe",
178 }
179 }
180}
181
182#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
183pub enum WriteConfig {
184 Must,
185 Try,
186 Dont,
187}
188
189impl Provider {
190 pub fn as_str(&self) -> &str {
191 match self {
192 Self::YouTube => "YouTube",
193 Self::Invidious => "Invidious",
194 }
195 }
196
197 pub fn rotate(&mut self) {
198 *self = match self {
199 Self::YouTube => Self::Invidious,
200 Self::Invidious => Self::YouTube,
201 };
202 }
203}
204
205// default functions
206
207fn invidious_instance_default() -> String {
208 String::from("https://invidious.f5.si")
209}
210
211fn message_bar_default_default() -> String {
212 String::from("All good :)")
213}
214
215const fn images_default() -> Images {
216 Images::Sixels
217}
218
219const fn image_index_default() -> usize {
220 4
221}
222
223const fn allow_unicode_default() -> bool {
224 true
225}
226
227const fn refresh_after_modifying_search_filters_default() -> bool {
228 true
229}
230
231const fn provider_default() -> Provider {
232 Provider::YouTube
233}
234
235#[cfg(not(any(feature = "rustypipe", feature = "invidious")))]
236compile_error!(
237 "You must include a search provider in your feature list, they are `rustypipe` and `invidious`"
238);
239
240#[cfg(feature = "rustypipe")]
241const fn search_provider_default() -> SearchProvider {
242 SearchProvider::RustyPipe
243}
244
245#[cfg(all(feature = "invidious", not(feature = "rustypipe")))]
246const fn search_provider_default() -> SearchProvider {
247 SearchProvider::Invidious
248}
249
250const fn search_history_default() -> usize {
251 75
252}
253
254const fn commands_history_default() -> usize {
255 75
256}
257
258const fn watch_history_default() -> usize {
259 50
260}
261
262const fn mouse_support_default() -> bool {
263 true
264}
265
266const fn write_to_config_default() -> WriteConfig {
267 WriteConfig::Try
268}
269
270fn default_env() -> HashMap<String, String> {
271 HashMap::from([
272 (String::from("video-player"), String::from("mpv")),
273 (String::from("browser"), String::from("xdg-open")),
274 (
275 String::from("terminal-emulator"),
276 String::from("konsole -e"),
277 ),
278 (String::from("youtube-downloader"), String::from("yt-dlp")),
279 (
280 String::from("download-path"),
281 String::from("~/Downloads/%(title)s-%(id)s.%(ext)s"),
282 ),
283 (
284 String::from("save-path"),
285 String::from("~/.local/share/youtube-tui/saved/"),
286 ),
287 ])
288}
289
290fn shell_default() -> String {
291 String::from("sh")
292}
293
294fn api_key_default() -> String {
295 String::from("YOUR API KEY HERE")
296}
297
298const fn download_images_default() -> bool {
299 true
300}
301
302const fn sync_channel_info_default() -> bool {
303 true
304}
305
306const fn sync_channel_cooldown_secs_default() -> u64 {
307 86400
308}
309
310const fn sync_videos_cooldown_secs_default() -> u64 {
311 600
312}
313
314const fn legacy_input_handling_default() -> bool {
315 false
316}
317
318#[derive(Serialize, Deserialize, Clone, Copy)]
319pub enum TextbarScrollBehaviour {
320 Character,
321 Word,
322 History,
323}
324const fn textbar_scroll_behaviour_default() -> TextbarScrollBehaviour {
325 TextbarScrollBehaviour::History
326}