src/config/main.rs

376 lines · 10301 bytes

45 symbols in this file
  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    #[serde(default = "blacklist")]
 47    pub block_list: BlockList,
 48}
 49
 50#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
 51pub struct BlockList {
 52    #[serde(default = "channels")]
 53    pub channels: Vec<String>
 54}
 55
 56const fn channels() -> Vec<String> {
 57    return vec![]
 58}
 59const fn blacklist() -> BlockList {
 60    BlockList {
 61        channels: vec![]
 62    }
 63}
 64
 65impl Key for MainConfig {
 66    type Value = Self;
 67}
 68
 69impl Default for MainConfig {
 70    fn default() -> Self {
 71        Self {
 72            write_config: write_to_config_default(),
 73            mouse_support: mouse_support_default(),
 74            invidious_instance: invidious_instance_default(),
 75            allow_unicode: allow_unicode_default(),
 76            message_bar_default: message_bar_default_default(),
 77            images: images_default(),
 78            image_index: image_index_default(),
 79            refresh_after_modifying_search_filters: refresh_after_modifying_search_filters_default(
 80            ),
 81            textbar_scroll_behaviour: textbar_scroll_behaviour_default(),
 82            limits: Limits::default(),
 83            syncing: sync_config_default(),
 84            provider: provider_default(),
 85            search_provider: search_provider_default(),
 86            api_key: api_key_default(),
 87            shell: shell_default(),
 88            legacy_input_handling: legacy_input_handling_default(),
 89
 90            env: default_env(),
 91            block_list: blacklist(),
 92        }
 93    }
 94}
 95
 96impl ConfigTrait for MainConfig {
 97    const LABEL: &'static str = "main";
 98}
 99
100#[derive(Clone, Copy, Serialize, Deserialize)]
101pub struct SyncConfig {
102    #[serde(default = "download_images_default")]
103    pub download_images: bool,
104    #[serde(default = "sync_channel_info_default")]
105    pub sync_channel_info: bool,
106    #[serde(default = "sync_channel_cooldown_secs_default")]
107    pub sync_channel_cooldown_secs: u64,
108    #[serde(default = "sync_videos_cooldown_secs_default")]
109    pub sync_videos_cooldown_secs: u64,
110}
111
112const fn sync_config_default() -> SyncConfig {
113    SyncConfig {
114        download_images: download_images_default(),
115        sync_channel_info: sync_channel_info_default(),
116        sync_channel_cooldown_secs: sync_channel_cooldown_secs_default(),
117        sync_videos_cooldown_secs: sync_videos_cooldown_secs_default(),
118    }
119}
120
121#[derive(Clone, Copy, Serialize, Deserialize)]
122pub struct Limits {
123    #[serde(default = "watch_history_default")]
124    pub watch_history: usize,
125    #[serde(default = "search_history_default")]
126    pub search_history: usize,
127    #[serde(default = "commands_history_default")]
128    pub commands_history: usize,
129}
130
131impl Default for Limits {
132    fn default() -> Self {
133        Self {
134            watch_history: watch_history_default(),
135            search_history: search_history_default(),
136            commands_history: commands_history_default(),
137        }
138    }
139}
140
141/// how images are handled/displayed
142#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
143pub enum Images {
144    r#None,
145    HalfBlocks,
146    #[serde(alias = "Sixels")]
147    Sixel,
148}
149
150impl Images {
151    pub fn display(&self) -> bool {
152        !(self == &Self::None)
153    }
154
155    pub fn use_sixels(&self) -> bool {
156        self == &Self::Sixel
157    }
158}
159
160#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
161pub enum Provider {
162    YouTube,
163    Invidious,
164}
165
166#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
167pub enum SearchProvider {
168    #[cfg(feature = "invidious")]
169    Invidious,
170    #[cfg(feature = "rustypipe")]
171    RustyPipe,
172}
173
174impl SearchProvider {
175    pub fn create(&self) -> Box<dyn SearchProviderTrait> {
176        match self {
177            #[cfg(feature = "invidious")]
178            Self::Invidious => Box::new(crate::global::structs::InvidiousClient::new(
179                unsafe { crate::MAIN_CONFIG.get() }
180                    .unwrap()
181                    .invidious_instance
182                    .clone(),
183            )),
184            #[cfg(feature = "rustypipe")]
185            Self::RustyPipe => Box::new(crate::global::structs::RustyPipeWrapper::default()),
186        }
187    }
188}
189
190impl SearchProvider {
191    pub fn as_str(&self) -> &str {
192        match self {
193            #[cfg(feature = "invidious")]
194            Self::Invidious => "Invidious",
195            #[cfg(feature = "rustypipe")]
196            Self::RustyPipe => "RustyPipe",
197        }
198    }
199}
200
201#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
202pub enum WriteConfig {
203    Must,
204    Try,
205    Dont,
206}
207
208impl Provider {
209    pub fn as_str(&self) -> &str {
210        match self {
211            Self::YouTube => "YouTube",
212            Self::Invidious => "Invidious",
213        }
214    }
215
216    pub fn rotate(&mut self) {
217        *self = match self {
218            Self::YouTube => Self::Invidious,
219            Self::Invidious => Self::YouTube,
220        };
221    }
222}
223
224// default functions
225
226fn invidious_instance_default() -> String {
227    String::from("https://invidious.f5.si")
228}
229
230fn message_bar_default_default() -> String {
231    String::from("All good :)")
232}
233
234const fn images_default() -> Images {
235    Images::Sixel
236}
237
238const fn image_index_default() -> usize {
239    4
240}
241
242const fn allow_unicode_default() -> bool {
243    true
244}
245
246const fn refresh_after_modifying_search_filters_default() -> bool {
247    true
248}
249
250const fn provider_default() -> Provider {
251    Provider::YouTube
252}
253
254#[cfg(not(any(feature = "rustypipe", feature = "invidious")))]
255compile_error!(
256    "You must include a search provider in your feature list, they are `rustypipe` and `invidious`"
257);
258
259#[cfg(feature = "rustypipe")]
260const fn search_provider_default() -> SearchProvider {
261    SearchProvider::RustyPipe
262}
263
264#[cfg(all(feature = "invidious", not(feature = "rustypipe")))]
265const fn search_provider_default() -> SearchProvider {
266    SearchProvider::Invidious
267}
268
269const fn search_history_default() -> usize {
270    75
271}
272
273const fn commands_history_default() -> usize {
274    75
275}
276
277const fn watch_history_default() -> usize {
278    50
279}
280
281const fn mouse_support_default() -> bool {
282    true
283}
284
285const fn write_to_config_default() -> WriteConfig {
286    WriteConfig::Try
287}
288
289fn default_env() -> HashMap<String, String> {
290    #[cfg(target_os = "windows")]
291    {
292        use crate::global::functions::paths;
293        HashMap::from([
294            (String::from("video-player"), String::from("mpv")),
295            (String::from("browser"), String::from("explorer")),
296            (
297                String::from("terminal-emulator"),
298                String::from("cmd /c start cmd /k"),
299            ),
300            (String::from("youtube-downloader"), String::from("yt-dlp")),
301            (
302                String::from("download-path"),
303                paths::default_download_path(),
304            ),
305            (
306                String::from("save-path"),
307                paths::default_save_path(),
308            ),
309        ])
310    }
311    #[cfg(not(target_os = "windows"))]
312    {
313        HashMap::from([
314            (String::from("video-player"), String::from("mpv")),
315            (String::from("browser"), String::from("xdg-open")),
316            (
317                String::from("terminal-emulator"),
318                String::from("konsole -e"),
319            ),
320            (String::from("youtube-downloader"), String::from("yt-dlp")),
321            (
322                String::from("download-path"),
323                String::from("~/Downloads/%(title)s-%(id)s.%(ext)s"),
324            ),
325            (
326                String::from("save-path"),
327                String::from("~/.local/share/youtube-tui/saved/"),
328            ),
329        ])
330    }
331}
332
333fn shell_default() -> String {
334    #[cfg(target_os = "windows")]
335    {
336        String::from("cmd")
337    }
338    #[cfg(not(target_os = "windows"))]
339    {
340        String::from("sh")
341    }
342}
343
344fn api_key_default() -> String {
345    String::from("YOUR API KEY HERE")
346}
347
348const fn download_images_default() -> bool {
349    true
350}
351
352const fn sync_channel_info_default() -> bool {
353    true
354}
355
356const fn sync_channel_cooldown_secs_default() -> u64 {
357    86400
358}
359
360const fn sync_videos_cooldown_secs_default() -> u64 {
361    600
362}
363
364const fn legacy_input_handling_default() -> bool {
365    false
366}
367
368#[derive(Serialize, Deserialize, Clone, Copy)]
369pub enum TextbarScrollBehaviour {
370    Character,
371    Word,
372    History,
373}
374const fn textbar_scroll_behaviour_default() -> TextbarScrollBehaviour {
375    TextbarScrollBehaviour::History
376}