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 @@
1[package] 1[package]
2name = "youtube-tui" 2name = "youtube-tui"
3version = "0.4.2" 3version = "0.4.3"
4edition = "2021" 4edition = "2021"
5authors = ["Siriusmart <siriusmart@tuta.io>"] 5authors = ["Siriusmart <siriusmart@tuta.io>"]
6description = "An aesthetically pleasing YouTube TUI written in Rust" 6description = "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
16 16
17![Screenshot](https://cdn.discordapp.com/attachments/906941311142219816/990684947830419526/Screenshot_20220626_192433.png) 17![Screenshot](https://cdn.discordapp.com/attachments/906941311142219816/990684947830419526/Screenshot_20220626_192433.png)
18 18
19## User manual
20
21[YouTube TUI user manual](https://siriusmart.github.io/youtube-tui)
22
19> ### What's new in this commit 23> ### What's new in this commit
20> 24>
21> * `layouts.yml` in `.config/youtube-tui`, now you can customize your screen layout 25> * Refresh pages
22> * More improvements to the `render()` method (no more `.clone()` used) 26> * Fixed cache not being cleared after exit
27> * Key modifiers for key bindings
28> * Image loading can be disabled in main config
29
23 30
24## Installation 31## Installation
25 32
@@ -62,6 +69,8 @@ youtube-tui # or path to the binary
62|Enter|Select/Launch| 69|Enter|Select/Launch|
63|Q|Quit the program| 70|Q|Quit the program|
64 71
72Check the user manual [here](https://siriusmart.github.io/youtube-tui)
73
65> ### Note 74> ### Note
66> 75>
67> 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. 76> 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 @@
1use std::collections::LinkedList; 1use std::collections::LinkedList;
2 2
3use crate::{ 3use crate::{
4 structs::{AppHistory, Item, Page, Row, SearchSettings, WatchHistory}, 4 structs::{AppHistory, Item, Page, SearchSettings, State, WatchHistory},
5 traits::ItemTrait, 5 traits::ItemTrait,
6}; 6};
7use crossterm::event::KeyCode; 7use crossterm::event::KeyEvent;
8use invidious::blocking::Client; 8use invidious::blocking::Client;
9use tui::{ 9use tui::{
10 backend::Backend, 10 backend::Backend,
@@ -20,7 +20,7 @@ use super::config::{Action, Config};
20pub struct App { 20pub struct App {
21 pub config: Config, 21 pub config: Config,
22 pub page: Page, 22 pub page: Page,
23 pub state: Vec<Row>, // Item 23 pub state: State, // Item
24 pub selectable: Vec<Vec<(usize, usize)>>, 24 pub selectable: Vec<Vec<(usize, usize)>>,
25 pub hover: Option<(usize, usize)>, // x, y 25 pub hover: Option<(usize, usize)>, // x, y
26 pub selected: Option<(usize, usize)>, 26 pub selected: Option<(usize, usize)>,
@@ -33,6 +33,7 @@ pub struct App {
33 pub watch_history: WatchHistory, 33 pub watch_history: WatchHistory,
34 pub search_settings: SearchSettings, 34 pub search_settings: SearchSettings,
35 pub search_text: String, 35 pub search_text: String,
36 pub search_index: usize,
36 pub page_no: usize, 37 pub page_no: usize,
37} 38}
38 39
@@ -51,11 +52,12 @@ pub struct AppNoState {
51 pub watch_history: WatchHistory, 52 pub watch_history: WatchHistory,
52 pub search_settings: SearchSettings, 53 pub search_settings: SearchSettings,
53 pub search_text: String, 54 pub search_text: String,
55 pub search_index: usize,
54 pub page_no: usize, 56 pub page_no: usize,
55} 57}
56 58
57impl AppNoState { 59impl AppNoState {
58 pub fn split(app: App) -> (Self, Vec<Row>) { 60 pub fn split(app: App) -> (Self, State) {
59 let out = Self { 61 let out = Self {
60 config: app.config, 62 config: app.config,
61 page: app.page, 63 page: app.page,
@@ -71,12 +73,13 @@ impl AppNoState {
71 watch_history: app.watch_history, 73 watch_history: app.watch_history,
72 search_settings: app.search_settings, 74 search_settings: app.search_settings,
73 search_text: app.search_text, 75 search_text: app.search_text,
76 search_index: app.search_index,
74 page_no: app.page_no, 77 page_no: app.page_no,
75 }; 78 };
76 (out, app.state) 79 (out, app.state)
77 } 80 }
78 81
79 pub fn join(app: Self, state: Vec<Row>) -> App { 82 pub fn join(app: Self, state: State) -> App {
80 App { 83 App {
81 config: app.config, 84 config: app.config,
82 page: app.page, 85 page: app.page,
@@ -92,6 +95,7 @@ impl AppNoState {
92 watch_history: app.watch_history, 95 watch_history: app.watch_history,
93 search_settings: app.search_settings, 96 search_settings: app.search_settings,
94 search_text: app.search_text, 97 search_text: app.search_text,
98 search_index: app.search_index,
95 page_no: app.page_no, 99 page_no: app.page_no,
96 state, 100 state,
97 } 101 }
@@ -101,7 +105,7 @@ impl AppNoState {
101impl Default for App { 105impl Default for App {
102 fn default() -> Self { 106 fn default() -> Self {
103 let config = Config::load().unwrap(); 107 let config = Config::load().unwrap();
104 let state = config.layouts.main_menu.clone().into(); 108 let state: State = config.layouts.main_menu.clone().into();
105 let selectable = App::selectable(&state); 109 let selectable = App::selectable(&state);
106 Self { 110 Self {
107 page: Page::default(), 111 page: Page::default(),
@@ -119,16 +123,17 @@ impl Default for App {
119 watch_history: WatchHistory::load(), 123 watch_history: WatchHistory::load(),
120 search_settings: SearchSettings::default(), 124 search_settings: SearchSettings::default(),
121 search_text: String::new(), 125 search_text: String::new(),
126 search_index: 0,
122 page_no: 1, 127 page_no: 1,
123 } 128 }
124 } 129 }
125} 130}
126 131
127impl App { 132impl App {
128 pub fn selectable(state: &Vec<Row>) -> Vec<Vec<(usize, usize)>> { 133 pub fn selectable(state: &State) -> Vec<Vec<(usize, usize)>> {
129 let mut selectable = Vec::new(); 134 let mut selectable = Vec::new();
130 135
131 for (y, row) in state.iter().enumerate() { 136 for (y, row) in state.0.iter().enumerate() {
132 let mut row_vec = Vec::new(); 137 let mut row_vec = Vec::new();
133 for (x, row_item) in row.items.iter().enumerate() { 138 for (x, row_item) in row.items.iter().enumerate() {
134 if match &row_item.item { 139 if match &row_item.item {
@@ -149,16 +154,16 @@ impl App {
149 selectable 154 selectable
150 } 155 }
151 156
152 pub fn key_input(mut self, key: KeyCode) -> App { 157 pub fn key_input(mut self, key: KeyEvent) -> App {
153 let action = self.config.keybindings.0.get(&key); 158 let action = self.config.keybindings.0.get(&key);
154 159
155 if let Some((x, y)) = self.selected { 160 if let Some((x, y)) = self.selected {
156 if action != Some(&Action::Deselect) { 161 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();
158 let updated; 163 let updated;
159 (updated, self) = item.key_input(key, self); 164 (updated, self) = item.key_input(key, self);
160 if updated { 165 if updated {
161 self.state[y].items[x].item = item; 166 self.state.0[y].items[x].item = item;
162 } 167 }
163 168
164 return self; 169 return self;
@@ -171,13 +176,17 @@ impl App {
171 }; 176 };
172 177
173 match action { 178 match action {
179 Action::Refresh => {
180 self.state.reset();
181 self.load = true;
182 }
174 Action::Select => { 183 Action::Select => {
175 if self.hover.is_some() && self.selected.is_none() { 184 if self.hover.is_some() && self.selected.is_none() {
176 let (mut x, mut y) = self.hover.unwrap(); 185 let (mut x, mut y) = self.hover.unwrap();
177 (x, y) = self.selectable[y][x]; 186 (x, y) = self.selectable[y][x];
178 187
179 let select; 188 let select;
180 (self, select) = self.state[y] 189 (self, select) = self.state.0[y]
181 .items 190 .items
182 .iter() 191 .iter()
183 .nth(x) 192 .nth(x)
@@ -292,6 +301,7 @@ impl App {
292 .direction(Direction::Vertical) 301 .direction(Direction::Vertical)
293 .constraints( 302 .constraints(
294 self.state 303 self.state
304 .0
295 .iter() 305 .iter()
296 .map(|row| row.height) 306 .map(|row| row.height)
297 .collect::<Vec<Constraint>>(), 307 .collect::<Vec<Constraint>>(),
@@ -301,6 +311,7 @@ impl App {
301 let (mut app, mut state) = AppNoState::split(self); 311 let (mut app, mut state) = AppNoState::split(self);
302 312
303 for (y, (row, row_chunk)) in state 313 for (y, (row, row_chunk)) in state
314 .0
304 .iter_mut() 315 .iter_mut()
305 .zip(vertical_chunks.clone().iter_mut()) 316 .zip(vertical_chunks.clone().iter_mut())
306 .enumerate() 317 .enumerate()
@@ -392,6 +403,7 @@ impl App {
392 search_settings: self.search_settings, 403 search_settings: self.search_settings,
393 popup_focus: app_history.popup_focus, 404 popup_focus: app_history.popup_focus,
394 search_text: app_history.search_text, 405 search_text: app_history.search_text,
406 search_index: app_history.search_index,
395 page_no: app_history.page_no, 407 page_no: app_history.page_no,
396 }; 408 };
397 409
src/app/config/colors.rs added +1
@@ -0,0 +1 @@
1// TODO
src/app/config/keybindings.rs +183 −21
@@ -1,6 +1,6 @@
1use std::collections::HashMap; 1use std::collections::HashMap;
2 2
3use crossterm::event::KeyCode; 3use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
4use serde::{Deserialize, Serialize}; 4use serde::{Deserialize, Serialize};
5 5
6use crate::traits::ConfigItem; 6use crate::traits::ConfigItem;
@@ -19,6 +19,7 @@ pub enum Action {
19 Exit, 19 Exit,
20 Back, 20 Back,
21 ClearHistory, 21 ClearHistory,
22 Refresh,
22} 23}
23 24
24#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] 25#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Hash)]
@@ -72,41 +73,202 @@ impl Into<KeybindingsConfig> for KeybindingsConfigTransitional {
72 fn into(self) -> KeybindingsConfig { 73 fn into(self) -> KeybindingsConfig {
73 let mut out = HashMap::default(); 74 let mut out = HashMap::default();
74 75
75 for (k, v) in self.0.into_iter() { 76 for ((modifiers, code), v) in self.0.into_iter() {
76 out.insert(k.into(), v); 77 out.insert(
78 KeyEvent {
79 code: code.into(),
80 modifiers: modifiers.into(),
81 },
82 v,
83 );
77 } 84 }
78 85
79 KeybindingsConfig(out) 86 KeybindingsConfig(out)
80 } 87 }
81} 88}
82 89
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
83#[derive(Serialize, Deserialize, Clone)] 136#[derive(Serialize, Deserialize, Clone)]
84pub struct KeybindingsConfigTransitional(pub HashMap<KeyCodeTransitional, Action>); 137pub struct KeybindingsConfigTransitional(
138 pub HashMap<(KeyModifiersTransitional, KeyCodeTransitional), Action>,
139);
85 140
86impl Default for KeybindingsConfigTransitional { 141impl Default for KeybindingsConfigTransitional {
87 fn default() -> Self { 142 fn default() -> Self {
88 let mut out = HashMap::default(); 143 let mut out = HashMap::default();
89 144
90 out.insert(KeyCodeTransitional::Up, Action::Up); 145 out.insert(
91 out.insert(KeyCodeTransitional::Down, Action::Down); 146 (KeyModifiersTransitional::default(), KeyCodeTransitional::Up),
92 out.insert(KeyCodeTransitional::Left, Action::Left); 147 Action::Up,
93 out.insert(KeyCodeTransitional::Right, Action::Right); 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 );
94 170
95 out.insert(KeyCodeTransitional::Char('h'), Action::Left); 171 out.insert(
96 out.insert(KeyCodeTransitional::Char('j'), Action::Down); 172 (
97 out.insert(KeyCodeTransitional::Char('k'), Action::Up); 173 KeyModifiersTransitional::default(),
98 out.insert(KeyCodeTransitional::Char('l'), Action::Right); 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 );
99 199
100 out.insert(KeyCodeTransitional::PageUp, Action::FirstItem); 200 out.insert(
101 out.insert(KeyCodeTransitional::PageDown, Action::LastItem); 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 );
102 214
103 out.insert(KeyCodeTransitional::Enter, Action::Select); 215 out.insert(
104 out.insert(KeyCodeTransitional::Esc, Action::Deselect); 216 (
105 out.insert(KeyCodeTransitional::Char('q'), Action::Exit); 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 );
106 236
107 out.insert(KeyCodeTransitional::Home, Action::Home); 237 out.insert(
108 out.insert(KeyCodeTransitional::Backspace, Action::Back); 238 (
109 out.insert(KeyCodeTransitional::End, Action::ClearHistory); 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 );
110 272
111 Self(out) 273 Self(out)
112 } 274 }
@@ -119,7 +281,7 @@ impl Default for KeybindingsConfig {
119} 281}
120 282
121#[derive(Clone)] 283#[derive(Clone)]
122pub struct KeybindingsConfig(pub HashMap<KeyCode, Action>); 284pub struct KeybindingsConfig(pub HashMap<KeyEvent, Action>);
123 285
124impl ConfigItem<'_> for KeybindingsConfig { 286impl ConfigItem<'_> for KeybindingsConfig {
125 type Struct = KeybindingsConfigTransitional; 287 type Struct = KeybindingsConfigTransitional;
src/app/config/layouts.rs +4 −4
@@ -6,7 +6,7 @@ use crate::{
6 main_menu::{MainMenu, MainMenuItem, MainMenuSelector}, 6 main_menu::{MainMenu, MainMenuItem, MainMenuSelector},
7 search::{Search, SearchItem}, 7 search::{Search, SearchItem},
8 }, 8 },
9 structs::{Item, Row, RowItem}, 9 structs::{Item, Row, RowItem, State},
10 traits::{ConfigItem, PageTrait}, 10 traits::{ConfigItem, PageTrait},
11 widgets::text_list::TextList, 11 widgets::text_list::TextList,
12}; 12};
@@ -64,9 +64,9 @@ pub struct LayoutConfig {
64 pub message: String, 64 pub message: String,
65} 65}
66 66
67impl Into<Vec<Row>> for LayoutConfig { 67impl Into<State> for LayoutConfig {
68 fn into(self) -> Vec<Row> { 68 fn into(self) -> State {
69 self.layout.into_iter().map(|item| item.into()).collect() 69 State(self.layout.into_iter().map(|item| item.into()).collect())
70 } 70 }
71} 71}
72 72
src/app/config/main.rs +8
@@ -22,6 +22,10 @@ fn env_default() -> HashMap<String, String> {
22 out 22 out
23} 23}
24 24
25fn display_thumbnails_default() -> bool {
26 true
27}
28
25#[derive(Clone, Serialize, Deserialize)] 29#[derive(Clone, Serialize, Deserialize)]
26pub struct MainConfig { 30pub struct MainConfig {
27 #[serde(default = "max_watch_history_default")] 31 #[serde(default = "max_watch_history_default")]
@@ -30,6 +34,9 @@ pub struct MainConfig {
30 #[serde(default = "server_url_default")] 34 #[serde(default = "server_url_default")]
31 pub server_url: String, 35 pub server_url: String,
32 36
37 #[serde(default = "display_thumbnails_default")]
38 pub display_thumbnails: bool,
39
33 #[serde(default = "env_default")] 40 #[serde(default = "env_default")]
34 pub env: HashMap<String, String>, 41 pub env: HashMap<String, String>,
35} 42}
@@ -39,6 +46,7 @@ impl Default for MainConfig {
39 Self { 46 Self {
40 max_watch_history: max_watch_history_default(), 47 max_watch_history: max_watch_history_default(),
41 server_url: server_url_default(), 48 server_url: server_url_default(),
49 display_thumbnails: display_thumbnails_default(),
42 env: env_default(), 50 env: env_default(),
43 } 51 }
44 } 52 }
src/app/config/mod.rs +3
@@ -14,3 +14,6 @@ pub use keybindings::*;
14 14
15mod layouts; 15mod layouts;
16pub use layouts::*; 16pub use layouts::*;
17
18mod colors;
19pub use colors::*;
src/app/pages/channel.rs +11 −5
@@ -1,6 +1,6 @@
1use std::{collections::LinkedList, error::Error}; 1use std::{collections::LinkedList, error::Error};
2 2
3use crossterm::event::KeyCode; 3use crossterm::event::KeyEvent;
4use serde::{Deserialize, Serialize}; 4use serde::{Deserialize, Serialize};
5use tui::{ 5use tui::{
6 backend::Backend, 6 backend::Backend,
@@ -61,7 +61,9 @@ impl ItemTrait for ChannelItem {
61 channel.authorThumbnails[4].url.clone(), 61 channel.authorThumbnails[4].url.clone(),
62 channel.authorId.clone(), 62 channel.authorId.clone(),
63 )); 63 ));
64 let _ = download_all_thumbnails(linked_list); 64 if app.config.main.display_thumbnails {
65 let _ = download_all_thumbnails(linked_list);
66 }
65 67
66 *displayitem = ChannelDisplayItem::Home(channel.into()); 68 *displayitem = ChannelDisplayItem::Home(channel.into());
67 } 69 }
@@ -85,7 +87,9 @@ impl ItemTrait for ChannelItem {
85 .push_back((thumbnail.clone(), item.playlist_id.clone())); 87 .push_back((thumbnail.clone(), item.playlist_id.clone()));
86 } 88 }
87 } 89 }
88 let _ = download_all_thumbnails(linked_list); 90 if app.config.main.display_thumbnails {
91 let _ = download_all_thumbnails(linked_list);
92 }
89 93
90 *displayitem = ChannelDisplayItem::Playlists(playlists, textlist); 94 *displayitem = ChannelDisplayItem::Playlists(playlists, textlist);
91 } 95 }
@@ -108,8 +112,10 @@ impl ItemTrait for ChannelItem {
108 linked_list 112 linked_list
109 .push_back((item.video_thumbnail.clone(), item.video_id.clone())); 113 .push_back((item.video_thumbnail.clone(), item.video_id.clone()));
110 } 114 }
111 let _ = download_all_thumbnails(linked_list);
112 115
116 if app.config.main.display_thumbnails {
117 let _ = download_all_thumbnails(linked_list);
118 }
113 *displayitem = ChannelDisplayItem::Videos(videos, textlist); 119 *displayitem = ChannelDisplayItem::Videos(videos, textlist);
114 } 120 }
115 } 121 }
@@ -277,7 +283,7 @@ impl ItemTrait for ChannelItem {
277 out 283 out
278 } 284 }
279 285
280 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) { 286 fn key_input(&mut self, key: KeyEvent, app: App) -> (bool, App) {
281 let action = match app.config.keybindings.0.get(&key) { 287 let action = match app.config.keybindings.0.get(&key) {
282 Some(action) => action, 288 Some(action) => action,
283 None => return (false, app), 289 None => return (false, app),
src/app/pages/global.rs +28 −6
@@ -1,6 +1,6 @@
1use std::fmt; 1use std::fmt;
2 2
3use crossterm::event::KeyCode; 3use crossterm::event::{KeyCode, KeyEvent};
4use serde::{Deserialize, Serialize}; 4use serde::{Deserialize, Serialize};
5use tui::{ 5use tui::{
6 backend::Backend, 6 backend::Backend,
@@ -70,14 +70,32 @@ impl ItemTrait for GlobalItem {
70 } 70 }
71 } 71 }
72 72
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) {
74 match self { 74 match self {
75 GlobalItem::SearchBar => match key { 75 GlobalItem::SearchBar => match key.code {
76 KeyCode::Char(c) => { 76 KeyCode::Char(c) => {
77 app.search_text.push(c); 77 app.search_text.insert(app.search_index, c);
78 app.search_index += 1;
78 } 79 }
79 KeyCode::Backspace => { 80 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 }
81 } 99 }
82 KeyCode::Enter => { 100 KeyCode::Enter => {
83 if app.search_text.len() == 0 { 101 if app.search_text.len() == 0 {
@@ -86,6 +104,7 @@ impl ItemTrait for GlobalItem {
86 let state = app.config.layouts.search.clone().into(); 104 let state = app.config.layouts.search.clone().into();
87 let mut history = app.history.clone(); 105 let mut history = app.history.clone();
88 let search_text = app.search_text.clone(); 106 let search_text = app.search_text.clone();
107 let search_index = app.search_index;
89 let search_settings = app.search_settings.clone(); 108 let search_settings = app.search_settings.clone();
90 history.push(app.into()); 109 history.push(app.into());
91 110
@@ -97,6 +116,7 @@ impl ItemTrait for GlobalItem {
97 selectable: App::selectable(&state), 116 selectable: App::selectable(&state),
98 state, 117 state,
99 search_text, 118 search_text,
119 search_index,
100 search_settings, 120 search_settings,
101 load: true, 121 load: true,
102 ..Default::default() 122 ..Default::default()
@@ -143,7 +163,9 @@ impl ItemTrait for GlobalItem {
143 .title_alignment(Alignment::Center); 163 .title_alignment(Alignment::Center);
144 let mut text = out.1.search_text.clone(); 164 let mut text = out.1.search_text.clone();
145 if selected { 165 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()]);
147 } 169 }
148 let paragraph = Paragraph::new(text).block(block); 170 let paragraph = Paragraph::new(text).block(block);
149 frame.render_widget(paragraph, rect); 171 frame.render_widget(paragraph, rect);
src/app/pages/item_info.rs +21 −13
@@ -14,7 +14,7 @@ use crate::{
14 traits::{ItemTrait, PageTrait}, 14 traits::{ItemTrait, PageTrait},
15 widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList}, 15 widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList},
16}; 16};
17use crossterm::event::KeyCode; 17use crossterm::event::KeyEvent;
18use tui::{ 18use tui::{
19 backend::Backend, 19 backend::Backend,
20 layout::Rect, 20 layout::Rect,
@@ -65,6 +65,10 @@ impl PlayListDisplayView {
65} 65}
66 66
67impl ItemTrait for ItemInfoItem { 67impl ItemTrait for ItemInfoItem {
68 fn reset(&mut self) {
69 // panic!("Got here!");
70 *self = Self::Unknown;
71 }
68 fn select(&mut self, app: App) -> (App, bool) { 72 fn select(&mut self, app: App) -> (App, bool) {
69 let selected = true; 73 let selected = true;
70 74
@@ -75,7 +79,7 @@ impl ItemTrait for ItemInfoItem {
75 true 79 true
76 } 80 }
77 81
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) {
79 let action = match app.config.keybindings.0.get(&key) { 83 let action = match app.config.keybindings.0.get(&key) {
80 Some(action) => *action, 84 Some(action) => *action,
81 None => return (false, app), 85 None => return (false, app),
@@ -268,10 +272,12 @@ impl ItemTrait for ItemInfoItem {
268 DisplayItem::Video(id) => { 272 DisplayItem::Video(id) => {
269 let mut list = TextList::default(); 273 let mut list = TextList::default();
270 let video: FullVideo = app.client.video(id, None)?.into(); 274 let video: FullVideo = app.client.video(id, None)?.into();
271 let _ = download_all_thumbnails(LinkedList::from([( 275 if app.config.main.display_thumbnails {
272 video.video_thumbnail.clone(), 276 let _ = download_all_thumbnails(LinkedList::from([(
273 video.video_id.clone(), 277 video.video_thumbnail.clone(),
274 )])); 278 video.video_id.clone(),
279 )]));
280 }
275 let commands = app 281 let commands = app
276 .config 282 .config
277 .page_commands 283 .page_commands
@@ -301,13 +307,15 @@ impl ItemTrait for ItemInfoItem {
301 let mut videos_text_list = TextList::default(); 307 let mut videos_text_list = TextList::default();
302 let mut playlist_text_list = TextList::default(); 308 let mut playlist_text_list = TextList::default();
303 let playlist: FullPlayList = app.client.playlist(id, None)?.into(); 309 let playlist: FullPlayList = app.client.playlist(id, None)?.into();
304 let _ = download_all_thumbnails( 310 if app.config.main.display_thumbnails {
305 playlist 311 let _ = download_all_thumbnails(
306 .videos 312 playlist
307 .iter() 313 .videos
308 .map(|v| (v.video_thumbnail.clone(), v.video_id.clone())) 314 .iter()
309 .collect(), 315 .map(|v| (v.video_thumbnail.clone(), v.video_id.clone()))
310 ); 316 .collect(),
317 );
318 }
311 319
312 videos_text_list.items = vec![String::from("Swtich view")]; 320 videos_text_list.items = vec![String::from("Swtich view")];
313 videos_text_list 321 videos_text_list
src/app/pages/main_menu.rs +33 −22
@@ -14,7 +14,7 @@ use crate::{
14 traits::{ItemTrait, PageTrait}, 14 traits::{ItemTrait, PageTrait},
15 widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList}, 15 widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList},
16}; 16};
17use crossterm::event::KeyCode; 17use crossterm::event::KeyEvent;
18use serde::{Deserialize, Serialize}; 18use serde::{Deserialize, Serialize};
19use tui::{ 19use tui::{
20 backend::Backend, 20 backend::Backend,
@@ -47,16 +47,18 @@ impl ItemTrait for MainMenuItem {
47 .map(|t| ListItem::MiniVideo(t.into())) 47 .map(|t| ListItem::MiniVideo(t.into()))
48 .collect(); 48 .collect();
49 49
50 download_all_thumbnails( 50 if app.config.main.display_thumbnails {
51 list.iter() 51 let _ = download_all_thumbnails(
52 .map(|t| match t { 52 list.iter()
53 ListItem::MiniVideo(v) => { 53 .map(|t| match t {
54 (v.video_thumbnail.clone(), v.video_id.clone()) 54 ListItem::MiniVideo(v) => {
55 } 55 (v.video_thumbnail.clone(), v.video_id.clone())
56 _ => unreachable!(), 56 }
57 }) 57 _ => unreachable!(),
58 .collect(), 58 })
59 )?; 59 .collect(),
60 );
61 }
60 62
61 if let Some((video_list, text_list, _)) = enum_items { 63 if let Some((video_list, text_list, _)) = enum_items {
62 *text_list = TextList::default(); 64 *text_list = TextList::default();
@@ -79,16 +81,18 @@ impl ItemTrait for MainMenuItem {
79 .map(|t| ListItem::MiniVideo(t.into())) 81 .map(|t| ListItem::MiniVideo(t.into()))
80 .collect(); 82 .collect();
81 83
82 let _ = download_all_thumbnails( 84 if app.config.main.display_thumbnails {
83 list.iter() 85 let _ = download_all_thumbnails(
84 .map(|t| match t { 86 list.iter()
85 ListItem::MiniVideo(v) => { 87 .map(|t| match t {
86 (v.video_thumbnail.clone(), v.video_id.clone()) 88 ListItem::MiniVideo(v) => {
87 } 89 (v.video_thumbnail.clone(), v.video_id.clone())
88 _ => unreachable!(), 90 }
89 }) 91 _ => unreachable!(),
90 .collect(), 92 })
91 ); 93 .collect(),
94 );
95 }
92 96
93 let mut text_list = TextList::default(); 97 let mut text_list = TextList::default();
94 98
@@ -220,6 +224,13 @@ impl ItemTrait for MainMenuItem {
220 } 224 }
221 out 225 out
222 } 226 }
227
228 fn reset(&mut self) {
229 if let Self::VideoList(something) = self {
230 *something = None;
231 }
232 }
233
223 fn select(&mut self, mut app: App) -> (App, bool) { 234 fn select(&mut self, mut app: App) -> (App, bool) {
224 let selected = match self { 235 let selected = match self {
225 MainMenuItem::SeletorTab(selector) => { 236 MainMenuItem::SeletorTab(selector) => {
@@ -241,7 +252,7 @@ impl ItemTrait for MainMenuItem {
241 true 252 true
242 } 253 }
243 254
244 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) { 255 fn key_input(&mut self, key: KeyEvent, app: App) -> (bool, App) {
245 match self { 256 match self {
246 MainMenuItem::VideoList(Some((list, textlist, _))) => { 257 MainMenuItem::VideoList(Some((list, textlist, _))) => {
247 let action = match app.config.keybindings.0.get(&key) { 258 let action = match app.config.keybindings.0.get(&key) {
src/app/pages/search.rs +5 −3
@@ -1,6 +1,6 @@
1use std::{collections::LinkedList, error::Error}; 1use std::{collections::LinkedList, error::Error};
2 2
3use crossterm::event::KeyCode; 3use crossterm::event::KeyEvent;
4use invidious::structs::hidden::SearchItem as InvidiousSearchItem; 4use invidious::structs::hidden::SearchItem as InvidiousSearchItem;
5use tui::{ 5use tui::{
6 backend::Backend, 6 backend::Backend,
@@ -45,7 +45,7 @@ impl ItemTrait for SearchItem {
45 true 45 true
46 } 46 }
47 47
48 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) { 48 fn key_input(&mut self, key: KeyEvent, app: App) -> (bool, App) {
49 let action = match app.config.keybindings.0.get(&key) { 49 let action = match app.config.keybindings.0.get(&key) {
50 Some(action) => action, 50 Some(action) => action,
51 None => return (false, app), 51 None => return (false, app),
@@ -275,7 +275,9 @@ impl ItemTrait for SearchItem {
275 } 275 }
276 } 276 }
277 277
278 let _ = download_all_thumbnails(thumbnails); 278 if app.config.main.display_thumbnails {
279 let _ = download_all_thumbnails(thumbnails);
280 }
279 281
280 *results = Some(items); 282 *results = Some(items);
281 } 283 }
src/main.rs +8 −8
@@ -10,7 +10,7 @@ use tui::{
10}; 10};
11use youtube_tui::{ 11use youtube_tui::{
12 app::{app::App, config::Action}, 12 app::{app::App, config::Action},
13 structs::{Row, RowItem}, 13 structs::{Row, RowItem, State},
14}; 14};
15 15
16fn main() -> Result<(), Box<dyn Error>> { 16fn main() -> Result<(), Box<dyn Error>> {
@@ -55,7 +55,7 @@ fn run_app<B: Backend>(mut terminal: &mut Terminal<B>, mut app: App) -> Result<(
55 app = hold.0; 55 app = hold.0;
56 hold.1?; 56 hold.1?;
57 let mut new_state = Vec::new(); 57 let mut new_state = Vec::new();
58 for row in app.state.iter() { 58 for row in app.state.0.iter() {
59 let mut row_vec = Vec::new(); 59 let mut row_vec = Vec::new();
60 for row_item in row.items.iter() { 60 for row_item in row.items.iter() {
61 match row_item.item.load_item(&app, &mut watch_history) { 61 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<(
82 } 82 }
83 83
84 app.watch_history = watch_history; 84 app.watch_history = watch_history;
85 app.state = new_state; 85 app.state = State(new_state);
86 86
87 app.load = false; 87 app.load = false;
88 terminal.clear()?; 88 terminal.clear()?;
@@ -92,7 +92,7 @@ fn run_app<B: Backend>(mut terminal: &mut Terminal<B>, mut app: App) -> Result<(
92 match event::read()? { 92 match event::read()? {
93 event::Event::Key(key) => { 93 event::Event::Key(key) => {
94 if app.selected.is_none() { 94 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) {
96 Some(action) => *action, 96 Some(action) => *action,
97 None => continue, 97 None => continue,
98 }; 98 };
@@ -122,7 +122,7 @@ fn run_app<B: Backend>(mut terminal: &mut Terminal<B>, mut app: App) -> Result<(
122 } 122 }
123 } 123 }
124 124
125 app = app.key_input(key.code); 125 app = app.key_input(key);
126 app.render = true; 126 app.render = true;
127 } 127 }
128 event::Event::Resize(_, _) => { 128 event::Event::Resize(_, _) => {
@@ -158,9 +158,10 @@ fn init() -> Result<(), Box<dyn Error>> {
158 let mut dir = home_dir.clone(); 158 let mut dir = home_dir.clone();
159 159
160 dir.push(".cache"); 160 dir.push(".cache");
161 if !dir.exists() { 161 if dir.exists() {
162 fs::create_dir(&dir)?; 162 fs::remove_dir_all(&dir)?;
163 } 163 }
164 fs::create_dir(&dir)?;
164 165
165 dir.push("youtube-tui"); 166 dir.push("youtube-tui");
166 if !dir.exists() { 167 if !dir.exists() {
@@ -235,7 +236,6 @@ fn exit() -> Result<(), Box<dyn Error>> {
235 236
236 dir.push(".cache"); 237 dir.push(".cache");
237 dir.push("youtube-tui"); 238 dir.push("youtube-tui");
238 dir.push("cache");
239 239
240 if dir.exists() { 240 if dir.exists() {
241 fs::remove_dir_all(&dir)?; 241 fs::remove_dir_all(&dir)?;
src/structs/apphistory.rs +7 −7
@@ -1,16 +1,13 @@
1use invidious::blocking::Client; 1use invidious::blocking::Client;
2 2
3use crate::{ 3use crate::{app::app::App, structs::Page};
4 app::app::App,
5 structs::{Page, Row},
6};
7 4
8use super::SearchSettings; 5use super::{SearchSettings, State};
9 6
10#[derive(Clone)] 7#[derive(Clone)]
11pub struct AppHistory { 8pub struct AppHistory {
12 pub page: Page, 9 pub page: Page,
13 pub state: Vec<Row>, // Item 10 pub state: State, // Item
14 pub selectable: Vec<Vec<(usize, usize)>>, 11 pub selectable: Vec<Vec<(usize, usize)>>,
15 pub hover: Option<(usize, usize)>, // x, y 12 pub hover: Option<(usize, usize)>, // x, y
16 pub selected: Option<(usize, usize)>, 13 pub selected: Option<(usize, usize)>,
@@ -21,6 +18,7 @@ pub struct AppHistory {
21 pub popup_focus: bool, 18 pub popup_focus: bool,
22 pub search_text: String, 19 pub search_text: String,
23 pub search_settings: SearchSettings, 20 pub search_settings: SearchSettings,
21 pub search_index: usize,
24 pub page_no: usize, 22 pub page_no: usize,
25} 23}
26 24
@@ -31,7 +29,8 @@ impl From<App> for AppHistory {
31 state: original.state, 29 state: original.state,
32 selectable: original.selectable, 30 selectable: original.selectable,
33 hover: original.hover, 31 hover: original.hover,
34 selected: original.selected, 32 // selected: original.selected,
33 selected: None,
35 client: original.client, 34 client: original.client,
36 message: original.message, 35 message: original.message,
37 load: original.load, 36 load: original.load,
@@ -39,6 +38,7 @@ impl From<App> for AppHistory {
39 popup_focus: original.popup_focus, 38 popup_focus: original.popup_focus,
40 search_text: original.search_text, 39 search_text: original.search_text,
41 search_settings: original.search_settings, 40 search_settings: original.search_settings,
41 search_index: original.search_index,
42 page_no: original.page_no, 42 page_no: original.page_no,
43 } 43 }
44 } 44 }
src/structs/search_settings.rs +3 −3
@@ -1,4 +1,4 @@
1use crossterm::event::KeyCode; 1use crossterm::event::KeyEvent;
2 2
3use crate::{ 3use crate::{
4 app::config::{Action, Config}, 4 app::config::{Action, Config},
@@ -66,8 +66,8 @@ impl SearchSettings {
66 vec 66 vec
67 } 67 }
68 68
69 pub fn key_input(&mut self, keycode: KeyCode, config: &Config) { 69 pub fn key_input(&mut self, key: KeyEvent, config: &Config) {
70 let action = match config.keybindings.0.get(&keycode) { 70 let action = match config.keybindings.0.get(&key) {
71 Some(action) => *action, 71 Some(action) => *action,
72 None => return, 72 None => return,
73 }; 73 };
src/structs/states.rs +25 −2
@@ -1,4 +1,4 @@
1use crossterm::event::KeyCode; 1use crossterm::event::KeyEvent;
2use std::error::Error; 2use std::error::Error;
3use tui::{ 3use tui::{
4 backend::Backend, 4 backend::Backend,
@@ -19,6 +19,19 @@ use crate::{
19 19
20use super::WatchHistory; 20use super::WatchHistory;
21 21
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
22#[derive(Clone)] 35#[derive(Clone)]
23pub enum Item { 36pub enum Item {
24 Global(GlobalItem), 37 Global(GlobalItem),
@@ -29,7 +42,7 @@ pub enum Item {
29} 42}
30 43
31impl Item { 44impl 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) {
33 match self { 46 match self {
34 Item::Global(item) => item.key_input(key, app), 47 Item::Global(item) => item.key_input(key, app),
35 Item::MainMenu(item) => item.key_input(key, app), 48 Item::MainMenu(item) => item.key_input(key, app),
@@ -39,6 +52,16 @@ impl Item {
39 } 52 }
40 } 53 }
41 54
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
42 pub fn render_item<B: Backend>( 65 pub fn render_item<B: Backend>(
43 &mut self, 66 &mut self,
44 frame: &mut Frame<B>, 67 frame: &mut Frame<B>,
src/traits/item.rs +3 −2
@@ -4,11 +4,11 @@ use crate::{
4 app::app::{App, AppNoState}, 4 app::app::{App, AppNoState},
5 structs::{Item, WatchHistory}, 5 structs::{Item, WatchHistory},
6}; 6};
7use crossterm::event::KeyCode; 7use crossterm::event::KeyEvent;
8use tui::{backend::Backend, layout::Rect, Frame}; 8use tui::{backend::Backend, layout::Rect, Frame};
9 9
10pub trait ItemTrait { 10pub 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);
12 fn render_item<B: Backend>( 12 fn render_item<B: Backend>(
13 &mut self, 13 &mut self,
14 frame: &mut Frame<B>, 14 frame: &mut Frame<B>,
@@ -26,4 +26,5 @@ pub trait ItemTrait {
26 app: &App, 26 app: &App,
27 watch_history: &mut WatchHistory, 27 watch_history: &mut WatchHistory,
28 ) -> Result<Item, Box<dyn Error>>; 28 ) -> Result<Item, Box<dyn Error>>;
29 fn reset(&mut self) {}
29} 30}