Commit 01ca06be37

01ca06be37b4a7c6c425edb8f9e0fd814d4d23bd

parent: 611b164e42

Unsigned

Siriusmart <71584876+Siriusmart@users.noreply.github.com> · 2022-07-10 13:08 UTC

0.4.3

Layout: unified · split

Cargo.toml +1 −1
@@ -1,6 +1,6 @@
11[package]
22name = "youtube-tui"
3version = "0.4.2"
3version = "0.4.3"
44edition = "2021"
55authors = ["Siriusmart <siriusmart@tuta.io>"]
66description = "An aesthetically pleasing YouTube TUI written in Rust"
README.md +11 −2
@@ -16,10 +16,17 @@ This program is being actively developed and there will be new features coming u
1616
1717![Screenshot](https://cdn.discordapp.com/attachments/906941311142219816/990684947830419526/Screenshot_20220626_192433.png)
1818
19## User manual
20
21[YouTube TUI user manual](https://siriusmart.github.io/youtube-tui)
22
1923> ### What's new in this commit
2024>
21> * `layouts.yml` in `.config/youtube-tui`, now you can customize your screen layout
22> * More improvements to the `render()` method (no more `.clone()` used)
25> * Refresh pages
26> * Fixed cache not being cleared after exit
27> * Key modifiers for key bindings
28> * Image loading can be disabled in main config
29
2330
2431## Installation
2532
@@ -62,6 +69,8 @@ youtube-tui # or path to the binary
6269|Enter|Select/Launch|
6370|Q|Quit the program|
6471
72Check the user manual [here](https://siriusmart.github.io/youtube-tui)
73
6574> ### Note
6675>
6776> These keys only work when nothing is selected. When something is selected, your key presses are passed directly to the "object" you've selected. Press escape (Esc) if you want to deselect.
src/app/app.rs +24 −12
@@ -1,10 +1,10 @@
11use std::collections::LinkedList;
22
33use crate::{
4 structs::{AppHistory, Item, Page, Row, SearchSettings, WatchHistory},
4 structs::{AppHistory, Item, Page, SearchSettings, State, WatchHistory},
55 traits::ItemTrait,
66};
7use crossterm::event::KeyCode;
7use crossterm::event::KeyEvent;
88use invidious::blocking::Client;
99use tui::{
1010 backend::Backend,
@@ -20,7 +20,7 @@ use super::config::{Action, Config};
2020pub struct App {
2121 pub config: Config,
2222 pub page: Page,
23 pub state: Vec<Row>, // Item
23 pub state: State, // Item
2424 pub selectable: Vec<Vec<(usize, usize)>>,
2525 pub hover: Option<(usize, usize)>, // x, y
2626 pub selected: Option<(usize, usize)>,
@@ -33,6 +33,7 @@ pub struct App {
3333 pub watch_history: WatchHistory,
3434 pub search_settings: SearchSettings,
3535 pub search_text: String,
36 pub search_index: usize,
3637 pub page_no: usize,
3738}
3839
@@ -51,11 +52,12 @@ pub struct AppNoState {
5152 pub watch_history: WatchHistory,
5253 pub search_settings: SearchSettings,
5354 pub search_text: String,
55 pub search_index: usize,
5456 pub page_no: usize,
5557}
5658
5759impl AppNoState {
58 pub fn split(app: App) -> (Self, Vec<Row>) {
60 pub fn split(app: App) -> (Self, State) {
5961 let out = Self {
6062 config: app.config,
6163 page: app.page,
@@ -71,12 +73,13 @@ impl AppNoState {
7173 watch_history: app.watch_history,
7274 search_settings: app.search_settings,
7375 search_text: app.search_text,
76 search_index: app.search_index,
7477 page_no: app.page_no,
7578 };
7679 (out, app.state)
7780 }
7881
79 pub fn join(app: Self, state: Vec<Row>) -> App {
82 pub fn join(app: Self, state: State) -> App {
8083 App {
8184 config: app.config,
8285 page: app.page,
@@ -92,6 +95,7 @@ impl AppNoState {
9295 watch_history: app.watch_history,
9396 search_settings: app.search_settings,
9497 search_text: app.search_text,
98 search_index: app.search_index,
9599 page_no: app.page_no,
96100 state,
97101 }
@@ -101,7 +105,7 @@ impl AppNoState {
101105impl Default for App {
102106 fn default() -> Self {
103107 let config = Config::load().unwrap();
104 let state = config.layouts.main_menu.clone().into();
108 let state: State = config.layouts.main_menu.clone().into();
105109 let selectable = App::selectable(&state);
106110 Self {
107111 page: Page::default(),
@@ -119,16 +123,17 @@ impl Default for App {
119123 watch_history: WatchHistory::load(),
120124 search_settings: SearchSettings::default(),
121125 search_text: String::new(),
126 search_index: 0,
122127 page_no: 1,
123128 }
124129 }
125130}
126131
127132impl App {
128 pub fn selectable(state: &Vec<Row>) -> Vec<Vec<(usize, usize)>> {
133 pub fn selectable(state: &State) -> Vec<Vec<(usize, usize)>> {
129134 let mut selectable = Vec::new();
130135
131 for (y, row) in state.iter().enumerate() {
136 for (y, row) in state.0.iter().enumerate() {
132137 let mut row_vec = Vec::new();
133138 for (x, row_item) in row.items.iter().enumerate() {
134139 if match &row_item.item {
@@ -149,16 +154,16 @@ impl App {
149154 selectable
150155 }
151156
152 pub fn key_input(mut self, key: KeyCode) -> App {
157 pub fn key_input(mut self, key: KeyEvent) -> App {
153158 let action = self.config.keybindings.0.get(&key);
154159
155160 if let Some((x, y)) = self.selected {
156161 if action != Some(&Action::Deselect) {
157 let mut item = self.state[y].items[x].item.clone();
162 let mut item = self.state.0[y].items[x].item.clone();
158163 let updated;
159164 (updated, self) = item.key_input(key, self);
160165 if updated {
161 self.state[y].items[x].item = item;
166 self.state.0[y].items[x].item = item;
162167 }
163168
164169 return self;
@@ -171,13 +176,17 @@ impl App {
171176 };
172177
173178 match action {
179 Action::Refresh => {
180 self.state.reset();
181 self.load = true;
182 }
174183 Action::Select => {
175184 if self.hover.is_some() && self.selected.is_none() {
176185 let (mut x, mut y) = self.hover.unwrap();
177186 (x, y) = self.selectable[y][x];
178187
179188 let select;
180 (self, select) = self.state[y]
189 (self, select) = self.state.0[y]
181190 .items
182191 .iter()
183192 .nth(x)
@@ -292,6 +301,7 @@ impl App {
292301 .direction(Direction::Vertical)
293302 .constraints(
294303 self.state
304 .0
295305 .iter()
296306 .map(|row| row.height)
297307 .collect::<Vec<Constraint>>(),
@@ -301,6 +311,7 @@ impl App {
301311 let (mut app, mut state) = AppNoState::split(self);
302312
303313 for (y, (row, row_chunk)) in state
314 .0
304315 .iter_mut()
305316 .zip(vertical_chunks.clone().iter_mut())
306317 .enumerate()
@@ -392,6 +403,7 @@ impl App {
392403 search_settings: self.search_settings,
393404 popup_focus: app_history.popup_focus,
394405 search_text: app_history.search_text,
406 search_index: app_history.search_index,
395407 page_no: app_history.page_no,
396408 };
397409
src/app/config/colors.rs added +1
@@ -0,0 +1 @@
1// TODO
src/app/config/keybindings.rs +183 −21
@@ -1,6 +1,6 @@
11use std::collections::HashMap;
22
3use crossterm::event::KeyCode;
3use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
44use serde::{Deserialize, Serialize};
55
66use crate::traits::ConfigItem;
@@ -19,6 +19,7 @@ pub enum Action {
1919 Exit,
2020 Back,
2121 ClearHistory,
22 Refresh,
2223}
2324
2425#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
@@ -72,41 +73,202 @@ impl Into<KeybindingsConfig> for KeybindingsConfigTransitional {
7273 fn into(self) -> KeybindingsConfig {
7374 let mut out = HashMap::default();
7475
75 for (k, v) in self.0.into_iter() {
76 out.insert(k.into(), v);
76 for ((modifiers, code), v) in self.0.into_iter() {
77 out.insert(
78 KeyEvent {
79 code: code.into(),
80 modifiers: modifiers.into(),
81 },
82 v,
83 );
7784 }
7885
7986 KeybindingsConfig(out)
8087 }
8188}
8289
90#[derive(Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
91pub enum KeyModifierTransitional {
92 Shift,
93 Control,
94 Alt,
95 r#None,
96}
97
98impl Into<KeyModifiers> for KeyModifierTransitional {
99 fn into(self) -> KeyModifiers {
100 match self {
101 Self::Shift => KeyModifiers::SHIFT,
102 Self::Control => KeyModifiers::CONTROL,
103 Self::Alt => KeyModifiers::ALT,
104 Self::None => KeyModifiers::NONE,
105 }
106 }
107}
108
109impl Into<KeyModifiersTransitional> for KeyModifierTransitional {
110 fn into(self) -> KeyModifiersTransitional {
111 KeyModifiersTransitional(vec![self])
112 }
113}
114
115#[derive(Clone, Hash, PartialEq, Eq, Serialize, Deserialize)]
116pub struct KeyModifiersTransitional(Vec<KeyModifierTransitional>);
117
118impl Into<KeyModifiers> for KeyModifiersTransitional {
119 fn into(self) -> KeyModifiers {
120 KeyModifiers::from_bits(
121 self.0
122 .into_iter()
123 .map(|item| Into::<KeyModifiers>::into(item).bits())
124 .sum(),
125 )
126 .unwrap()
127 }
128}
129
130impl Default for KeyModifiersTransitional {
131 fn default() -> Self {
132 Self(vec![KeyModifierTransitional::None])
133 }
134}
135
83136#[derive(Serialize, Deserialize, Clone)]
84pub struct KeybindingsConfigTransitional(pub HashMap<KeyCodeTransitional, Action>);
137pub struct KeybindingsConfigTransitional(
138 pub HashMap<(KeyModifiersTransitional, KeyCodeTransitional), Action>,
139);
85140
86141impl Default for KeybindingsConfigTransitional {
87142 fn default() -> Self {
88143 let mut out = HashMap::default();
89144
90 out.insert(KeyCodeTransitional::Up, Action::Up);
91 out.insert(KeyCodeTransitional::Down, Action::Down);
92 out.insert(KeyCodeTransitional::Left, Action::Left);
93 out.insert(KeyCodeTransitional::Right, Action::Right);
145 out.insert(
146 (KeyModifiersTransitional::default(), KeyCodeTransitional::Up),
147 Action::Up,
148 );
149 out.insert(
150 (
151 KeyModifiersTransitional::default(),
152 KeyCodeTransitional::Down,
153 ),
154 Action::Down,
155 );
156 out.insert(
157 (
158 KeyModifiersTransitional::default(),
159 KeyCodeTransitional::Left,
160 ),
161 Action::Left,
162 );
163 out.insert(
164 (
165 KeyModifiersTransitional::default(),
166 KeyCodeTransitional::Right,
167 ),
168 Action::Right,
169 );
94170
95 out.insert(KeyCodeTransitional::Char('h'), Action::Left);
96 out.insert(KeyCodeTransitional::Char('j'), Action::Down);
97 out.insert(KeyCodeTransitional::Char('k'), Action::Up);
98 out.insert(KeyCodeTransitional::Char('l'), Action::Right);
171 out.insert(
172 (
173 KeyModifiersTransitional::default(),
174 KeyCodeTransitional::Char('h'),
175 ),
176 Action::Left,
177 );
178 out.insert(
179 (
180 KeyModifiersTransitional::default(),
181 KeyCodeTransitional::Char('j'),
182 ),
183 Action::Down,
184 );
185 out.insert(
186 (
187 KeyModifiersTransitional::default(),
188 KeyCodeTransitional::Char('k'),
189 ),
190 Action::Up,
191 );
192 out.insert(
193 (
194 KeyModifiersTransitional::default(),
195 KeyCodeTransitional::Char('l'),
196 ),
197 Action::Right,
198 );
99199
100 out.insert(KeyCodeTransitional::PageUp, Action::FirstItem);
101 out.insert(KeyCodeTransitional::PageDown, Action::LastItem);
200 out.insert(
201 (
202 KeyModifiersTransitional::default(),
203 KeyCodeTransitional::PageUp,
204 ),
205 Action::FirstItem,
206 );
207 out.insert(
208 (
209 KeyModifiersTransitional::default(),
210 KeyCodeTransitional::PageDown,
211 ),
212 Action::LastItem,
213 );
102214
103 out.insert(KeyCodeTransitional::Enter, Action::Select);
104 out.insert(KeyCodeTransitional::Esc, Action::Deselect);
105 out.insert(KeyCodeTransitional::Char('q'), Action::Exit);
215 out.insert(
216 (
217 KeyModifiersTransitional::default(),
218 KeyCodeTransitional::Enter,
219 ),
220 Action::Select,
221 );
222 out.insert(
223 (
224 KeyModifiersTransitional::default(),
225 KeyCodeTransitional::Esc,
226 ),
227 Action::Deselect,
228 );
229 out.insert(
230 (
231 KeyModifiersTransitional::default(),
232 KeyCodeTransitional::Char('q'),
233 ),
234 Action::Exit,
235 );
106236
107 out.insert(KeyCodeTransitional::Home, Action::Home);
108 out.insert(KeyCodeTransitional::Backspace, Action::Back);
109 out.insert(KeyCodeTransitional::End, Action::ClearHistory);
237 out.insert(
238 (
239 KeyModifiersTransitional::default(),
240 KeyCodeTransitional::Home,
241 ),
242 Action::Home,
243 );
244 out.insert(
245 (
246 KeyModifiersTransitional::default(),
247 KeyCodeTransitional::Backspace,
248 ),
249 Action::Back,
250 );
251 out.insert(
252 (
253 KeyModifierTransitional::Alt.into(),
254 KeyCodeTransitional::Left,
255 ),
256 Action::Back,
257 );
258 out.insert(
259 (
260 KeyModifiersTransitional::default(),
261 KeyCodeTransitional::End,
262 ),
263 Action::ClearHistory,
264 );
265 out.insert(
266 (
267 KeyModifierTransitional::Control.into(),
268 KeyCodeTransitional::Char('r'),
269 ),
270 Action::Refresh,
271 );
110272
111273 Self(out)
112274 }
@@ -119,7 +281,7 @@ impl Default for KeybindingsConfig {
119281}
120282
121283#[derive(Clone)]
122pub struct KeybindingsConfig(pub HashMap<KeyCode, Action>);
284pub struct KeybindingsConfig(pub HashMap<KeyEvent, Action>);
123285
124286impl ConfigItem<'_> for KeybindingsConfig {
125287 type Struct = KeybindingsConfigTransitional;
src/app/config/layouts.rs +4 −4
@@ -6,7 +6,7 @@ use crate::{
66 main_menu::{MainMenu, MainMenuItem, MainMenuSelector},
77 search::{Search, SearchItem},
88 },
9 structs::{Item, Row, RowItem},
9 structs::{Item, Row, RowItem, State},
1010 traits::{ConfigItem, PageTrait},
1111 widgets::text_list::TextList,
1212};
@@ -64,9 +64,9 @@ pub struct LayoutConfig {
6464 pub message: String,
6565}
6666
67impl Into<Vec<Row>> for LayoutConfig {
68 fn into(self) -> Vec<Row> {
69 self.layout.into_iter().map(|item| item.into()).collect()
67impl Into<State> for LayoutConfig {
68 fn into(self) -> State {
69 State(self.layout.into_iter().map(|item| item.into()).collect())
7070 }
7171}
7272
src/app/config/main.rs +8
@@ -22,6 +22,10 @@ fn env_default() -> HashMap<String, String> {
2222 out
2323}
2424
25fn display_thumbnails_default() -> bool {
26 true
27}
28
2529#[derive(Clone, Serialize, Deserialize)]
2630pub struct MainConfig {
2731 #[serde(default = "max_watch_history_default")]
@@ -30,6 +34,9 @@ pub struct MainConfig {
3034 #[serde(default = "server_url_default")]
3135 pub server_url: String,
3236
37 #[serde(default = "display_thumbnails_default")]
38 pub display_thumbnails: bool,
39
3340 #[serde(default = "env_default")]
3441 pub env: HashMap<String, String>,
3542}
@@ -39,6 +46,7 @@ impl Default for MainConfig {
3946 Self {
4047 max_watch_history: max_watch_history_default(),
4148 server_url: server_url_default(),
49 display_thumbnails: display_thumbnails_default(),
4250 env: env_default(),
4351 }
4452 }
src/app/config/mod.rs +3
@@ -14,3 +14,6 @@ pub use keybindings::*;
1414
1515mod layouts;
1616pub use layouts::*;
17
18mod colors;
19pub use colors::*;
src/app/pages/channel.rs +11 −5
@@ -1,6 +1,6 @@
11use std::{collections::LinkedList, error::Error};
22
3use crossterm::event::KeyCode;
3use crossterm::event::KeyEvent;
44use serde::{Deserialize, Serialize};
55use tui::{
66 backend::Backend,
@@ -61,7 +61,9 @@ impl ItemTrait for ChannelItem {
6161 channel.authorThumbnails[4].url.clone(),
6262 channel.authorId.clone(),
6363 ));
64 let _ = download_all_thumbnails(linked_list);
64 if app.config.main.display_thumbnails {
65 let _ = download_all_thumbnails(linked_list);
66 }
6567
6668 *displayitem = ChannelDisplayItem::Home(channel.into());
6769 }
@@ -85,7 +87,9 @@ impl ItemTrait for ChannelItem {
8587 .push_back((thumbnail.clone(), item.playlist_id.clone()));
8688 }
8789 }
88 let _ = download_all_thumbnails(linked_list);
90 if app.config.main.display_thumbnails {
91 let _ = download_all_thumbnails(linked_list);
92 }
8993
9094 *displayitem = ChannelDisplayItem::Playlists(playlists, textlist);
9195 }
@@ -108,8 +112,10 @@ impl ItemTrait for ChannelItem {
108112 linked_list
109113 .push_back((item.video_thumbnail.clone(), item.video_id.clone()));
110114 }
111 let _ = download_all_thumbnails(linked_list);
112115
116 if app.config.main.display_thumbnails {
117 let _ = download_all_thumbnails(linked_list);
118 }
113119 *displayitem = ChannelDisplayItem::Videos(videos, textlist);
114120 }
115121 }
@@ -277,7 +283,7 @@ impl ItemTrait for ChannelItem {
277283 out
278284 }
279285
280 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
286 fn key_input(&mut self, key: KeyEvent, app: App) -> (bool, App) {
281287 let action = match app.config.keybindings.0.get(&key) {
282288 Some(action) => action,
283289 None => return (false, app),
src/app/pages/global.rs +28 −6
@@ -1,6 +1,6 @@
11use std::fmt;
22
3use crossterm::event::KeyCode;
3use crossterm::event::{KeyCode, KeyEvent};
44use serde::{Deserialize, Serialize};
55use tui::{
66 backend::Backend,
@@ -70,14 +70,32 @@ impl ItemTrait for GlobalItem {
7070 }
7171 }
7272
73 fn key_input(&mut self, key: KeyCode, mut app: App) -> (bool, App) {
73 fn key_input(&mut self, key: KeyEvent, mut app: App) -> (bool, App) {
7474 match self {
75 GlobalItem::SearchBar => match key {
75 GlobalItem::SearchBar => match key.code {
7676 KeyCode::Char(c) => {
77 app.search_text.push(c);
77 app.search_text.insert(app.search_index, c);
78 app.search_index += 1;
7879 }
7980 KeyCode::Backspace => {
80 app.search_text.pop();
81 if app.search_index != 0 && app.search_text.len() != 0 {
82 app.search_text = format!(
83 "{}{}",
84 &app.search_text[0..app.search_index - 1],
85 &app.search_text[app.search_index..app.search_text.len()]
86 );
87 app.search_index -= 1;
88 };
89 }
90 KeyCode::Left => {
91 if app.search_index > 0 {
92 app.search_index -= 1;
93 }
94 }
95 KeyCode::Right => {
96 if app.search_index < app.search_text.len() {
97 app.search_index += 1;
98 }
8199 }
82100 KeyCode::Enter => {
83101 if app.search_text.len() == 0 {
@@ -86,6 +104,7 @@ impl ItemTrait for GlobalItem {
86104 let state = app.config.layouts.search.clone().into();
87105 let mut history = app.history.clone();
88106 let search_text = app.search_text.clone();
107 let search_index = app.search_index;
89108 let search_settings = app.search_settings.clone();
90109 history.push(app.into());
91110
@@ -97,6 +116,7 @@ impl ItemTrait for GlobalItem {
97116 selectable: App::selectable(&state),
98117 state,
99118 search_text,
119 search_index,
100120 search_settings,
101121 load: true,
102122 ..Default::default()
@@ -143,7 +163,9 @@ impl ItemTrait for GlobalItem {
143163 .title_alignment(Alignment::Center);
144164 let mut text = out.1.search_text.clone();
145165 if selected {
146 text.push('█');
166 text.push(' ');
167 text.replace_range(out.1.search_index..out.1.search_index + 1, "█");
168 // text = format!("{}█{}", &text[0..out.1.search_index], &text[out.1.search_index+1..text.len()]);
147169 }
148170 let paragraph = Paragraph::new(text).block(block);
149171 frame.render_widget(paragraph, rect);
src/app/pages/item_info.rs +21 −13
@@ -14,7 +14,7 @@ use crate::{
1414 traits::{ItemTrait, PageTrait},
1515 widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList},
1616};
17use crossterm::event::KeyCode;
17use crossterm::event::KeyEvent;
1818use tui::{
1919 backend::Backend,
2020 layout::Rect,
@@ -65,6 +65,10 @@ impl PlayListDisplayView {
6565}
6666
6767impl ItemTrait for ItemInfoItem {
68 fn reset(&mut self) {
69 // panic!("Got here!");
70 *self = Self::Unknown;
71 }
6872 fn select(&mut self, app: App) -> (App, bool) {
6973 let selected = true;
7074
@@ -75,7 +79,7 @@ impl ItemTrait for ItemInfoItem {
7579 true
7680 }
7781
78 fn key_input(&mut self, key: KeyCode, mut app: App) -> (bool, App) {
82 fn key_input(&mut self, key: KeyEvent, mut app: App) -> (bool, App) {
7983 let action = match app.config.keybindings.0.get(&key) {
8084 Some(action) => *action,
8185 None => return (false, app),
@@ -268,10 +272,12 @@ impl ItemTrait for ItemInfoItem {
268272 DisplayItem::Video(id) => {
269273 let mut list = TextList::default();
270274 let video: FullVideo = app.client.video(id, None)?.into();
271 let _ = download_all_thumbnails(LinkedList::from([(
272 video.video_thumbnail.clone(),
273 video.video_id.clone(),
274 )]));
275 if app.config.main.display_thumbnails {
276 let _ = download_all_thumbnails(LinkedList::from([(
277 video.video_thumbnail.clone(),
278 video.video_id.clone(),
279 )]));
280 }
275281 let commands = app
276282 .config
277283 .page_commands
@@ -301,13 +307,15 @@ impl ItemTrait for ItemInfoItem {
301307 let mut videos_text_list = TextList::default();
302308 let mut playlist_text_list = TextList::default();
303309 let playlist: FullPlayList = app.client.playlist(id, None)?.into();
304 let _ = download_all_thumbnails(
305 playlist
306 .videos
307 .iter()
308 .map(|v| (v.video_thumbnail.clone(), v.video_id.clone()))
309 .collect(),
310 );
310 if app.config.main.display_thumbnails {
311 let _ = download_all_thumbnails(
312 playlist
313 .videos
314 .iter()
315 .map(|v| (v.video_thumbnail.clone(), v.video_id.clone()))
316 .collect(),
317 );
318 }
311319
312320 videos_text_list.items = vec![String::from("Swtich view")];
313321 videos_text_list
src/app/pages/main_menu.rs +33 −22
@@ -14,7 +14,7 @@ use crate::{
1414 traits::{ItemTrait, PageTrait},
1515 widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList},
1616};
17use crossterm::event::KeyCode;
17use crossterm::event::KeyEvent;
1818use serde::{Deserialize, Serialize};
1919use tui::{
2020 backend::Backend,
@@ -47,16 +47,18 @@ impl ItemTrait for MainMenuItem {
4747 .map(|t| ListItem::MiniVideo(t.into()))
4848 .collect();
4949
50 download_all_thumbnails(
51 list.iter()
52 .map(|t| match t {
53 ListItem::MiniVideo(v) => {
54 (v.video_thumbnail.clone(), v.video_id.clone())
55 }
56 _ => unreachable!(),
57 })
58 .collect(),
59 )?;
50 if app.config.main.display_thumbnails {
51 let _ = download_all_thumbnails(
52 list.iter()
53 .map(|t| match t {
54 ListItem::MiniVideo(v) => {
55 (v.video_thumbnail.clone(), v.video_id.clone())
56 }
57 _ => unreachable!(),
58 })
59 .collect(),
60 );
61 }
6062
6163 if let Some((video_list, text_list, _)) = enum_items {
6264 *text_list = TextList::default();
@@ -79,16 +81,18 @@ impl ItemTrait for MainMenuItem {
7981 .map(|t| ListItem::MiniVideo(t.into()))
8082 .collect();
8183
82 let _ = download_all_thumbnails(
83 list.iter()
84 .map(|t| match t {
85 ListItem::MiniVideo(v) => {
86 (v.video_thumbnail.clone(), v.video_id.clone())
87 }
88 _ => unreachable!(),
89 })
90 .collect(),
91 );
84 if app.config.main.display_thumbnails {
85 let _ = download_all_thumbnails(
86 list.iter()
87 .map(|t| match t {
88 ListItem::MiniVideo(v) => {
89 (v.video_thumbnail.clone(), v.video_id.clone())
90 }
91 _ => unreachable!(),
92 })
93 .collect(),
94 );
95 }
9296
9397 let mut text_list = TextList::default();
9498
@@ -220,6 +224,13 @@ impl ItemTrait for MainMenuItem {
220224 }
221225 out
222226 }
227
228 fn reset(&mut self) {
229 if let Self::VideoList(something) = self {
230 *something = None;
231 }
232 }
233
223234 fn select(&mut self, mut app: App) -> (App, bool) {
224235 let selected = match self {
225236 MainMenuItem::SeletorTab(selector) => {
@@ -241,7 +252,7 @@ impl ItemTrait for MainMenuItem {
241252 true
242253 }
243254
244 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
255 fn key_input(&mut self, key: KeyEvent, app: App) -> (bool, App) {
245256 match self {
246257 MainMenuItem::VideoList(Some((list, textlist, _))) => {
247258 let action = match app.config.keybindings.0.get(&key) {
src/app/pages/search.rs +5 −3
@@ -1,6 +1,6 @@
11use std::{collections::LinkedList, error::Error};
22
3use crossterm::event::KeyCode;
3use crossterm::event::KeyEvent;
44use invidious::structs::hidden::SearchItem as InvidiousSearchItem;
55use tui::{
66 backend::Backend,
@@ -45,7 +45,7 @@ impl ItemTrait for SearchItem {
4545 true
4646 }
4747
48 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
48 fn key_input(&mut self, key: KeyEvent, app: App) -> (bool, App) {
4949 let action = match app.config.keybindings.0.get(&key) {
5050 Some(action) => action,
5151 None => return (false, app),
@@ -275,7 +275,9 @@ impl ItemTrait for SearchItem {
275275 }
276276 }
277277
278 let _ = download_all_thumbnails(thumbnails);
278 if app.config.main.display_thumbnails {
279 let _ = download_all_thumbnails(thumbnails);
280 }
279281
280282 *results = Some(items);
281283 }
src/main.rs +8 −8
@@ -10,7 +10,7 @@ use tui::{
1010};
1111use youtube_tui::{
1212 app::{app::App, config::Action},
13 structs::{Row, RowItem},
13 structs::{Row, RowItem, State},
1414};
1515
1616fn main() -> Result<(), Box<dyn Error>> {
@@ -55,7 +55,7 @@ fn run_app<B: Backend>(mut terminal: &mut Terminal<B>, mut app: App) -> Result<(
5555 app = hold.0;
5656 hold.1?;
5757 let mut new_state = Vec::new();
58 for row in app.state.iter() {
58 for row in app.state.0.iter() {
5959 let mut row_vec = Vec::new();
6060 for row_item in row.items.iter() {
6161 match row_item.item.load_item(&app, &mut watch_history) {
@@ -82,7 +82,7 @@ fn run_app<B: Backend>(mut terminal: &mut Terminal<B>, mut app: App) -> Result<(
8282 }
8383
8484 app.watch_history = watch_history;
85 app.state = new_state;
85 app.state = State(new_state);
8686
8787 app.load = false;
8888 terminal.clear()?;
@@ -92,7 +92,7 @@ fn run_app<B: Backend>(mut terminal: &mut Terminal<B>, mut app: App) -> Result<(
9292 match event::read()? {
9393 event::Event::Key(key) => {
9494 if app.selected.is_none() {
95 let action = match app.config.keybindings.0.get(&key.code) {
95 let action = match app.config.keybindings.0.get(&key) {
9696 Some(action) => *action,
9797 None => continue,
9898 };
@@ -122,7 +122,7 @@ fn run_app<B: Backend>(mut terminal: &mut Terminal<B>, mut app: App) -> Result<(
122122 }
123123 }
124124
125 app = app.key_input(key.code);
125 app = app.key_input(key);
126126 app.render = true;
127127 }
128128 event::Event::Resize(_, _) => {
@@ -158,9 +158,10 @@ fn init() -> Result<(), Box<dyn Error>> {
158158 let mut dir = home_dir.clone();
159159
160160 dir.push(".cache");
161 if !dir.exists() {
162 fs::create_dir(&dir)?;
161 if dir.exists() {
162 fs::remove_dir_all(&dir)?;
163163 }
164 fs::create_dir(&dir)?;
164165
165166 dir.push("youtube-tui");
166167 if !dir.exists() {
@@ -235,7 +236,6 @@ fn exit() -> Result<(), Box<dyn Error>> {
235236
236237 dir.push(".cache");
237238 dir.push("youtube-tui");
238 dir.push("cache");
239239
240240 if dir.exists() {
241241 fs::remove_dir_all(&dir)?;
src/structs/apphistory.rs +7 −7
@@ -1,16 +1,13 @@
11use invidious::blocking::Client;
22
3use crate::{
4 app::app::App,
5 structs::{Page, Row},
6};
3use crate::{app::app::App, structs::Page};
74
8use super::SearchSettings;
5use super::{SearchSettings, State};
96
107#[derive(Clone)]
118pub struct AppHistory {
129 pub page: Page,
13 pub state: Vec<Row>, // Item
10 pub state: State, // Item
1411 pub selectable: Vec<Vec<(usize, usize)>>,
1512 pub hover: Option<(usize, usize)>, // x, y
1613 pub selected: Option<(usize, usize)>,
@@ -21,6 +18,7 @@ pub struct AppHistory {
2118 pub popup_focus: bool,
2219 pub search_text: String,
2320 pub search_settings: SearchSettings,
21 pub search_index: usize,
2422 pub page_no: usize,
2523}
2624
@@ -31,7 +29,8 @@ impl From<App> for AppHistory {
3129 state: original.state,
3230 selectable: original.selectable,
3331 hover: original.hover,
34 selected: original.selected,
32 // selected: original.selected,
33 selected: None,
3534 client: original.client,
3635 message: original.message,
3736 load: original.load,
@@ -39,6 +38,7 @@ impl From<App> for AppHistory {
3938 popup_focus: original.popup_focus,
4039 search_text: original.search_text,
4140 search_settings: original.search_settings,
41 search_index: original.search_index,
4242 page_no: original.page_no,
4343 }
4444 }
src/structs/search_settings.rs +3 −3
@@ -1,4 +1,4 @@
1use crossterm::event::KeyCode;
1use crossterm::event::KeyEvent;
22
33use crate::{
44 app::config::{Action, Config},
@@ -66,8 +66,8 @@ impl SearchSettings {
6666 vec
6767 }
6868
69 pub fn key_input(&mut self, keycode: KeyCode, config: &Config) {
70 let action = match config.keybindings.0.get(&keycode) {
69 pub fn key_input(&mut self, key: KeyEvent, config: &Config) {
70 let action = match config.keybindings.0.get(&key) {
7171 Some(action) => *action,
7272 None => return,
7373 };
src/structs/states.rs +25 −2
@@ -1,4 +1,4 @@
1use crossterm::event::KeyCode;
1use crossterm::event::KeyEvent;
22use std::error::Error;
33use tui::{
44 backend::Backend,
@@ -19,6 +19,19 @@ use crate::{
1919
2020use super::WatchHistory;
2121
22#[derive(Clone)]
23pub struct State(pub Vec<Row>);
24
25impl State {
26 pub fn reset(&mut self) {
27 for row in self.0.iter_mut() {
28 for rowitem in row.items.iter_mut() {
29 rowitem.item.reset();
30 }
31 }
32 }
33}
34
2235#[derive(Clone)]
2336pub enum Item {
2437 Global(GlobalItem),
@@ -29,7 +42,7 @@ pub enum Item {
2942}
3043
3144impl Item {
32 pub fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
45 pub fn key_input(&mut self, key: KeyEvent, app: App) -> (bool, App) {
3346 match self {
3447 Item::Global(item) => item.key_input(key, app),
3548 Item::MainMenu(item) => item.key_input(key, app),
@@ -39,6 +52,16 @@ impl Item {
3952 }
4053 }
4154
55 pub fn reset(&mut self) {
56 match self {
57 Item::Global(item) => item.reset(),
58 Item::MainMenu(item) => item.reset(),
59 Item::ItemInfo(item) => item.reset(),
60 Item::Search(item) => item.reset(),
61 Item::Channel(item) => item.reset(),
62 };
63 }
64
4265 pub fn render_item<B: Backend>(
4366 &mut self,
4467 frame: &mut Frame<B>,
src/traits/item.rs +3 −2
@@ -4,11 +4,11 @@ use crate::{
44 app::app::{App, AppNoState},
55 structs::{Item, WatchHistory},
66};
7use crossterm::event::KeyCode;
7use crossterm::event::KeyEvent;
88use tui::{backend::Backend, layout::Rect, Frame};
99
1010pub trait ItemTrait {
11 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App);
11 fn key_input(&mut self, key: KeyEvent, app: App) -> (bool, App);
1212 fn render_item<B: Backend>(
1313 &mut self,
1414 frame: &mut Frame<B>,
@@ -26,4 +26,5 @@ pub trait ItemTrait {
2626 app: &App,
2727 watch_history: &mut WatchHistory,
2828 ) -> Result<Item, Box<dyn Error>>;
29 fn reset(&mut self) {}
2930}