src/config/main.rs

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