src/structs/search_settings.rs

6f8b64c4fa1b89868a2dbaa1bbd9fda20ecd0768
youtube-tui/src/structs/search_settings.rs history · blame · raw

387 lines · 11601 bytes

  1use crossterm::event::KeyCode;
  2
  3use crate::widgets::text_list::TextList;
  4
  5#[derive(Debug, Clone)]
  6pub struct SearchSettings {
  7    pub sort_by: SearchSettingsSortBy,
  8    pub date: Option<SearchSettingsDate>,
  9    pub duartion: Option<SearchSettingsDuration>,
 10    pub r#type: Option<SearchSettingsType>,
 11
 12    pub row: bool, // left: true, right: false
 13
 14    pub text_list: TextList,
 15    pub select_text_list: TextList,
 16}
 17
 18impl Default for SearchSettings {
 19    fn default() -> Self {
 20        let mut text_list = TextList::default();
 21        text_list.items = vec![
 22            String::from("Sort by"),
 23            String::from("Date"),
 24            String::from("Duration"),
 25            String::from("Type"),
 26            String::from("Reset All"),
 27        ];
 28
 29        let mut out = SearchSettings {
 30            sort_by: SearchSettingsSortBy::Relevance,
 31            date: None,
 32            duartion: None,
 33            r#type: None,
 34
 35            row: true,
 36
 37            text_list,
 38            select_text_list: TextList::default(),
 39        };
 40
 41        out.update_left();
 42
 43        out
 44    }
 45}
 46
 47impl SearchSettings {
 48    pub fn to_vec(self) -> Vec<String> {
 49        let mut vec = vec![self.sort_by.as_url_string()];
 50
 51        if let Some(date) = self.date {
 52            vec.push(date.as_url_string());
 53        }
 54
 55        if let Some(duration) = self.duartion {
 56            vec.push(duration.as_url_string());
 57        }
 58
 59        if let Some(r#type) = self.r#type {
 60            vec.push(r#type.as_url_string());
 61        }
 62
 63        vec
 64    }
 65
 66    pub fn key_input(&mut self, keycode: KeyCode) {
 67        match keycode {
 68            KeyCode::Up => {
 69                if self.row {
 70                    self.text_list.up();
 71                    self.update_left()
 72                } else {
 73                    self.select_text_list.up();
 74
 75                    match self.text_list.selected {
 76                        0 => {
 77                            self.sort_by =
 78                                SearchSettingsSortBy::index_to_value(self.select_text_list.selected)
 79                        }
 80                        1 => {
 81                            self.date =
 82                                SearchSettingsDate::index_to_value(self.select_text_list.selected)
 83                        }
 84                        2 => {
 85                            self.duartion = SearchSettingsDuration::index_to_value(
 86                                self.select_text_list.selected,
 87                            )
 88                        }
 89                        3 => {
 90                            self.r#type =
 91                                SearchSettingsType::index_to_value(self.select_text_list.selected)
 92                        }
 93                        _ => {}
 94                    }
 95                }
 96            }
 97            KeyCode::Down => {
 98                if self.row {
 99                    self.text_list.down();
100                    self.update_left()
101                } else {
102                    self.select_text_list.down();
103
104                    match self.text_list.selected {
105                        0 => {
106                            self.sort_by =
107                                SearchSettingsSortBy::index_to_value(self.select_text_list.selected)
108                        }
109                        1 => {
110                            self.date =
111                                SearchSettingsDate::index_to_value(self.select_text_list.selected)
112                        }
113                        2 => {
114                            self.duartion = SearchSettingsDuration::index_to_value(
115                                self.select_text_list.selected,
116                            )
117                        }
118                        3 => {
119                            self.r#type =
120                                SearchSettingsType::index_to_value(self.select_text_list.selected)
121                        }
122                        _ => {}
123                    }
124                }
125            }
126            KeyCode::Left => {
127                self.row = true;
128            }
129            KeyCode::Right => {
130                self.row = false;
131            }
132            KeyCode::Enter => {
133                if self.text_list.selected == 4 && !self.row {
134                    *self = Self::default();
135                    self.text_list.selected = 4;
136                    self.update_left();
137                } else {
138                    self.row = !self.row;
139                }
140            }
141            _ => {}
142        }
143    }
144
145    pub fn update_left(&mut self) {
146        match self.text_list.selected {
147            0 => {
148                self.select_text_list.items = SearchSettingsSortBy::as_vec();
149                self.select_text_list.selected = SearchSettingsSortBy::value_to_index(self.sort_by);
150            }
151            1 => {
152                self.select_text_list.items = SearchSettingsDate::as_vec();
153                self.select_text_list.selected = SearchSettingsDate::value_to_index(self.date);
154            }
155            2 => {
156                self.select_text_list.items = SearchSettingsDuration::as_vec();
157                self.select_text_list.selected =
158                    SearchSettingsDuration::value_to_index(self.duartion);
159            }
160            3 => {
161                self.select_text_list.items = SearchSettingsType::as_vec();
162                self.select_text_list.selected = SearchSettingsType::value_to_index(self.r#type);
163            }
164            4 => {
165                self.select_text_list.items = vec![String::from("Reset All")];
166                self.select_text_list.selected = 0;
167            }
168            _ => {}
169        }
170    }
171}
172
173pub trait AsUrlString {
174    fn as_url_string(&self) -> String;
175}
176
177#[derive(Debug, Clone, Copy)]
178pub enum SearchSettingsSortBy {
179    Relevance,
180    Rating,
181    Date,
182    ViewCount,
183}
184
185impl AsUrlString for SearchSettingsSortBy {
186    fn as_url_string(&self) -> String {
187        format!(
188            "sort={}",
189            match self {
190                SearchSettingsSortBy::Relevance => String::from("relevance"),
191                SearchSettingsSortBy::Rating => String::from("rating"),
192                SearchSettingsSortBy::Date => String::from("upload_date"),
193                SearchSettingsSortBy::ViewCount => String::from("view_count"),
194            }
195        )
196    }
197}
198
199impl SearchSettingsSortBy {
200    pub fn as_vec() -> Vec<String> {
201        vec![
202            String::from("Relevance"),
203            String::from("Rating"),
204            String::from("Date"),
205            String::from("View Count"),
206        ]
207    }
208
209    pub fn index_to_value(index: usize) -> SearchSettingsSortBy {
210        match index {
211            0 => SearchSettingsSortBy::Relevance,
212            1 => SearchSettingsSortBy::Rating,
213            2 => SearchSettingsSortBy::Date,
214            3 => SearchSettingsSortBy::ViewCount,
215            _ => unreachable!(),
216        }
217    }
218
219    pub fn value_to_index(value: SearchSettingsSortBy) -> usize {
220        match value {
221            SearchSettingsSortBy::Relevance => 0,
222            SearchSettingsSortBy::Rating => 1,
223            SearchSettingsSortBy::Date => 2,
224            SearchSettingsSortBy::ViewCount => 3,
225        }
226    }
227}
228
229#[derive(Debug, Clone, Copy)]
230pub enum SearchSettingsDate {
231    Hour,
232    Day,
233    Week,
234    Month,
235    Year,
236}
237
238impl AsUrlString for SearchSettingsDate {
239    fn as_url_string(&self) -> String {
240        format!(
241            "date={}",
242            match self {
243                SearchSettingsDate::Hour => String::from("hour"),
244                SearchSettingsDate::Day => String::from("today"),
245                SearchSettingsDate::Week => String::from("week"),
246                SearchSettingsDate::Month => String::from("month"),
247                SearchSettingsDate::Year => String::from("year"),
248            }
249        )
250    }
251}
252
253impl SearchSettingsDate {
254    pub fn as_vec() -> Vec<String> {
255        vec![
256            String::from("No Filter"),
257            String::from("Hour"),
258            String::from("Day"),
259            String::from("Week"),
260            String::from("Month"),
261            String::from("Year"),
262        ]
263    }
264
265    pub fn index_to_value(index: usize) -> Option<SearchSettingsDate> {
266        match index {
267            0 => None,
268            1 => Some(SearchSettingsDate::Hour),
269            2 => Some(SearchSettingsDate::Day),
270            3 => Some(SearchSettingsDate::Week),
271            4 => Some(SearchSettingsDate::Month),
272            5 => Some(SearchSettingsDate::Year),
273            _ => unreachable!(),
274        }
275    }
276
277    pub fn value_to_index(value: Option<SearchSettingsDate>) -> usize {
278        match value {
279            None => 0,
280            Some(SearchSettingsDate::Hour) => 1,
281            Some(SearchSettingsDate::Day) => 2,
282            Some(SearchSettingsDate::Week) => 3,
283            Some(SearchSettingsDate::Month) => 4,
284            Some(SearchSettingsDate::Year) => 5,
285        }
286    }
287}
288
289#[derive(Debug, Clone, Copy)]
290pub enum SearchSettingsDuration {
291    Short,
292    Long,
293}
294
295impl AsUrlString for SearchSettingsDuration {
296    fn as_url_string(&self) -> String {
297        format!(
298            "duration={}",
299            match self {
300                SearchSettingsDuration::Short => String::from("short"),
301                SearchSettingsDuration::Long => String::from("long"),
302            }
303        )
304    }
305}
306
307impl SearchSettingsDuration {
308    pub fn as_vec() -> Vec<String> {
309        vec![
310            String::from("No Filter"),
311            String::from("Short"),
312            String::from("Long"),
313        ]
314    }
315
316    pub fn index_to_value(index: usize) -> Option<SearchSettingsDuration> {
317        match index {
318            0 => None,
319            1 => Some(SearchSettingsDuration::Short),
320            2 => Some(SearchSettingsDuration::Long),
321            _ => unreachable!(),
322        }
323    }
324
325    pub fn value_to_index(value: Option<SearchSettingsDuration>) -> usize {
326        match value {
327            None => 0,
328            Some(SearchSettingsDuration::Short) => 1,
329            Some(SearchSettingsDuration::Long) => 2,
330        }
331    }
332}
333
334#[derive(Debug, Clone, Copy)]
335pub enum SearchSettingsType {
336    Video,
337    Channel,
338    Playlist,
339    All,
340}
341
342impl AsUrlString for SearchSettingsType {
343    fn as_url_string(&self) -> String {
344        format!(
345            "type={}",
346            match self {
347                SearchSettingsType::Video => String::from("video"),
348                SearchSettingsType::Channel => String::from("channel"),
349                SearchSettingsType::Playlist => String::from("playlist"),
350                SearchSettingsType::All => String::from("all"),
351            }
352        )
353    }
354}
355
356impl SearchSettingsType {
357    pub fn as_vec() -> Vec<String> {
358        vec![
359            String::from("No Filter"),
360            String::from("Video"),
361            String::from("Channel"),
362            String::from("Playlist"),
363            String::from("All"),
364        ]
365    }
366
367    pub fn index_to_value(index: usize) -> Option<SearchSettingsType> {
368        match index {
369            0 => None,
370            1 => Some(SearchSettingsType::Video),
371            2 => Some(SearchSettingsType::Channel),
372            3 => Some(SearchSettingsType::Playlist),
373            4 => Some(SearchSettingsType::All),
374            _ => unreachable!(),
375        }
376    }
377
378    pub fn value_to_index(value: Option<SearchSettingsType>) -> usize {
379        match value {
380            None => 0,
381            Some(SearchSettingsType::Video) => 1,
382            Some(SearchSettingsType::Channel) => 2,
383            Some(SearchSettingsType::Playlist) => 3,
384            Some(SearchSettingsType::All) => 4,
385        }
386    }
387}