Commit 4a3947397f

4a3947397fe2de2cda4a6bfbd5365015546ebdb9

parent: 947b90ad9b

Unsigned

Siriusmart <71584876+Siriusmart@users.noreply.github.com> · 2022-06-23 18:11 UTC

Changed nothing btw

Layout: unified · split

README.md +57 −132
@@ -1,144 +1,69 @@
1## Warning:
2* This app is only for *\*nix* systems
1![](https://img.shields.io/github/languages/top/siriusmart/youtube-tui?label=rust)
2![](https://shields.io/github/license/siriusmart/youtube-tui)
3[![](https://img.shields.io/crates/d/youtube-tui?label=crates.io%20downloads)](https://crates.io/crates/youtube-tui)
4[![](https://img.shields.io/crates/v/youtube-tui?label=crates.io%20version)](https://crates.io/crates/youtube-tui)
5![](https://shields.io/github/stars/siriusmart/youtube-tui?style=social)
36
4## Changes made in this commit
5* Channel support
7# YouTube TUI
8
9An aesthetically pleasing pleasing YouTube TUI written in Rust.
10
11## Overview
12
13YouTube TUI is a text user interface that provides a clean UI for browsing YouTube content. It can perform searches and view channels in the terminal, play videos and playlists with external programs like MPV. Rust is used in writing this program for its better performance and ease of maintaining.
14
15This program is being actively developed and there will be new features coming up every commit.
16
17> ### What's new in this commit
18>
19> * Code is being cleaned up but still unreadable
20> * Haven't touched this project for a week because of exams, so I'm just trying to figure out how stuff works again
21> * Just a little save stat before I overhaul the configs and how it's handled
622
723## Installation
8Requirements: Git, Cargo and the Rust Compiler
924
10Executable size: ~12.5MB
25### Install from crates.io
1126
1227```bash
13# Install from crates.io (might not be as up-to-date as the one on GitHub)
1428cargo install youtube-tui
1529```
30### Clone from GitHub and build
31
32```bash
33git clone https://github.com/sirusmart/youtube-tui && cd youtube-tui && cargo build --release
34```
35
36The binary is located at `./target/release/youtube-tui`, go move it to other locations so that you can launch the program with `youtube-tui` command.
37
38## Usage
39
40### To launch
1641
1742```bash
18# Install from GitHub with cargo install (The binary will be placed in ~/.cargo/bin, and can be called with `youtube-tui`)
19git clone https://github.com/siriusmart/youtube-tui && cd youtube-tui && cargo install --path . && cd .. && rm -rf youtube-tui
43youtube-tui # or path to the binary
2044```
2145
22## Features
23* View the Trending and Popular tab of YouTube
24* View your Watch history
25* Download videos and audio
26* Watch videos and audio
27* Video and playlist support
28
29## Movement
30* Use the arrow keys to move around the screen
31* Press enter to select an item
32* When item is selected all your keys will be passed into it
33* Press escape to exit the item
34* Press q to quit the program
35
36## Layout
37* Each divided area is one item
38* Red shows the item your currently on
39* Blue shows the selected item
40* Yellow shows an item in a special state
41
42## Storage
43* Cache is stored in `~/.cache/youtube-tui`, it is cleared when you quit the program
44* Storage is stored in `~/.local/share/youtube-tui`
45* Config files are stored in `~/.config/youtube-tui`
46
47## Config files
48The config files are located in `~/.config/youtube-tui`, run the program once to create the config files. The following are the default config files:
49
50> ### `main.yml`
51> ```yaml
52> # The program's main config file
53> ---
54> yt_dl: # Donwload location for your downloaded videos
55> video_path: ~/Downloads/%(title)s.%(ext)s
56> audio_path: ~/Downloads/%(title)s.%(ext)s
57> max_watch_history: 50 # Maximum length of watch history
58> ```
59
60> ### `commands.yml`
61> ```yaml
62> # Command arguments to use when launching applications
63> ---
64> video_player:
65> command: mpv
66> open_in_console: false
67> args:
68> - "--no-terminal"
69> - "{url}"
70> audio_player:
71> command: mpv
72> open_in_console: true
73> args:
74> - "--no-video"
75> - "{url}"
76> image_viewer:
77> command: mpv
78> open_in_console: false
79> args:
80> - "{url}"
81> - "--no-terminal"
82> video_downloader:
83> command: yt-dlp
84> open_in_console: true
85> args:
86> - "{url}"
87> - "-o"
88> - "{video_save_location}"
89> audio_downloader:
90> command: yt-dlp
91> open_in_console: true
92> args:
93> - "{url}"
94> - "-o"
95> - "{audio_save_location}"
96> - "-x"
97> terminal:
98> command: konsole
99> open_in_console: false
100> args:
101> - "-e"
102> - "{command}"
103> playlist_audio_all:
104> command: mpv
105> open_in_console: true
106> args:
107> - "--no-video"
108> - "{url}"
109> playlist_video_all:
110> command: mpv
111> open_in_console: false
112> args:
113> - "{url}"
114> - "--no-terminal"
115> playlist_shuffle_audio_all:
116> command: mpv
117> open_in_console: true
118> args:
119> - "--no-video"
120> - "{url}"
121> - "--shuffle"
122> download_all_audio:
123> command: yt-dlp
124> open_in_console: true
125> args:
126> - "{url}"
127> - "-o"
128> - "{audio_save_location}"
129> - "-x"
130> download_all_video:
131> command: yt-dlp
132> open_in_console: true
133> args:
134> - "{url}"
135> - "-o"
136> - "{video_save_location}"
137> ```
138## To Do
139* Update notice (e.g. when a new version is released on GitHub or crates.io, can be disabled in the config file)
140* Keybind options (e.g. change the keybinds in the config file)
141* Channel search + item sorting
142
143## Bugs
144* If there is a bug please open a GitHub issue thx :)
\ No newline at end of file
46### Movement
47
48|Key|What does it do|
49|---|---|
50|Arrow keys|Move in corresponding direction|
51|Right arrow|Move cursor right
52|Enter|Select/Launch|
53|Q|Quit the program|
54
55> ### Note
56>
57> 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.
58
59## Credits
60
61* [ytfzf](https://github.com/pystardust/ytfzf) by [pystardust](https://github.com/pystardust) - The TUI I used to watch YouTube, gave me an idea how this program is going to work
62* [Terminal Typeracer](https://gitlab.com/ttyperacer/terminal-typeracer) by [Darrien Glasser](https://gitlab.com/DarrienG) - A very clean looking TUI for typing speed test, gave me idea on how this program should look like
63* [Invidious](https://invidious.io) - For having a nice API for doing YouTube searches and stuff. (I made a wrapper for the API you can check it [here](https://crates.io/crates/invidious) out if you want to)
64
65
66... and of course, credits to myself for not having the ability to read the docs for ytfzf and decided to make my own instead.
67## Bug reports
68
69If there is an issue with this program, please open an issue on GitHub, thank you :D
src/app/app.rs +40 −138
@@ -6,7 +6,7 @@ use std::{
66use crate::{
77 app::pages::main_menu::*,
88 structs::{AppHistory, Item, Page, Row, SearchSettings, WatchHistory},
9 traits::{KeyInput, SelectItem},
9 traits::{ItemTrait, PageTrait},
1010};
1111use crossterm::event::KeyCode;
1212use invidious::blocking::Client;
@@ -23,7 +23,7 @@ use super::{
2323 pages::{channel::Channel, item_info::ItemInfo, search::Search},
2424};
2525
26#[derive(Debug)]
26#[derive(Debug, Clone)]
2727pub struct App {
2828 pub config: Config,
2929 pub page: Page,
@@ -96,47 +96,11 @@ impl App {
9696 pub fn key_input(mut self, key: KeyCode) -> App {
9797 if let Some((x, y)) = self.selected {
9898 if key != KeyCode::Esc {
99 match &mut self.state[y].items[x].item {
100 Item::Global(item) => {
101 let mut item = item.clone();
102 let hold = item.key_input(key, self);
103 self = hold.1;
104 if hold.0 {
105 self.state[y].items[x].item = Item::Global(item);
106 }
107 }
108 Item::MainMenu(item) => {
109 let mut item = item.clone();
110 let hold = item.key_input(key, self);
111 self = hold.1;
112 if hold.0 {
113 self.state[y].items[x].item = Item::MainMenu(item);
114 }
115 }
116 Item::ItemInfo(item) => {
117 let mut item = item.clone();
118 let hold = item.key_input(key, self);
119 self = hold.1;
120 if hold.0 {
121 self.state[y].items[x].item = Item::ItemInfo(item);
122 }
123 }
124 Item::Search(item) => {
125 let mut item = item.clone();
126 let hold = item.key_input(key, self);
127 self = hold.1;
128 if hold.0 {
129 self.state[y].items[x].item = Item::Search(item);
130 }
131 }
132 Item::Channel(item) => {
133 let mut item = item.clone();
134 let hold = item.key_input(key, self);
135 self = hold.1;
136 if hold.0 {
137 self.state[y].items[x].item = Item::Channel(item);
138 }
139 }
99 let mut item = self.state[y].items[x].item.clone();
100 let updated;
101 (updated, self) = item.key_input(key, self);
102 if updated {
103 self.state[y].items[x].item = item;
140104 }
141105
142106 return self;
@@ -148,7 +112,9 @@ impl App {
148112 if self.hover.is_some() && self.selected.is_none() {
149113 let (mut x, mut y) = self.hover.unwrap();
150114 (x, y) = self.selectable[y][x];
151 match self.state[y]
115
116 let select;
117 (self, select) = self.state[y]
152118 .items
153119 .iter()
154120 .nth(x)
@@ -156,42 +122,9 @@ impl App {
156122 .unwrap()
157123 .item
158124 .clone()
159 {
160 Item::Global(mut item) => {
161 let held = item.select(self);
162 self = held.0;
163 if held.1 {
164 self.selected = Some((x, y));
165 }
166 }
167 Item::MainMenu(mut item) => {
168 let held = item.select(self);
169 self = held.0;
170 if held.1 {
171 self.selected = Some((x, y));
172 }
173 }
174 Item::ItemInfo(mut item) => {
175 let held = item.select(self);
176 self = held.0;
177 if held.1 {
178 self.selected = Some((x, y));
179 }
180 }
181 Item::Search(mut item) => {
182 let held = item.select(self);
183 self = held.0;
184 if held.1 {
185 self.selected = Some((x, y));
186 }
187 }
188 Item::Channel(mut item) => {
189 let held = item.select(self);
190 self = held.0;
191 if held.1 {
192 self.selected = Some((x, y));
193 }
194 }
125 .select(self);
126 if select {
127 self.selected = Some((x, y));
195128 }
196129
197130 return self;
@@ -268,9 +201,10 @@ impl App {
268201 self
269202 }
270203
271 pub fn render<B: Backend>(&mut self, frame: &mut Frame<B>) {
204 pub fn render<B: Backend>(mut self, frame: &mut Frame<B>) -> Self {
272205 let size = frame.size();
273206 let mut popups = Vec::new();
207 let mut modified = Vec::new();
274208
275209 let min = match self.page {
276210 Page::MainMenu(_) => MainMenu::min(),
@@ -288,7 +222,7 @@ impl App {
288222 .style(Style::default().fg(Color::Red))
289223 .wrap(Wrap { trim: true });
290224 frame.render_widget(paragraph, size);
291 return;
225 return self;
292226 }
293227
294228 let hover_selected = if let Some((x, y)) = self.hover {
@@ -307,9 +241,10 @@ impl App {
307241 )
308242 .split(size);
309243
310 for (y, (row, row_chunk)) in self
311 .state
312 .iter_mut()
244 let state = self.state.clone();
245
246 for (y, (row, row_chunk)) in state
247 .iter()
313248 .zip(vertical_chunks.clone().into_iter())
314249 .enumerate()
315250 {
@@ -348,73 +283,40 @@ impl App {
348283
349284 frame.render_widget(Block::default(), chunks.next().unwrap());
350285
351 for (x, (chunk, item)) in chunks
352 .zip(row.items.iter_mut().map(|i| &mut i.item))
353 .enumerate()
354 {
286 for (x, (chunk, item)) in chunks.zip(row.items.iter().map(|i| &i.item)).enumerate() {
355287 let selected = self.selected == Some((x, y));
356288
357289 let hover = hover_selected == Some((x, y));
290 let popup_focus = self.popup_focus;
358291
359 if match item {
360 Item::Global(i) => i.render_item(
361 frame,
362 chunk,
363 selected,
364 hover,
365 false,
366 &*self.message.lock().unwrap(),
367 &mut self.search_settings,
368 &self.search_text,
369 ),
370 Item::MainMenu(i) => {
371 i.render_item(frame, chunk, selected, hover, self.popup_focus, &self.page);
372 false
373 }
292 let hold =
293 item.render_item(frame, chunk, self, selected, hover, popup_focus, false);
374294
375 Item::ItemInfo(i) => {
376 i.render_item(frame, chunk, selected, hover, self.popup_focus);
377 false
378 }
295 self = hold.2;
379296
380 Item::Search(i) => {
381 i.render_item(frame, chunk, selected, hover, self.popup_focus);
382 false
383 }
297 if let Some(item) = hold.1 {
298 modified.push((x, y, item));
299 }
384300
385 Item::Channel(i) => {
386 i.render_item(frame, chunk, selected, hover, self.popup_focus, &self.page);
387 false
388 }
389 } {
390 popups.push((item, selected, hover, chunk));
301 if hold.0 {
302 popups.push((item, selected, hover, chunk, x, y));
391303 }
392304 }
393305 }
394306
395 for (item, selected, hover, chunk) in popups {
396 match item {
397 Item::Global(i) => {
398 i.render_item(
399 frame,
400 chunk,
401 selected,
402 hover,
403 true,
404 &*self.message.lock().unwrap(),
405 &mut self.search_settings,
406 &self.search_text,
407 );
408 }
409 _ => {} // Item::MainMenu(i) => {
410 // i.render_item(frame, chunk, selected, hover, &self.page);
411 // }
307 for (item, selected, hover, chunk, x, y) in popups {
308 let hold = item.render_item(frame, chunk, self, selected, hover, true, true);
309 self = hold.2;
412310
413 // Item::ItemInfo(i) => {
414 // i.render_item(frame, chunk, selected, hover);
415 // }
311 if let Some(item) = hold.1 {
312 modified.push((x, y, item));
416313 }
417314 }
315
316 for (x, y, item) in modified {
317 self.state[y].items[x].item = item;
318 }
319 self
418320 }
419321
420322 pub fn pop(mut self) -> (App, bool) {
src/app/pages/channel.rs +55 −20
@@ -12,8 +12,10 @@ use tui::{
1212use crate::{
1313 app::app::App,
1414 functions::download_all_thumbnails,
15 structs::{FullChannel, Item, ListItem, MiniPlayList, MiniVideo, Page, Row, RowItem},
16 traits::{KeyInput, SelectItem},
15 structs::{
16 FullChannel, Item, ListItem, MiniPlayList, MiniVideo, Page, Row, RowItem, WatchHistory,
17 },
18 traits::{ItemTrait, PageTrait},
1719 widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList},
1820};
1921
@@ -43,8 +45,8 @@ pub enum ChannelDisplayItem {
4345 Unknown,
4446}
4547
46impl ChannelItem {
47 pub fn load_item(&self, app: &App) -> Result<Self, Box<dyn Error>> {
48impl ItemTrait for ChannelItem {
49 fn load_item(&self, app: &App, _: &mut WatchHistory) -> Result<Item, Box<dyn Error>> {
4850 let mut this = self.clone();
4951
5052 if let Page::Channel(channel_page, id) = &app.page {
@@ -112,18 +114,28 @@ impl ChannelItem {
112114 }
113115 }
114116
115 Ok(this)
117 Ok(Item::Channel(this))
116118 }
117119
118 pub fn render_item<B: Backend>(
119 &mut self,
120 fn render_item<B: Backend>(
121 // &mut self,
122 // frame: &mut Frame<B>,
123 // rect: Rect,
124 // selected: bool,
125 // hover: bool,
126 // popup_focus: bool,
127 // page: &Page,
128 &self,
120129 frame: &mut Frame<B>,
121130 rect: Rect,
131 app: App,
122132 selected: bool,
123133 hover: bool,
124134 popup_focus: bool,
125 page: &Page,
126 ) {
135 _: bool,
136 ) -> (bool, Option<Item>, App) {
137 let mut out = (false, None, app);
138
127139 let mut style = Style::default().fg(if selected {
128140 Color::LightBlue
129141 } else if hover {
@@ -172,9 +184,16 @@ impl ChannelItem {
172184
173185 frame.render_widget(split, rect);
174186
175 textlist.area(chunks[0]);
176187 let mut textlist = textlist.clone();
177188
189 textlist.area(chunks[0]);
190
191 let hold_textlist = if textlist.area == textlist.prev_area {
192 None
193 } else {
194 Some(textlist.clone())
195 };
196
178197 if selected {
179198 textlist.selected_style(Style::default().fg(Color::LightRed));
180199 } else {
@@ -193,6 +212,12 @@ impl ChannelItem {
193212 }
194213
195214 frame.render_widget(textlist, chunks[0]);
215
216 if let Some(textlist) = hold_textlist {
217 out.1 = Some(Item::Channel(ChannelItem::InfoDisplay(
218 ChannelDisplayItem::Videos(videos.clone(), textlist),
219 )));
220 }
196221 }
197222 ChannelDisplayItem::Playlists(playlists, textlist) => {
198223 let split = HorizontalSplit::default()
@@ -209,9 +234,16 @@ impl ChannelItem {
209234
210235 frame.render_widget(split, rect);
211236
212 textlist.area(chunks[0]);
213237 let mut textlist = textlist.clone();
214238
239 textlist.area(chunks[0]);
240
241 let hold_textlist = if textlist.area == textlist.prev_area {
242 None
243 } else {
244 Some(textlist.clone())
245 };
246
215247 if selected {
216248 textlist.selected_style(Style::default().fg(Color::LightRed));
217249 } else {
@@ -230,11 +262,17 @@ impl ChannelItem {
230262 }
231263
232264 frame.render_widget(textlist, chunks[0]);
265
266 if let Some(textlist) = hold_textlist {
267 out.1 = Some(Item::Channel(ChannelItem::InfoDisplay(
268 ChannelDisplayItem::Playlists(playlists.clone(), textlist),
269 )));
270 }
233271 }
234272 },
235273 ChannelItem::SelectItems(selected_page) => {
236274 if !hover {
237 if let Page::Channel(channel_page, _) = page {
275 if let Page::Channel(channel_page, _) = &out.2.page {
238276 if *selected_page == *channel_page {
239277 style = style.fg(Color::LightYellow);
240278 }
@@ -252,10 +290,9 @@ impl ChannelItem {
252290 frame.render_widget(paragraph, rect);
253291 }
254292 }
293 out
255294 }
256}
257295
258impl KeyInput for ChannelItem {
259296 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
260297 match self {
261298 ChannelItem::InfoDisplay(displayitem) => match displayitem {
@@ -336,9 +373,7 @@ impl KeyInput for ChannelItem {
336373
337374 (true, app)
338375 }
339}
340376
341impl SelectItem for ChannelItem {
342377 fn select(&mut self, app: App) -> (App, bool) {
343378 let cloned = self.clone();
344379 match &cloned {
@@ -377,16 +412,16 @@ impl SelectItem for ChannelItem {
377412
378413pub struct Channel;
379414
380impl Channel {
381 pub fn message() -> String {
415impl PageTrait for Channel {
416 fn message() -> String {
382417 String::from("Loading channel info...")
383418 }
384419
385 pub fn min() -> (u16, u16) {
420 fn min() -> (u16, u16) {
386421 (45, 15)
387422 }
388423
389 pub fn default() -> Vec<Row> {
424 fn default() -> Vec<Row> {
390425 vec![
391426 Row {
392427 items: vec![
src/app/pages/global.rs +47 −24
@@ -12,8 +12,8 @@ use tui::{
1212use crate::{
1313 app::app::App,
1414 functions::center_rect,
15 structs::{Page, SearchSettings},
16 traits::{KeyInput, SelectItem},
15 structs::{Item, Page, WatchHistory},
16 traits::{ItemTrait, PageTrait},
1717 widgets::{force_clear::ForceClear, horizontal_split::HorizontalSplit},
1818};
1919
@@ -50,7 +50,7 @@ impl Mode {
5050 }
5151}
5252
53impl SelectItem for GlobalItem {
53impl ItemTrait for GlobalItem {
5454 fn select(&mut self, mut app: App) -> (App, bool) {
5555 let selected = match self {
5656 GlobalItem::SearchBar => true,
@@ -70,9 +70,7 @@ impl SelectItem for GlobalItem {
7070 _ => false,
7171 }
7272 }
73}
7473
75impl KeyInput for GlobalItem {
7674 fn key_input(&mut self, key: KeyCode, mut app: App) -> (bool, App) {
7775 match self {
7876 GlobalItem::SearchBar => match key {
@@ -118,21 +116,28 @@ impl KeyInput for GlobalItem {
118116
119117 (true, app)
120118 }
121}
122119
123impl GlobalItem {
124 pub fn render_item<B: Backend>(
120 fn render_item<B: Backend>(
121 // &self,
122 // frame: &mut Frame<B>,
123 // rect: Rect,
124 // selected: bool,
125 // hover: bool,
126 // popup_render: bool,
127 // message: &Option<String>,
128 // search_settings: &mut SearchSettings,
129 // search_text: &String,
125130 &self,
126131 frame: &mut Frame<B>,
127132 rect: Rect,
133 app: App,
128134 selected: bool,
129135 hover: bool,
136 _: bool,
130137 popup_render: bool,
131 message: &Option<String>,
132 search_settings: &mut SearchSettings,
133 search_text: &String,
134 ) -> bool {
138 ) -> (bool, Option<Item>, App) {
135139 let area = frame.size();
140 let mut out = (false, None, app);
136141 match self {
137142 GlobalItem::SearchBar => {
138143 let block = Block::default()
@@ -147,10 +152,15 @@ impl GlobalItem {
147152 }))
148153 .title("Search YouTube")
149154 .title_alignment(Alignment::Center);
150 let paragraph = Paragraph::new(search_text.clone()).block(block);
155 let mut text = out.2.search_text.clone();
156 if selected {
157 text.push('█');
158 }
159 let paragraph = Paragraph::new(text).block(block);
151160 frame.render_widget(paragraph, rect);
152161 }
153162 GlobalItem::MessageBar => {
163 let message = out.2.message.lock().unwrap();
154164 // let color = Color::LightYellow;
155165 let block = Block::default()
156166 .border_type(BorderType::Rounded)
@@ -196,32 +206,39 @@ impl GlobalItem {
196206
197207 frame.render_widget(split, rect);
198208
199 search_settings.text_list.area(chunks[0]);
209 out.2.search_settings.text_list.area(chunks[0]);
200210
201 if search_settings.row {
202 search_settings
211 if out.2.search_settings.row {
212 out.2
213 .search_settings
203214 .text_list
204215 .selected_style(Style::default().fg(Color::LightRed));
205216
206 search_settings
217 out.2
218 .search_settings
207219 .select_text_list
208220 .selected_style(Style::default().fg(Color::LightYellow));
209221 } else {
210 search_settings
222 out.2
223 .search_settings
211224 .text_list
212225 .selected_style(Style::default().fg(Color::LightYellow));
213226
214 search_settings
227 out.2
228 .search_settings
215229 .select_text_list
216230 .selected_style(Style::default().fg(Color::LightRed));
217231 }
218232
219 frame.render_widget(search_settings.text_list.clone(), chunks[0]);
233 frame.render_widget(
234 out.2.search_settings.text_list.clone(),
235 chunks[0],
236 );
220237
221 search_settings.select_text_list.area(chunks[1]);
238 out.2.search_settings.select_text_list.area(chunks[1]);
222239
223240 frame.render_widget(
224 search_settings.select_text_list.clone(),
241 out.2.search_settings.select_text_list.clone(),
225242 chunks[1],
226243 );
227244
@@ -235,12 +252,18 @@ impl GlobalItem {
235252 }
236253 //panic!("{:?}", rect);
237254 } else {
238 return true;
255 out.0 = true;
239256 }
240257 }
241258 }
242259 }
243260
244 false
261 out
262 }
263
264 fn load_item(&self, _: &App, _: &mut WatchHistory) -> Result<Item, Box<dyn std::error::Error>> {
265 Ok(Item::Global(self.clone()))
245266 }
246267}
268
269impl GlobalItem {}
src/app/pages/item_info.rs +28 −23
@@ -10,7 +10,7 @@ use crate::{
1010 app::{app::App, config::EnvVar, pages::global::*},
1111 functions::download_all_thumbnails,
1212 structs::{FullPlayList, FullVideo, Item, ListItem, Page, Row, RowItem, WatchHistory},
13 traits::{KeyInput, SelectItem},
13 traits::{ItemTrait, PageTrait},
1414 widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList},
1515};
1616use crossterm::event::KeyCode;
@@ -64,7 +64,7 @@ impl PlayListDisplayView {
6464 }
6565}
6666
67impl SelectItem for ItemInfoItem {
67impl ItemTrait for ItemInfoItem {
6868 fn select(&mut self, app: App) -> (App, bool) {
6969 let selected = true;
7070
@@ -74,9 +74,7 @@ impl SelectItem for ItemInfoItem {
7474 fn selectable(&self) -> bool {
7575 true
7676 }
77}
7877
79impl KeyInput for ItemInfoItem {
8078 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
8179 match self {
8280 ItemInfoItem::Video(videoinfo) => match key {
@@ -587,14 +585,12 @@ impl KeyInput for ItemInfoItem {
587585
588586 (true, app)
589587 }
590}
591588
592impl ItemInfoItem {
593 pub fn load_item(
589 fn load_item(
594590 &self,
595591 app: &App,
596592 watch_history: &mut WatchHistory,
597 ) -> Result<Self, Box<dyn Error>> {
593 ) -> Result<Item, Box<dyn Error>> {
598594 let mut this = self.clone();
599595
600596 if let ItemInfoItem::Unknown = this {
@@ -682,22 +678,30 @@ impl ItemInfoItem {
682678 _ => {}
683679 }
684680 }
685 Ok(this)
681 Ok(Item::ItemInfo(this))
686682 }
687683
688 pub fn render_item<B: Backend>(
689 &mut self,
684 fn render_item<B: Backend>(
685 // &mut self,
686 // frame: &mut Frame<B>,
687 // rect: Rect,
688 // selected: bool,
689 // hover: bool,
690 // popup_focus: bool,
691 &self,
690692 frame: &mut Frame<B>,
691693 rect: Rect,
694 app: App,
692695 selected: bool,
693696 hover: bool,
694697 popup_focus: bool,
695 ) {
698 _: bool,
699 ) -> (bool, Option<Item>, App) {
700 let out = (false, None, app);
696701 match self {
697702 ItemInfoItem::Video(videoinfo) => {
698 let index = videoinfo.list.items.len() - 1;
699 videoinfo.list.items[index] = format!("Mode: {}", videoinfo.mode);
700 drop(index);
703 let mut list = videoinfo.list.clone();
704 list.items[videoinfo.list.items.len() - 1] = format!("Mode: {}", videoinfo.mode);
701705
702706 let split = HorizontalSplit::default()
703707 .percentages(vec![40, 60])
@@ -710,7 +714,6 @@ impl ItemInfoItem {
710714 }));
711715
712716 let chunks = split.inner(rect);
713 let mut list = videoinfo.list.clone();
714717
715718 list.area(chunks[1]);
716719 if selected {
@@ -768,10 +771,9 @@ impl ItemInfoItem {
768771 frame.render_widget(list, chunks[1]);
769772 }
770773 PlayListDisplayView::PlayListView => {
771 let index = playlistinfo.playlist_view_list.items.len() - 1;
772 playlistinfo.playlist_view_list.items[index] =
774 let mut playlist_view_list = playlistinfo.playlist_view_list.clone();
775 playlist_view_list.items[playlistinfo.playlist_view_list.items.len() - 1] =
773776 format!("Mode: {}", playlistinfo.mode);
774 drop(index);
775777
776778 let mut list = playlistinfo.playlist_view_list.clone();
777779 list.area(chunks[1]);
@@ -800,6 +802,7 @@ impl ItemInfoItem {
800802 frame.render_widget(split, rect);
801803 }
802804 }
805 out
803806 }
804807}
805808
@@ -811,16 +814,16 @@ pub enum DisplayItem {
811814
812815pub struct ItemInfo;
813816
814impl ItemInfo {
815 pub fn message() -> String {
817impl PageTrait for ItemInfo {
818 fn message() -> String {
816819 String::from("Loading item info...")
817820 }
818821
819 pub fn min() -> (u16, u16) {
822 fn min() -> (u16, u16) {
820823 (21, 12)
821824 }
822825
823 pub fn default() -> Vec<Row> {
826 fn default() -> Vec<Row> {
824827 vec![
825828 Row {
826829 items: vec![
@@ -856,6 +859,8 @@ impl ItemInfo {
856859 }
857860}
858861
862impl ItemInfoItem {}
863
859864fn run_command(args: Vec<String>, message: Arc<Mutex<Option<String>>>) {
860865 thread::spawn(move || {
861866 let mut args = args.into_iter();
src/app/pages/main_menu.rs +110 −88
@@ -6,8 +6,8 @@ use crate::{
66 pages::{global::*, item_info::*},
77 },
88 functions::download_all_thumbnails,
9 structs::{Item, ListItem, Page, Row, RowItem},
10 traits::{KeyInput, SelectItem},
9 structs::{Item, ListItem, Page, Row, RowItem, WatchHistory},
10 traits::{ItemTrait, PageTrait},
1111 widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList},
1212};
1313use crossterm::event::KeyCode;
@@ -26,80 +26,8 @@ pub enum MainMenuItem {
2626
2727// app.client.trending(None)
2828
29impl SelectItem for MainMenuItem {
30 fn select(&mut self, mut app: App) -> (App, bool) {
31 let selected = match self {
32 MainMenuItem::SeletorTab(selector) => {
33 if app.page == (Page::MainMenu(*selector)) {
34 return (app, false);
35 }
36 app.page = Page::MainMenu(*selector);
37 app.load = true;
38 false
39 }
40
41 _ => true,
42 };
43
44 (app, selected)
45 }
46
47 fn selectable(&self) -> bool {
48 true
49 }
50}
51
52impl KeyInput for MainMenuItem {
53 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
54 match self {
55 MainMenuItem::VideoList(Some((list, textlist, _))) => match key {
56 KeyCode::Up => textlist.up(),
57 KeyCode::Down => textlist.down(),
58 KeyCode::PageUp => textlist.selected = 0,
59 KeyCode::PageDown => textlist.selected = textlist.items.len() - 1,
60 KeyCode::Enter => {
61 let state = ItemInfo::default();
62 let mut history = app.history.clone();
63 history.push(app.into());
64
65 return (
66 false,
67 App {
68 history,
69 page: Page::ItemDisplay(
70 match list.iter().nth(textlist.selected).unwrap() {
71 ListItem::FullVideo(item) => {
72 DisplayItem::Video(item.video_id.clone())
73 }
74 ListItem::FullPlayList(item) => {
75 DisplayItem::PlayList(item.playlist_id.clone())
76 }
77 ListItem::MiniPlayList(item) => {
78 DisplayItem::PlayList(item.playlist_id.clone())
79 }
80 ListItem::MiniVideo(item) => {
81 DisplayItem::Video(item.video_id.clone())
82 }
83 _ => unreachable!(),
84 },
85 ),
86 selectable: App::selectable(&state),
87 state,
88 ..Default::default()
89 },
90 );
91 }
92 _ => {}
93 },
94 _ => {}
95 }
96
97 (true, app)
98 }
99}
100
101impl MainMenuItem {
102 pub fn load_item(&self, app: &App) -> Result<Self, Box<dyn Error>> {
29impl ItemTrait for MainMenuItem {
30 fn load_item(&self, app: &App, _: &mut WatchHistory) -> Result<Item, Box<dyn Error>> {
10331 let mut this = self.clone();
10432
10533 match &mut this {
@@ -201,18 +129,27 @@ impl MainMenuItem {
201129 _ => {}
202130 }
203131
204 Ok(this)
132 Ok(Item::MainMenu(this))
205133 }
206134
207 pub fn render_item<B: Backend>(
208 &mut self,
135 fn render_item<B: Backend>(
136 // &mut self,
137 // frame: &mut Frame<B>,
138 // rect: Rect,
139 // selected: bool,
140 // hover: bool,
141 // popup_focus: bool,
142 // page: &Page,
143 &self,
209144 frame: &mut Frame<B>,
210145 rect: Rect,
146 app: App,
211147 selected: bool,
212148 hover: bool,
213149 popup_focus: bool,
214 page: &Page,
215 ) {
150 _: bool,
151 ) -> (bool, Option<Item>, App) {
152 let mut out = (false, None, app);
216153 let mut style = Style::default().fg(if selected {
217154 Color::LightBlue
218155 } else if hover {
@@ -223,7 +160,7 @@ impl MainMenuItem {
223160
224161 match self {
225162 MainMenuItem::SeletorTab(selector) => {
226 if !hover && page == &(Page::MainMenu(*selector)) {
163 if !hover && &out.2.page == &(Page::MainMenu(*selector)) {
227164 style = style.fg(Color::LightYellow);
228165 }
229166 let text = match selector {
@@ -257,10 +194,17 @@ impl MainMenuItem {
257194
258195 frame.render_widget(split, rect);
259196
260 if let Some((videos, list, _)) = data {
261 list.area(chunks[0]);
197 if let Some((videos, list, index)) = data {
262198 let mut list = list.clone();
263199
200 list.area(chunks[0]);
201
202 let hold_textlist = if list.area == list.prev_area {
203 None
204 } else {
205 Some(list.clone())
206 };
207
264208 if selected {
265209 list.selected_style(Style::default().fg(Color::LightRed));
266210 } else {
@@ -274,12 +218,90 @@ impl MainMenuItem {
274218 }
275219
276220 frame.render_widget(list, chunks[0]);
221
222 if let Some(textlist) = hold_textlist {
223 out.1 = Some(Item::MainMenu(MainMenuItem::VideoList(Some((
224 videos.clone(),
225 textlist,
226 *index,
227 )))));
228 }
229 }
230 }
231 }
232 out
233 }
234 fn select(&mut self, mut app: App) -> (App, bool) {
235 let selected = match self {
236 MainMenuItem::SeletorTab(selector) => {
237 if app.page == (Page::MainMenu(*selector)) {
238 return (app, false);
277239 }
240 app.page = Page::MainMenu(*selector);
241 app.load = true;
242 false
278243 }
244
245 _ => true,
246 };
247
248 (app, selected)
249 }
250
251 fn selectable(&self) -> bool {
252 true
253 }
254
255 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
256 match self {
257 MainMenuItem::VideoList(Some((list, textlist, _))) => match key {
258 KeyCode::Up => textlist.up(),
259 KeyCode::Down => textlist.down(),
260 KeyCode::PageUp => textlist.selected = 0,
261 KeyCode::PageDown => textlist.selected = textlist.items.len() - 1,
262 KeyCode::Enter => {
263 let state = ItemInfo::default();
264 let mut history = app.history.clone();
265 history.push(app.into());
266
267 return (
268 false,
269 App {
270 history,
271 page: Page::ItemDisplay(
272 match list.iter().nth(textlist.selected).unwrap() {
273 ListItem::FullVideo(item) => {
274 DisplayItem::Video(item.video_id.clone())
275 }
276 ListItem::FullPlayList(item) => {
277 DisplayItem::PlayList(item.playlist_id.clone())
278 }
279 ListItem::MiniPlayList(item) => {
280 DisplayItem::PlayList(item.playlist_id.clone())
281 }
282 ListItem::MiniVideo(item) => {
283 DisplayItem::Video(item.video_id.clone())
284 }
285 _ => unreachable!(),
286 },
287 ),
288 selectable: App::selectable(&state),
289 state,
290 ..Default::default()
291 },
292 );
293 }
294 _ => {}
295 },
296 _ => {}
279297 }
298
299 (true, app)
280300 }
281301}
282302
303impl MainMenuItem {}
304
283305pub fn textlist_from_video_list(original: &LinkedList<ListItem>) -> Vec<String> {
284306 original.iter().map(|item| item.to_string()).collect()
285307}
@@ -299,16 +321,16 @@ impl Default for MainMenuSelector {
299321
300322pub struct MainMenu;
301323
302impl MainMenu {
303 pub fn message() -> String {
324impl PageTrait for MainMenu {
325 fn message() -> String {
304326 String::from("Loading home page...")
305327 }
306328
307 pub fn min() -> (u16, u16) {
329 fn min() -> (u16, u16) {
308330 (45, 15)
309331 }
310332
311 pub fn default() -> Vec<Row> {
333 fn default() -> Vec<Row> {
312334 vec![
313335 Row {
314336 items: vec![
src/app/pages/search.rs +20 −17
@@ -12,8 +12,10 @@ use tui::{
1212use crate::{
1313 app::app::App,
1414 functions::download_all_thumbnails,
15 structs::{Item, ListItem, MiniChannel, MiniPlayList, MiniVideo, Page, Row, RowItem},
16 traits::{KeyInput, SelectItem},
15 structs::{
16 Item, ListItem, MiniChannel, MiniPlayList, MiniVideo, Page, Row, RowItem, WatchHistory,
17 },
18 traits::{ItemTrait, PageTrait},
1719 widgets::{horizontal_split::HorizontalSplit, item_display::ItemDisplay, text_list::TextList},
1820};
1921
@@ -32,7 +34,7 @@ pub enum SearchItem {
3234 },
3335}
3436
35impl SelectItem for SearchItem {
37impl ItemTrait for SearchItem {
3638 fn select(&mut self, app: App) -> (App, bool) {
3739 (app, true)
3840 }
@@ -40,9 +42,7 @@ impl SelectItem for SearchItem {
4042 fn selectable(&self) -> bool {
4143 true
4244 }
43}
4445
45impl KeyInput for SearchItem {
4646 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
4747 match self {
4848 SearchItem::Search { results, text_list } => match key {
@@ -154,10 +154,8 @@ impl KeyInput for SearchItem {
154154
155155 (true, app)
156156 }
157}
158157
159impl SearchItem {
160 pub fn load_item(&self, app: &App) -> Result<Self, Box<dyn Error>> {
158 fn load_item(&self, app: &App, _: &mut WatchHistory) -> Result<Item, Box<dyn Error>> {
161159 let mut this = self.clone();
162160
163161 match &mut this {
@@ -279,17 +277,20 @@ impl SearchItem {
279277 }
280278 }
281279
282 Ok(this)
280 Ok(Item::Search(this))
283281 }
284282
285 pub fn render_item<B: Backend>(
286 &mut self,
283 fn render_item<B: Backend>(
284 &self,
287285 frame: &mut Frame<B>,
288286 rect: Rect,
287 app: App,
289288 selected: bool,
290289 hover: bool,
291290 popup_focus: bool,
292 ) {
291 _: bool,
292 ) -> (bool, Option<Item>, App) {
293 let out = (false, None, app);
293294 match self {
294295 SearchItem::Search { results, text_list } => {
295296 let split = HorizontalSplit::default()
@@ -306,8 +307,8 @@ impl SearchItem {
306307
307308 frame.render_widget(split, rect);
308309
309 text_list.area(chunks[0]);
310310 let mut text_list = text_list.clone();
311 text_list.area(chunks[0]);
311312
312313 if let Some(result) = results {
313314 if let Some(item) = result.iter().nth(text_list.selected) {
@@ -326,21 +327,23 @@ impl SearchItem {
326327 frame.render_widget(text_list, chunks[0]);
327328 }
328329 }
330
331 out
329332 }
330333}
331334
332335pub struct Search;
333336
334impl Search {
335 pub fn message() -> String {
337impl PageTrait for Search {
338 fn message() -> String {
336339 String::from("Loading search results...")
337340 }
338341
339 pub fn min() -> (u16, u16) {
342 fn min() -> (u16, u16) {
340343 (45, 12)
341344 }
342345
343 pub fn default() -> Vec<Row> {
346 fn default() -> Vec<Row> {
344347 vec![
345348 Row {
346349 items: vec![
src/main.rs +33 −82
@@ -3,7 +3,7 @@ use crossterm::{
33 execute,
44 terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
55};
6use std::{error::Error, fs, io};
6use std::{error::Error, fs, io, sync::mpsc::channel};
77use tui::{
88 backend::{Backend, CrosstermBackend},
99 Terminal,
@@ -11,9 +11,8 @@ use tui::{
1111use youtube_tui::{
1212 app::{
1313 app::App,
14 pages::{channel::Channel, item_info::ItemInfo, main_menu::MainMenu, search::Search},
1514 },
16 structs::{Item, Page, Row, RowItem},
15 structs::{Row, RowItem},
1716};
1817
1918fn main() -> Result<(), Box<dyn Error>> {
@@ -45,92 +44,35 @@ fn main() -> Result<(), Box<dyn Error>> {
4544fn run_app<B: Backend>(mut terminal: &mut Terminal<B>, mut app: App) -> Result<(), Box<dyn Error>> {
4645 loop {
4746 if app.render {
48 ui(&mut terminal, &mut app)?;
47 let hold = ui(&mut terminal, app);
48 app = hold.0;
49 hold.1?;
4950 }
5051
5152 if app.load {
52 *app.message.lock().unwrap() = Some(match app.page {
53 Page::MainMenu(_) => MainMenu::message(),
54 Page::ItemDisplay(_) => ItemInfo::message(),
55 Page::Search => Search::message(),
56 Page::Channel(_, _) => Channel::message(),
57 });
53 *app.message.lock().unwrap() = Some(app.page.message());
54 let mut watch_history = app.watch_history.clone();
5855 terminal.clear()?;
59 ui(&mut terminal, &mut app)?;
56 let hold = ui(&mut terminal, app);
57 app = hold.0;
58 hold.1?;
6059 let mut new_state = Vec::new();
6160 for row in app.state.iter() {
6261 let mut row_vec = Vec::new();
6362 for row_item in row.items.iter() {
64 match &row_item.item {
65 Item::MainMenu(item) => match item.load_item(&app) {
66 Ok(new) => {
67 row_vec.push(RowItem {
68 item: Item::MainMenu(new),
69 ..*row_item
70 });
71 }
72 Err(e) => {
73 row_vec.push(RowItem {
74 item: Item::MainMenu(item.clone()),
75 ..*row_item
76 });
77 *app.message.lock().unwrap() = Some(e.to_string());
78 }
79 },
80 Item::ItemInfo(item) => {
81 let mut watch_history = app.watch_history.clone();
82 match item.load_item(&app, &mut watch_history) {
83 Ok(new) => {
84 row_vec.push(RowItem {
85 item: Item::ItemInfo(new),
86 ..*row_item
87 });
88 }
89 Err(e) => {
90 row_vec.push(RowItem {
91 item: Item::ItemInfo(item.clone()),
92 ..*row_item
93 });
94 *app.message.lock().unwrap() = Some(e.to_string());
95 }
96 }
97 app.watch_history = watch_history;
63 match row_item.item.load_item(&app, &mut watch_history) {
64 Ok(new) => {
65 row_vec.push(RowItem {
66 item: new,
67 ..*row_item
68 });
9869 }
99 Item::Search(item) => match item.load_item(&app) {
100 Ok(new) => {
101 row_vec.push(RowItem {
102 item: Item::Search(new),
103 ..*row_item
104 });
105 }
106 Err(e) => {
107 row_vec.push(RowItem {
108 item: Item::Search(item.clone()),
109 ..*row_item
110 });
111 *app.message.lock().unwrap() = Some(e.to_string());
112 }
113 },
114 Item::Channel(item) => match item.load_item(&app) {
115 Ok(new) => {
116 row_vec.push(RowItem {
117 item: Item::Channel(new),
118 ..*row_item
119 });
120 }
121 Err(e) => {
122 row_vec.push(RowItem {
123 item: Item::Channel(item.clone()),
124 ..*row_item
125 });
126 *app.message.lock().unwrap() = Some(e.to_string());
127 }
128 },
129 _ => {
70 Err(e) => {
13071 row_vec.push(RowItem {
13172 item: row_item.item.clone(),
13273 ..*row_item
13374 });
75 *app.message.lock().unwrap() = Some(e.to_string());
13476 }
13577 }
13678 }
@@ -141,11 +83,13 @@ fn run_app<B: Backend>(mut terminal: &mut Terminal<B>, mut app: App) -> Result<(
14183 });
14284 }
14385
86 app.watch_history = watch_history;
14487 app.state = new_state;
14588
14689 app.load = false;
14790 terminal.clear()?;
148 ui(&mut terminal, &mut app)?;
91 let hold = ui(&mut terminal, app);
92 app = hold.0;
14993 } else {
15094 match event::read()? {
15195 event::Event::Key(key) => {
@@ -188,15 +132,22 @@ fn run_app<B: Backend>(mut terminal: &mut Terminal<B>, mut app: App) -> Result<(
188132 }
189133}
190134
191fn ui<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> Result<(), Box<dyn Error>> {
192 terminal.draw(|mut frame| {
193 app.render(&mut frame);
194 })?;
135fn ui<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> (App, Result<(), Box<dyn Error>>) {
136 let (send, recv) = channel();
137 let res = terminal.draw(|mut frame| {
138 let app = app.render(&mut frame);
139 send.send(app).unwrap();
140 });
141
142 app = recv.recv().unwrap();
195143
196144 *app.message.lock().unwrap() = None;
197145 app.render = false;
198146
199 Ok(())
147 match res {
148 Ok(_) => (app, Ok(())),
149 Err(e) => (app, Err(Box::new(e))),
150 }
200151}
201152
202153fn init() -> Result<(), Box<dyn Error>> {
src/structs/page.rs +19 −2
@@ -1,5 +1,11 @@
1use crate::app::pages::{
2 channel::ChannelPage, item_info::DisplayItem, main_menu::MainMenuSelector,
1use crate::{
2 app::pages::{
3 channel::{Channel, ChannelPage},
4 item_info::{DisplayItem, ItemInfo},
5 main_menu::{MainMenu, MainMenuSelector},
6 search::Search,
7 },
8 traits::PageTrait,
39};
410
511#[derive(Debug, PartialEq, Clone)]
@@ -15,3 +21,14 @@ impl Default for Page {
1521 Self::MainMenu(MainMenuSelector::default())
1622 }
1723}
24
25impl Page {
26 pub fn message(&self) -> String {
27 match self {
28 Self::MainMenu(_) => MainMenu::message(),
29 Self::ItemDisplay(_) => ItemInfo::message(),
30 Self::Channel(_, _) => Channel::message(),
31 Self::Search => Search::message(),
32 }
33 }
34}
src/structs/states.rs +84 −4
@@ -1,10 +1,25 @@
1use tui::layout::Constraint;
1use std::error::Error;
22
3use crate::app::pages::{
4 channel::ChannelItem, global::GlobalItem, item_info::ItemInfoItem, main_menu::MainMenuItem,
5 search::SearchItem,
3use crossterm::event::KeyCode;
4use tui::{
5 backend::Backend,
6 layout::{Constraint, Rect},
7 Frame,
68};
79
10use crate::{
11 app::{
12 app::App,
13 pages::{
14 channel::ChannelItem, global::GlobalItem, item_info::ItemInfoItem,
15 main_menu::MainMenuItem, search::SearchItem,
16 },
17 },
18 traits::ItemTrait,
19};
20
21use super::WatchHistory;
22
823#[derive(Debug, Clone)]
924pub enum Item {
1025 Global(GlobalItem),
@@ -14,6 +29,71 @@ pub enum Item {
1429 Channel(ChannelItem),
1530}
1631
32impl Item {
33 pub fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App) {
34 match self {
35 Item::Global(item) => item.key_input(key, app),
36 Item::MainMenu(item) => item.key_input(key, app),
37 Item::ItemInfo(item) => item.key_input(key, app),
38 Item::Search(item) => item.key_input(key, app),
39 Item::Channel(item) => item.key_input(key, app),
40 }
41 }
42
43 pub fn render_item<B: Backend>(
44 &self,
45 frame: &mut Frame<B>,
46 rect: Rect,
47 app: App,
48 selected: bool,
49 hover: bool,
50 popup_focus: bool,
51 popup_render: bool,
52 ) -> (bool, Option<Item>, App) {
53 match self {
54 Item::Global(item) => {
55 item.render_item(frame, rect, app, selected, hover, popup_focus, popup_render)
56 }
57 Item::MainMenu(item) => {
58 item.render_item(frame, rect, app, selected, hover, popup_focus, popup_render)
59 }
60 Item::ItemInfo(item) => {
61 item.render_item(frame, rect, app, selected, hover, popup_focus, popup_render)
62 }
63 Item::Search(item) => {
64 item.render_item(frame, rect, app, selected, hover, popup_focus, popup_render)
65 }
66 Item::Channel(item) => {
67 item.render_item(frame, rect, app, selected, hover, popup_focus, popup_render)
68 }
69 }
70 }
71
72 pub fn select(&mut self, app: App) -> (App, bool) {
73 match self {
74 Item::Global(item) => item.select(app),
75 Item::MainMenu(item) => item.select(app),
76 Item::ItemInfo(item) => item.select(app),
77 Item::Search(item) => item.select(app),
78 Item::Channel(item) => item.select(app),
79 }
80 }
81
82 pub fn load_item(
83 &self,
84 app: &App,
85 watchhistory: &mut WatchHistory,
86 ) -> Result<Self, Box<dyn Error>> {
87 match self {
88 Item::Global(item) => item.load_item(app, watchhistory),
89 Item::MainMenu(item) => item.load_item(app, watchhistory),
90 Item::ItemInfo(item) => item.load_item(app, watchhistory),
91 Item::Search(item) => item.load_item(app, watchhistory),
92 Item::Channel(item) => item.load_item(app, watchhistory),
93 }
94 }
95}
96
1797#[derive(Debug, Clone)]
1898pub struct Row {
1999 pub items: Vec<RowItem>,
src/traits/item.rs added +29
@@ -0,0 +1,29 @@
1use std::error::Error;
2
3use crate::{
4 app::app::App,
5 structs::{Item, WatchHistory},
6};
7use crossterm::event::KeyCode;
8use tui::{backend::Backend, layout::Rect, Frame};
9
10pub trait ItemTrait {
11 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App);
12 fn render_item<B: Backend>(
13 &self,
14 frame: &mut Frame<B>,
15 rect: Rect,
16 app: App,
17 selected: bool,
18 hover: bool,
19 popup_focus: bool,
20 popup_render: bool,
21 ) -> (bool, Option<Item>, App);
22 fn select(&mut self, app: App) -> (App, bool);
23 fn selectable(&self) -> bool;
24 fn load_item(
25 &self,
26 app: &App,
27 watch_history: &mut WatchHistory,
28 ) -> Result<Item, Box<dyn Error>>;
29}
src/traits/key_input.rs deleted −6
@@ -1,6 +0,0 @@
1use crate::app::app::App;
2use crossterm::event::KeyCode;
3
4pub trait KeyInput {
5 fn key_input(&mut self, key: KeyCode, app: App) -> (bool, App);
6}
src/traits/mod.rs +4 −4
@@ -1,5 +1,5 @@
1mod select_item;
2pub use select_item::*;
1mod item;
2pub use item::*;
33
4mod key_input;
5pub use key_input::*;
4mod page;
5pub use page::*;
src/traits/page.rs added +11
@@ -0,0 +1,11 @@
1use crate::structs::Row;
2
3pub trait PageTrait {
4 fn message() -> String {
5 String::from("Loading page...")
6 }
7
8 fn min() -> (u16, u16);
9
10 fn default() -> Vec<Row>;
11}
src/traits/render_item.rs deleted −7
@@ -1,7 +0,0 @@
1use tui::{backend::Backend, layout::Rect, Frame};
2
3use crate::app::app::App;
4
5pub trait RenderItem {
6 fn render_item<B: Backend>(&mut self, frame: &mut Frame<B>, rect: Rect, selected: bool, hover: bool);
7}
\ No newline at end of file
src/traits/select_item.rs deleted −6
@@ -1,6 +0,0 @@
1use crate::app::app::App;
2
3pub trait SelectItem {
4 fn select(&mut self, app: App) -> (App, bool);
5 fn selectable(&self) -> bool;
6}
src/widgets/text_list.rs +3
@@ -15,6 +15,7 @@ pub struct TextList {
1515 pub selected: usize,
1616 pub scroll: usize,
1717 pub area: Option<Rect>,
18 pub prev_area: Option<Rect>,
1819 pub style: Style,
1920 pub selected_style: Style,
2021}
@@ -119,6 +120,7 @@ impl Default for TextList {
119120 selected: 0,
120121 scroll: 0,
121122 area: None,
123 prev_area: None,
122124 style: Style::default(),
123125 selected_style: Style::default().fg(Color::Yellow),
124126 }
@@ -127,6 +129,7 @@ impl Default for TextList {
127129
128130impl TextList {
129131 pub fn area(&mut self, area: Rect) {
132 self.prev_area = self.area;
130133 self.area = Some(area);
131134 }
132135