src/config/main.rs

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