src/config/main.rs

224 lines · 6027 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 = "legacy_input_handling_default")]
 36    pub legacy_input_handling: bool,
 37    #[serde(default = "default_env")]
 38    pub env: HashMap<String, String>,
 39}
 40
 41impl Key for MainConfig {
 42    type Value = Self;
 43}
 44
 45impl Default for MainConfig {
 46    fn default() -> Self {
 47        Self {
 48            write_config: write_to_config_default(),
 49            mouse_support: mouse_support_default(),
 50            invidious_instance: invidious_instance_default(),
 51            max_watch_history: max_watch_history_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            syncing: sync_config_default(),
 59            provider: provider_default(),
 60            shell: shell_default(),
 61            legacy_input_handling: legacy_input_handling_default(),
 62
 63            env: default_env(),
 64        }
 65    }
 66}
 67
 68impl ConfigTrait for MainConfig {
 69    const LABEL: &'static str = "main";
 70}
 71
 72#[derive(Clone, Copy, Serialize, Deserialize)]
 73pub struct SyncConfig {
 74    #[serde(default = "download_images_default")]
 75    pub download_images: bool,
 76    #[serde(default = "sync_channel_info_default")]
 77    pub sync_channel_info: bool,
 78    #[serde(default = "sync_channel_cooldown_secs_default")]
 79    pub sync_channel_cooldown_secs: u64,
 80    #[serde(default = "sync_videos_cooldown_secs_default")]
 81    pub sync_videos_cooldown_secs: u64,
 82}
 83
 84const fn sync_config_default() -> SyncConfig {
 85    SyncConfig {
 86        download_images: download_images_default(),
 87        sync_channel_info: sync_channel_info_default(),
 88        sync_channel_cooldown_secs: sync_channel_cooldown_secs_default(),
 89        sync_videos_cooldown_secs: sync_videos_cooldown_secs_default(),
 90    }
 91}
 92
 93/// how images are handled/displayed
 94#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
 95pub enum Images {
 96    r#None,
 97    HalfBlocks,
 98    Sixels,
 99}
100
101impl Images {
102    pub fn display(&self) -> bool {
103        !(self == &Self::None)
104    }
105
106    pub fn use_sixels(&self) -> bool {
107        self == &Self::Sixels
108    }
109}
110
111#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
112pub enum Provider {
113    YouTube,
114    Invidious,
115}
116
117#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
118pub enum WriteConfig {
119    Must,
120    Try,
121    Dont,
122}
123
124impl Provider {
125    pub fn as_str(&self) -> &str {
126        match self {
127            Self::YouTube => "YouTube",
128            Self::Invidious => "Invidious",
129        }
130    }
131
132    pub fn rotate(&mut self) {
133        *self = match self {
134            Self::YouTube => Self::Invidious,
135            Self::Invidious => Self::YouTube,
136        };
137    }
138}
139
140// default functions
141
142fn invidious_instance_default() -> String {
143    String::from("https://vid.puffyan.us")
144}
145
146fn message_bar_default_default() -> String {
147    String::from("All good :)")
148}
149
150const fn images_default() -> Images {
151    Images::Sixels
152}
153
154const fn image_index_default() -> usize {
155    4
156}
157
158const fn allow_unicode_default() -> bool {
159    true
160}
161
162const fn refresh_after_modifying_search_filters_default() -> bool {
163    true
164}
165
166const fn provider_default() -> Provider {
167    Provider::YouTube
168}
169
170const fn max_watch_history_default() -> usize {
171    50
172}
173
174const fn mouse_support_default() -> bool {
175    true
176}
177
178const fn write_to_config_default() -> WriteConfig {
179    WriteConfig::Try
180}
181
182fn default_env() -> HashMap<String, String> {
183    HashMap::from([
184        (String::from("video-player"), String::from("mpv")),
185        (String::from("browser"), String::from("firefox")),
186        (
187            String::from("terminal-emulator"),
188            String::from("konsole -e"),
189        ),
190        (String::from("youtube-downloader"), String::from("yt-dlp")),
191        (
192            String::from("download-path"),
193            String::from("~/Downloads/%(title)s-%(id)s.%(ext)s"),
194        ),
195        (
196            String::from("save-path"),
197            String::from("~/.local/share/youtube-tui/saved/"),
198        ),
199    ])
200}
201
202fn shell_default() -> String {
203    String::from("sh")
204}
205
206const fn download_images_default() -> bool {
207    true
208}
209
210const fn sync_channel_info_default() -> bool {
211    true
212}
213
214const fn sync_channel_cooldown_secs_default() -> u64 {
215    86400
216}
217
218const fn sync_videos_cooldown_secs_default() -> u64 {
219    600
220}
221
222const fn legacy_input_handling_default() -> bool {
223    false
224}