src/structs/search_settings.rs

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

395 lines · 11786 bytes

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