src/config/main.rs

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