src/config/main.rs

217 lines · 5796 bytes

  1use crate::global::traits::ConfigTrait;
  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 = "max_watch_history_default")]
 16    pub max_watch_history: usize,
 17    #[serde(default = "allow_unicode_default")]
 18    pub allow_unicode: bool,
 19    #[serde(default = "message_bar_default_default")]
 20    pub message_bar_default: String,
 21    #[serde(default = "images_default")]
 22    pub images: Images,
 23    #[serde(default = "refresh_after_modifying_search_filters_default")]
 24    pub refresh_after_modifying_search_filters: bool,
 25    pub syncing: SyncConfig,
 26    #[serde(default = "image_index_default")]
 27    // The image to download from the array of images provided by the invidious api
 28    // 0 is usually `maxres` and 3 (default) is good enough for normal uses without having huge files sizes
 29    // Check `https://{any invidious instance}/api/v1/videos/{any valid video id}` property videoThumbnails in Firefox dev tools to see it for yourself
 30    pub image_index: usize,
 31    #[serde(default = "provider_default")]
 32    pub provider: Provider,
 33    #[serde(default = "shell_default")]
 34    pub shell: String,
 35    #[serde(default = "default_env")]
 36    pub env: HashMap<String, String>,
 37}
 38
 39impl Key for MainConfig {
 40    type Value = Self;
 41}
 42
 43impl Default for MainConfig {
 44    fn default() -> Self {
 45        Self {
 46            write_config: write_to_config_default(),
 47            mouse_support: mouse_support_default(),
 48            invidious_instance: invidious_instance_default(),
 49            max_watch_history: max_watch_history_default(),
 50            allow_unicode: allow_unicode_default(),
 51            message_bar_default: message_bar_default_default(),
 52            images: images_default(),
 53            image_index: image_index_default(),
 54            refresh_after_modifying_search_filters: refresh_after_modifying_search_filters_default(
 55            ),
 56            syncing: sync_config_default(),
 57            provider: provider_default(),
 58            shell: shell_default(),
 59
 60            env: default_env(),
 61        }
 62    }
 63}
 64
 65impl ConfigTrait for MainConfig {
 66    const LABEL: &'static str = "main";
 67}
 68
 69#[derive(Clone, Copy, Serialize, Deserialize)]
 70pub struct SyncConfig {
 71    #[serde(default = "download_images_default")]
 72    pub download_images: bool,
 73    #[serde(default = "sync_channel_info_default")]
 74    pub sync_channel_info: bool,
 75    #[serde(default = "sync_channel_cooldown_secs_default")]
 76    pub sync_channel_cooldown_secs: u64,
 77    #[serde(default = "sync_videos_cooldown_secs_default")]
 78    pub sync_videos_cooldown_secs: u64,
 79}
 80
 81const fn sync_config_default() -> SyncConfig {
 82    SyncConfig {
 83        download_images: download_images_default(),
 84        sync_channel_info: sync_channel_info_default(),
 85        sync_channel_cooldown_secs: sync_channel_cooldown_secs_default(),
 86        sync_videos_cooldown_secs: sync_videos_cooldown_secs_default(),
 87    }
 88}
 89
 90/// how images are handled/displayed
 91#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
 92pub enum Images {
 93    r#None,
 94    HalfBlocks,
 95    Sixels,
 96}
 97
 98impl Images {
 99    pub fn display(&self) -> bool {
100        !(self == &Self::None)
101    }
102
103    pub fn use_sixels(&self) -> bool {
104        self == &Self::Sixels
105    }
106}
107
108#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
109pub enum Provider {
110    YouTube,
111    Invidious,
112}
113
114#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
115pub enum WriteConfig {
116    Must,
117    Try,
118    Dont,
119}
120
121impl Provider {
122    pub fn as_str(&self) -> &str {
123        match self {
124            Self::YouTube => "YouTube",
125            Self::Invidious => "Invidious",
126        }
127    }
128
129    pub fn rotate(&mut self) {
130        *self = match self {
131            Self::YouTube => Self::Invidious,
132            Self::Invidious => Self::YouTube,
133        };
134    }
135}
136
137// default functions
138
139fn invidious_instance_default() -> String {
140    String::from("https://y.com.sb")
141}
142
143fn message_bar_default_default() -> String {
144    String::from("All good :)")
145}
146
147const fn images_default() -> Images {
148    Images::Sixels
149}
150
151const fn image_index_default() -> usize {
152    4
153}
154
155const fn allow_unicode_default() -> bool {
156    true
157}
158
159const fn refresh_after_modifying_search_filters_default() -> bool {
160    true
161}
162
163const fn provider_default() -> Provider {
164    Provider::YouTube
165}
166
167const fn max_watch_history_default() -> usize {
168    50
169}
170
171const fn mouse_support_default() -> bool {
172    true
173}
174
175const fn write_to_config_default() -> WriteConfig {
176    WriteConfig::Try
177}
178
179fn default_env() -> HashMap<String, String> {
180    HashMap::from([
181        (String::from("video-player"), String::from("mpv")),
182        (String::from("browser"), String::from("firefox")),
183        (
184            String::from("terminal-emulator"),
185            String::from("konsole -e"),
186        ),
187        (String::from("youtube-downloader"), String::from("yt-dlp")),
188        (
189            String::from("download-path"),
190            String::from("~/Downloads/%(title)s-%(id)s.%(ext)s"),
191        ),
192        (
193            String::from("save-path"),
194            String::from("~/.local/share/youtube-tui/saved/"),
195        ),
196    ])
197}
198
199fn shell_default() -> String {
200    String::from("sh")
201}
202
203const fn download_images_default() -> bool {
204    true
205}
206
207const fn sync_channel_info_default() -> bool {
208    true
209}
210
211const fn sync_channel_cooldown_secs_default() -> u64 {
212    86400
213}
214
215const fn sync_videos_cooldown_secs_default() -> u64 {
216    600
217}