src/config/main.rs

147 lines · 3926 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 = "invidious_instance_default")]
 10    pub invidious_instance: String,
 11    #[serde(default = "max_watch_history_default")]
 12    pub max_watch_history: usize,
 13    #[serde(default = "allow_unicode_default")]
 14    pub allow_unicode: bool,
 15    #[serde(default = "message_bar_default_default")]
 16    pub message_bar_default: String,
 17    #[serde(default = "images_default")]
 18    pub images: Images,
 19    #[serde(default = "refresh_after_modifying_search_filters_default")]
 20    pub refresh_after_modifying_search_filters: bool,
 21    #[serde(default = "image_index_default")]
 22    // The image to download from the array of images provided by the invidious api
 23    // 0 is usually `maxres` and 3 (default) is good enough for normal uses without having huge files sizes
 24    // Check `https://{any invidious instance}/api/v1/videos/{any valid video id}` property videoThumbnails in Firefox dev tools to see it for yourself
 25    pub image_index: usize,
 26    #[serde(default = "provider_default")]
 27    pub provider: Provider,
 28    #[serde(default = "default_env")]
 29    pub env: HashMap<String, String>,
 30}
 31
 32impl Key for MainConfig {
 33    type Value = Self;
 34}
 35
 36impl Default for MainConfig {
 37    fn default() -> Self {
 38        Self {
 39            invidious_instance: invidious_instance_default(),
 40            max_watch_history: max_watch_history_default(),
 41            allow_unicode: allow_unicode_default(),
 42            message_bar_default: message_bar_default_default(),
 43            images: images_default(),
 44            image_index: image_index_default(),
 45            refresh_after_modifying_search_filters: refresh_after_modifying_search_filters_default(
 46            ),
 47            provider: provider_default(),
 48
 49            env: default_env(),
 50        }
 51    }
 52}
 53
 54impl ConfigTrait for MainConfig {
 55    const LABEL: &'static str = "main";
 56}
 57
 58/// how images are handled/displayed
 59#[derive(Clone, Copy, Serialize, Deserialize, PartialEq)]
 60pub enum Images {
 61    r#None,
 62    HalfBlocks,
 63    Sixels,
 64}
 65
 66impl Images {
 67    pub fn display(&self) -> bool {
 68        !(self == &Self::None)
 69    }
 70
 71    pub fn use_sixels(&self) -> bool {
 72        self == &Self::Sixels
 73    }
 74}
 75
 76#[derive(Clone, Copy, Serialize, Deserialize, PartialEq)]
 77pub enum Provider {
 78    YouTube,
 79    Invidious,
 80    // Piped,
 81}
 82
 83impl Provider {
 84    pub fn as_str(&self) -> &str {
 85        match self {
 86            Self::YouTube => "YouTube",
 87            Self::Invidious => "Invidious",
 88        }
 89    }
 90
 91    pub fn rotate(&mut self) {
 92        *self = match self {
 93            Self::YouTube => Self::Invidious,
 94            Self::Invidious => Self::YouTube,
 95        };
 96    }
 97}
 98
 99// default functions
100
101fn invidious_instance_default() -> String {
102    String::from("https://vid.puffyan.us")
103}
104
105fn message_bar_default_default() -> String {
106    String::from("All good :)")
107}
108
109const fn images_default() -> Images {
110    Images::Sixels
111}
112
113const fn image_index_default() -> usize {
114    4
115}
116
117const fn allow_unicode_default() -> bool {
118    false
119}
120
121const fn refresh_after_modifying_search_filters_default() -> bool {
122    true
123}
124
125const fn provider_default() -> Provider {
126    Provider::YouTube
127}
128
129const fn max_watch_history_default() -> usize {
130    50
131}
132
133fn default_env() -> HashMap<String, String> {
134    HashMap::from([
135        (String::from("video-player"), String::from("mpv")),
136        (String::from("browser"), String::from("firefox")),
137        (
138            String::from("terminal-emulator"),
139            String::from("konsole -e"),
140        ),
141        (String::from("youtube-downloader"), String::from("yt-dlp")),
142        (
143            String::from("download-path"),
144            String::from("'~/Downloads/%(title)s-%(id)s.%(ext)s'"),
145        ),
146    ])
147}