src/config/main.rs
154 lines · 4117 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 // Piped,
84}
85
86impl Provider {
87 pub fn as_str(&self) -> &str {
88 match self {
89 Self::YouTube => "YouTube",
90 Self::Invidious => "Invidious",
91 }
92 }
93
94 pub fn rotate(&mut self) {
95 *self = match self {
96 Self::YouTube => Self::Invidious,
97 Self::Invidious => Self::YouTube,
98 };
99 }
100}
101
102// default functions
103
104fn invidious_instance_default() -> String {
105 String::from("https://vid.puffyan.us")
106}
107
108fn message_bar_default_default() -> String {
109 String::from("All good :)")
110}
111
112const fn images_default() -> Images {
113 Images::Sixels
114}
115
116const fn image_index_default() -> usize {
117 4
118}
119
120const fn allow_unicode_default() -> bool {
121 true
122}
123
124const fn refresh_after_modifying_search_filters_default() -> bool {
125 true
126}
127
128const fn provider_default() -> Provider {
129 Provider::YouTube
130}
131
132const fn max_watch_history_default() -> usize {
133 50
134}
135
136const fn mouse_support_default() -> bool {
137 true
138}
139
140fn default_env() -> HashMap<String, String> {
141 HashMap::from([
142 (String::from("video-player"), String::from("mpv")),
143 (String::from("browser"), String::from("firefox")),
144 (
145 String::from("terminal-emulator"),
146 String::from("konsole -e"),
147 ),
148 (String::from("youtube-downloader"), String::from("yt-dlp")),
149 (
150 String::from("download-path"),
151 String::from("'~/Downloads/%(title)s-%(id)s.%(ext)s'"),
152 ),
153 ])
154}