src/config/main.rs
302 lines · 7946 bytes
1use crate::{
2 global::{
3 structs::{InvidiousClient, RustyPipeWrapper},
4 traits::{ConfigTrait, SearchProviderTrait},
5 },
6 MAIN_CONFIG,
7};
8use serde::{Deserialize, Serialize};
9use std::collections::HashMap;
10use typemap::Key;
11
12/// `main.yml`, the main config file
13#[derive(Serialize, Deserialize, Clone)]
14pub struct MainConfig {
15 #[serde(default = "mouse_support_default")]
16 pub mouse_support: bool,
17 #[serde(default = "invidious_instance_default")]
18 pub invidious_instance: String,
19 #[serde(default = "write_to_config_default")]
20 pub write_config: WriteConfig,
21 #[serde(default = "allow_unicode_default")]
22 pub allow_unicode: bool,
23 #[serde(default = "message_bar_default_default")]
24 pub message_bar_default: String,
25 #[serde(default = "images_default")]
26 pub images: Images,
27 #[serde(default = "refresh_after_modifying_search_filters_default")]
28 pub refresh_after_modifying_search_filters: bool,
29 #[serde(default = "sync_config_default")]
30 pub syncing: SyncConfig,
31 #[serde(default)]
32 pub limits: Limits,
33 #[serde(default = "image_index_default")]
34 // The image to download from the array of images provided by the invidious api
35 // 0 is usually `maxres` and 3 (default) is good enough for normal uses without having huge files sizes
36 // Check `https://{any invidious instance}/api/v1/videos/{any valid video id}` property videoThumbnails in Firefox dev tools to see it for yourself
37 pub image_index: usize,
38 #[serde(default = "provider_default")]
39 pub provider: Provider,
40 #[serde(default = "search_provider_default")]
41 pub search_provider: SearchProvider,
42 #[serde(default = "api_key_default")]
43 pub api_key: String,
44 #[serde(default = "shell_default")]
45 pub shell: String,
46 #[serde(default = "legacy_input_handling_default")]
47 pub legacy_input_handling: bool,
48 #[serde(default = "default_env")]
49 pub env: HashMap<String, String>,
50}
51
52impl Key for MainConfig {
53 type Value = Self;
54}
55
56impl Default for MainConfig {
57 fn default() -> Self {
58 Self {
59 write_config: write_to_config_default(),
60 mouse_support: mouse_support_default(),
61 invidious_instance: invidious_instance_default(),
62 allow_unicode: allow_unicode_default(),
63 message_bar_default: message_bar_default_default(),
64 images: images_default(),
65 image_index: image_index_default(),
66 refresh_after_modifying_search_filters: refresh_after_modifying_search_filters_default(
67 ),
68 limits: Limits::default(),
69 syncing: sync_config_default(),
70 provider: provider_default(),
71 search_provider: search_provider_default(),
72 api_key: api_key_default(),
73 shell: shell_default(),
74 legacy_input_handling: legacy_input_handling_default(),
75
76 env: default_env(),
77 }
78 }
79}
80
81impl ConfigTrait for MainConfig {
82 const LABEL: &'static str = "main";
83}
84
85#[derive(Clone, Copy, Serialize, Deserialize)]
86pub struct SyncConfig {
87 #[serde(default = "download_images_default")]
88 pub download_images: bool,
89 #[serde(default = "sync_channel_info_default")]
90 pub sync_channel_info: bool,
91 #[serde(default = "sync_channel_cooldown_secs_default")]
92 pub sync_channel_cooldown_secs: u64,
93 #[serde(default = "sync_videos_cooldown_secs_default")]
94 pub sync_videos_cooldown_secs: u64,
95}
96
97const fn sync_config_default() -> SyncConfig {
98 SyncConfig {
99 download_images: download_images_default(),
100 sync_channel_info: sync_channel_info_default(),
101 sync_channel_cooldown_secs: sync_channel_cooldown_secs_default(),
102 sync_videos_cooldown_secs: sync_videos_cooldown_secs_default(),
103 }
104}
105
106#[derive(Clone, Copy, Serialize, Deserialize)]
107pub struct Limits {
108 #[serde(default = "watch_history_default")]
109 pub watch_history: usize,
110 #[serde(default = "search_history_default")]
111 pub search_history: usize,
112 #[serde(default = "commands_history_default")]
113 pub commands_history: usize,
114}
115
116impl Default for Limits {
117 fn default() -> Self {
118 Self {
119 watch_history: watch_history_default(),
120 search_history: search_history_default(),
121 commands_history: commands_history_default(),
122 }
123 }
124}
125
126/// how images are handled/displayed
127#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
128pub enum Images {
129 r#None,
130 HalfBlocks,
131 Sixels,
132}
133
134impl Images {
135 pub fn display(&self) -> bool {
136 !(self == &Self::None)
137 }
138
139 pub fn use_sixels(&self) -> bool {
140 self == &Self::Sixels
141 }
142}
143
144#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
145pub enum Provider {
146 YouTube,
147 Invidious,
148}
149
150#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Hash)]
151pub enum SearchProvider {
152 Invidious,
153 RustyPipe,
154}
155
156impl SearchProvider {
157 pub fn create(&self) -> Box<dyn SearchProviderTrait> {
158 match self {
159 Self::Invidious => Box::new(InvidiousClient::new(
160 unsafe { MAIN_CONFIG.get() }
161 .unwrap()
162 .invidious_instance
163 .clone(),
164 )),
165 Self::RustyPipe => Box::new(RustyPipeWrapper::default()),
166 }
167 }
168}
169
170impl SearchProvider {
171 pub fn as_str(&self) -> &str {
172 match self {
173 Self::Invidious => "Invidious",
174 Self::RustyPipe => "RustyPipe",
175 }
176 }
177}
178
179#[derive(Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
180pub enum WriteConfig {
181 Must,
182 Try,
183 Dont,
184}
185
186impl Provider {
187 pub fn as_str(&self) -> &str {
188 match self {
189 Self::YouTube => "YouTube",
190 Self::Invidious => "Invidious",
191 }
192 }
193
194 pub fn rotate(&mut self) {
195 *self = match self {
196 Self::YouTube => Self::Invidious,
197 Self::Invidious => Self::YouTube,
198 };
199 }
200}
201
202// default functions
203
204fn invidious_instance_default() -> String {
205 String::from("https://invidious.f5.si")
206}
207
208fn message_bar_default_default() -> String {
209 String::from("All good :)")
210}
211
212const fn images_default() -> Images {
213 Images::Sixels
214}
215
216const fn image_index_default() -> usize {
217 4
218}
219
220const fn allow_unicode_default() -> bool {
221 true
222}
223
224const fn refresh_after_modifying_search_filters_default() -> bool {
225 true
226}
227
228const fn provider_default() -> Provider {
229 Provider::YouTube
230}
231
232const fn search_provider_default() -> SearchProvider {
233 SearchProvider::RustyPipe
234}
235
236const fn search_history_default() -> usize {
237 75
238}
239
240const fn commands_history_default() -> usize {
241 75
242}
243
244const fn watch_history_default() -> usize {
245 50
246}
247
248const fn mouse_support_default() -> bool {
249 true
250}
251
252const fn write_to_config_default() -> WriteConfig {
253 WriteConfig::Try
254}
255
256fn default_env() -> HashMap<String, String> {
257 HashMap::from([
258 (String::from("video-player"), String::from("mpv")),
259 (String::from("browser"), String::from("firefox")),
260 (
261 String::from("terminal-emulator"),
262 String::from("konsole -e"),
263 ),
264 (String::from("youtube-downloader"), String::from("yt-dlp")),
265 (
266 String::from("download-path"),
267 String::from("~/Downloads/%(title)s-%(id)s.%(ext)s"),
268 ),
269 (
270 String::from("save-path"),
271 String::from("~/.local/share/youtube-tui/saved/"),
272 ),
273 ])
274}
275
276fn shell_default() -> String {
277 String::from("sh")
278}
279
280fn api_key_default() -> String {
281 String::from("YOUR API KEY HERE")
282}
283
284const fn download_images_default() -> bool {
285 true
286}
287
288const fn sync_channel_info_default() -> bool {
289 true
290}
291
292const fn sync_channel_cooldown_secs_default() -> u64 {
293 86400
294}
295
296const fn sync_videos_cooldown_secs_default() -> u64 {
297 600
298}
299
300const fn legacy_input_handling_default() -> bool {
301 false
302}