Commit 262311623d
Unsigned
Layout: unified · split
Cargo.lock +58 −64
| @@ -66,22 +66,6 @@ version = "0.8.3" | |||
| 66 | source = "registry+https://github.com/rust-lang/crates.io-index" | 66 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 67 | checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" | 67 | checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc" |
| 68 | 68 | ||
| 69 | [[package]] | ||
| 70 | name = "crossterm" | ||
| 71 | version = "0.22.1" | ||
| 72 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 73 | checksum = "c85525306c4291d1b73ce93c8acf9c339f9b213aef6c1d85c3830cbf1c16325c" | ||
| 74 | dependencies = [ | ||
| 75 | "bitflags", | ||
| 76 | "crossterm_winapi", | ||
| 77 | "libc", | ||
| 78 | "mio 0.7.14", | ||
| 79 | "parking_lot 0.11.2", | ||
| 80 | "signal-hook", | ||
| 81 | "signal-hook-mio", | ||
| 82 | "winapi", | ||
| 83 | ] | ||
| 84 | |||
| 85 | [[package]] | 69 | [[package]] |
| 86 | name = "crossterm" | 70 | name = "crossterm" |
| 87 | version = "0.23.2" | 71 | version = "0.23.2" |
| @@ -91,8 +75,8 @@ dependencies = [ | |||
| 91 | "bitflags", | 75 | "bitflags", |
| 92 | "crossterm_winapi", | 76 | "crossterm_winapi", |
| 93 | "libc", | 77 | "libc", |
| 94 | "mio 0.8.2", | 78 | "mio", |
| 95 | "parking_lot 0.12.0", | 79 | "parking_lot", |
| 96 | "signal-hook", | 80 | "signal-hook", |
| 97 | "signal-hook-mio", | 81 | "signal-hook-mio", |
| 98 | "winapi", | 82 | "winapi", |
| @@ -156,6 +140,21 @@ dependencies = [ | |||
| 156 | "percent-encoding", | 140 | "percent-encoding", |
| 157 | ] | 141 | ] |
| 158 | 142 | ||
| 143 | [[package]] | ||
| 144 | name = "futures" | ||
| 145 | version = "0.3.21" | ||
| 146 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 147 | checksum = "f73fe65f54d1e12b726f517d3e2135ca3125a437b6d998caf1962961f7172d9e" | ||
| 148 | dependencies = [ | ||
| 149 | "futures-channel", | ||
| 150 | "futures-core", | ||
| 151 | "futures-executor", | ||
| 152 | "futures-io", | ||
| 153 | "futures-sink", | ||
| 154 | "futures-task", | ||
| 155 | "futures-util", | ||
| 156 | ] | ||
| 157 | |||
| 159 | [[package]] | 158 | [[package]] |
| 160 | name = "futures-channel" | 159 | name = "futures-channel" |
| 161 | version = "0.3.21" | 160 | version = "0.3.21" |
| @@ -163,6 +162,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
| 163 | checksum = "c3083ce4b914124575708913bca19bfe887522d6e2e6d0952943f5eac4a74010" | 162 | checksum = "c3083ce4b914124575708913bca19bfe887522d6e2e6d0952943f5eac4a74010" |
| 164 | dependencies = [ | 163 | dependencies = [ |
| 165 | "futures-core", | 164 | "futures-core", |
| 165 | "futures-sink", | ||
| 166 | ] | 166 | ] |
| 167 | 167 | ||
| 168 | [[package]] | 168 | [[package]] |
| @@ -171,12 +171,34 @@ version = "0.3.21" | |||
| 171 | source = "registry+https://github.com/rust-lang/crates.io-index" | 171 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 172 | checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3" | 172 | checksum = "0c09fd04b7e4073ac7156a9539b57a484a8ea920f79c7c675d05d289ab6110d3" |
| 173 | 173 | ||
| 174 | [[package]] | ||
| 175 | name = "futures-executor" | ||
| 176 | version = "0.3.21" | ||
| 177 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 178 | checksum = "9420b90cfa29e327d0429f19be13e7ddb68fa1cccb09d65e5706b8c7a749b8a6" | ||
| 179 | dependencies = [ | ||
| 180 | "futures-core", | ||
| 181 | "futures-task", | ||
| 182 | "futures-util", | ||
| 183 | ] | ||
| 184 | |||
| 174 | [[package]] | 185 | [[package]] |
| 175 | name = "futures-io" | 186 | name = "futures-io" |
| 176 | version = "0.3.21" | 187 | version = "0.3.21" |
| 177 | source = "registry+https://github.com/rust-lang/crates.io-index" | 188 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 178 | checksum = "fc4045962a5a5e935ee2fdedaa4e08284547402885ab326734432bed5d12966b" | 189 | checksum = "fc4045962a5a5e935ee2fdedaa4e08284547402885ab326734432bed5d12966b" |
| 179 | 190 | ||
| 191 | [[package]] | ||
| 192 | name = "futures-macro" | ||
| 193 | version = "0.3.21" | ||
| 194 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 195 | checksum = "33c1e13800337f4d4d7a316bf45a567dbcb6ffe087f16424852d97e97a91f512" | ||
| 196 | dependencies = [ | ||
| 197 | "proc-macro2", | ||
| 198 | "quote", | ||
| 199 | "syn", | ||
| 200 | ] | ||
| 201 | |||
| 180 | [[package]] | 202 | [[package]] |
| 181 | name = "futures-sink" | 203 | name = "futures-sink" |
| 182 | version = "0.3.21" | 204 | version = "0.3.21" |
| @@ -195,8 +217,11 @@ version = "0.3.21" | |||
| 195 | source = "registry+https://github.com/rust-lang/crates.io-index" | 217 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 196 | checksum = "d8b7abd5d659d9b90c8cba917f6ec750a74e2dc23902ef9cd4cc8c8b22e6036a" | 218 | checksum = "d8b7abd5d659d9b90c8cba917f6ec750a74e2dc23902ef9cd4cc8c8b22e6036a" |
| 197 | dependencies = [ | 219 | dependencies = [ |
| 220 | "futures-channel", | ||
| 198 | "futures-core", | 221 | "futures-core", |
| 199 | "futures-io", | 222 | "futures-io", |
| 223 | "futures-macro", | ||
| 224 | "futures-sink", | ||
| 200 | "futures-task", | 225 | "futures-task", |
| 201 | "memchr", | 226 | "memchr", |
| 202 | "pin-project-lite", | 227 | "pin-project-lite", |
| @@ -238,6 +263,12 @@ dependencies = [ | |||
| 238 | "libc", | 263 | "libc", |
| 239 | ] | 264 | ] |
| 240 | 265 | ||
| 266 | [[package]] | ||
| 267 | name = "hrtime" | ||
| 268 | version = "0.2.0" | ||
| 269 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 270 | checksum = "2a80870ee775a6b862265eb04a8d61d1b1ef8e4c423154a52ba6a1e3ff24836c" | ||
| 271 | |||
| 241 | [[package]] | 272 | [[package]] |
| 242 | name = "http" | 273 | name = "http" |
| 243 | version = "0.2.6" | 274 | version = "0.2.6" |
| @@ -420,19 +451,6 @@ version = "0.3.16" | |||
| 420 | source = "registry+https://github.com/rust-lang/crates.io-index" | 451 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 421 | checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" | 452 | checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" |
| 422 | 453 | ||
| 423 | [[package]] | ||
| 424 | name = "mio" | ||
| 425 | version = "0.7.14" | ||
| 426 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 427 | checksum = "8067b404fe97c70829f082dec8bcf4f71225d7eaea1d8645349cb76fa06205cc" | ||
| 428 | dependencies = [ | ||
| 429 | "libc", | ||
| 430 | "log", | ||
| 431 | "miow", | ||
| 432 | "ntapi", | ||
| 433 | "winapi", | ||
| 434 | ] | ||
| 435 | |||
| 436 | [[package]] | 454 | [[package]] |
| 437 | name = "mio" | 455 | name = "mio" |
| 438 | version = "0.8.2" | 456 | version = "0.8.2" |
| @@ -532,17 +550,6 @@ dependencies = [ | |||
| 532 | "vcpkg", | 550 | "vcpkg", |
| 533 | ] | 551 | ] |
| 534 | 552 | ||
| 535 | [[package]] | ||
| 536 | name = "parking_lot" | ||
| 537 | version = "0.11.2" | ||
| 538 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 539 | checksum = "7d17b78036a60663b797adeaee46f5c9dfebb86948d1255007a1d6be0271ff99" | ||
| 540 | dependencies = [ | ||
| 541 | "instant", | ||
| 542 | "lock_api", | ||
| 543 | "parking_lot_core 0.8.5", | ||
| 544 | ] | ||
| 545 | |||
| 546 | [[package]] | 553 | [[package]] |
| 547 | name = "parking_lot" | 554 | name = "parking_lot" |
| 548 | version = "0.12.0" | 555 | version = "0.12.0" |
| @@ -550,21 +557,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
| 550 | checksum = "87f5ec2493a61ac0506c0f4199f99070cbe83857b0337006a30f3e6719b8ef58" | 557 | checksum = "87f5ec2493a61ac0506c0f4199f99070cbe83857b0337006a30f3e6719b8ef58" |
| 551 | dependencies = [ | 558 | dependencies = [ |
| 552 | "lock_api", | 559 | "lock_api", |
| 553 | "parking_lot_core 0.9.2", | 560 | "parking_lot_core", |
| 554 | ] | ||
| 555 | |||
| 556 | [[package]] | ||
| 557 | name = "parking_lot_core" | ||
| 558 | version = "0.8.5" | ||
| 559 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
| 560 | checksum = "d76e8e1493bcac0d2766c42737f34458f1c8c50c0d23bcb24ea953affb273216" | ||
| 561 | dependencies = [ | ||
| 562 | "cfg-if", | ||
| 563 | "instant", | ||
| 564 | "libc", | ||
| 565 | "redox_syscall", | ||
| 566 | "smallvec", | ||
| 567 | "winapi", | ||
| 568 | ] | 561 | ] |
| 569 | 562 | ||
| 570 | [[package]] | 563 | [[package]] |
| @@ -781,8 +774,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" | |||
| 781 | checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af" | 774 | checksum = "29ad2e15f37ec9a6cc544097b78a1ec90001e9f71b81338ca39f430adaca99af" |
| 782 | dependencies = [ | 775 | dependencies = [ |
| 783 | "libc", | 776 | "libc", |
| 784 | "mio 0.7.14", | 777 | "mio", |
| 785 | "mio 0.8.2", | ||
| 786 | "signal-hook", | 778 | "signal-hook", |
| 787 | ] | 779 | ] |
| 788 | 780 | ||
| @@ -866,7 +858,7 @@ dependencies = [ | |||
| 866 | "bytes", | 858 | "bytes", |
| 867 | "libc", | 859 | "libc", |
| 868 | "memchr", | 860 | "memchr", |
| 869 | "mio 0.8.2", | 861 | "mio", |
| 870 | "num_cpus", | 862 | "num_cpus", |
| 871 | "pin-project-lite", | 863 | "pin-project-lite", |
| 872 | "socket2", | 864 | "socket2", |
| @@ -943,13 +935,13 @@ checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" | |||
| 943 | 935 | ||
| 944 | [[package]] | 936 | [[package]] |
| 945 | name = "tui" | 937 | name = "tui" |
| 946 | version = "0.17.0" | 938 | version = "0.18.0" |
| 947 | source = "registry+https://github.com/rust-lang/crates.io-index" | 939 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 948 | checksum = "23ed0a32c88b039b73f1b6c5acbd0554bfa5b6be94467375fd947c4de3a02271" | 940 | checksum = "96fe69244ec2af261bced1d9046a6fee6c8c2a6b0228e59e5ba39bc8ba4ed729" |
| 949 | dependencies = [ | 941 | dependencies = [ |
| 950 | "bitflags", | 942 | "bitflags", |
| 951 | "cassowary", | 943 | "cassowary", |
| 952 | "crossterm 0.22.1", | 944 | "crossterm", |
| 953 | "unicode-segmentation", | 945 | "unicode-segmentation", |
| 954 | "unicode-width", | 946 | "unicode-width", |
| 955 | ] | 947 | ] |
| @@ -1175,7 +1167,9 @@ dependencies = [ | |||
| 1175 | name = "youtube-tui" | 1167 | name = "youtube-tui" |
| 1176 | version = "0.1.0" | 1168 | version = "0.1.0" |
| 1177 | dependencies = [ | 1169 | dependencies = [ |
| 1178 | "crossterm 0.23.2", | 1170 | "crossterm", |
| 1171 | "futures", | ||
| 1172 | "hrtime", | ||
| 1179 | "invidious", | 1173 | "invidious", |
| 1180 | "tui", | 1174 | "tui", |
| 1181 | ] | 1175 | ] |
Cargo.toml +3 −1
| @@ -6,9 +6,11 @@ edition = "2021" | |||
| 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | 6 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html |
| 7 | 7 | ||
| 8 | [dependencies] | 8 | [dependencies] |
| 9 | tui = "0.17" | 9 | tui = "0.18" |
| 10 | crossterm = "0.23" | 10 | crossterm = "0.23" |
| 11 | invidious = "0.1.0" | 11 | invidious = "0.1.0" |
| 12 | futures = "0.3" | ||
| 13 | hrtime = "0.2" | ||
| 12 | 14 | ||
| 13 | [profile.dev.package."*"] | 15 | [profile.dev.package."*"] |
| 14 | opt-level = 3 | 16 | opt-level = 3 |
README.md added +1
| @@ -0,0 +1 @@ | |||
| 1 | Very bad coding, but if it works, it works.\ No newline at end of file | ||
src/app/app.rs +161 −13
| @@ -1,23 +1,177 @@ | |||
| 1 | use crate::app::pages::main_menu::*; | 1 | use crate::{ |
| 2 | app::pages::{global::*, main_menu::*}, | ||
| 3 | traits::{KeyInput, SelectItem}, | ||
| 4 | }; | ||
| 5 | use crossterm::event::KeyCode; | ||
| 2 | use invidious::blocking::Client; | 6 | use invidious::blocking::Client; |
| 3 | use tui::layout::Constraint; | 7 | use tui::layout::Constraint; |
| 4 | 8 | ||
| 5 | #[derive(Debug)] | 9 | #[derive(Debug)] |
| 6 | pub struct App { | 10 | pub struct App { |
| 7 | pub page: Page, | 11 | pub page: Page, |
| 8 | pub state: Vec<Row>, // Item, Selectable | 12 | pub state: Vec<Row>, // Item |
| 9 | pub selected: Option<Item>, | 13 | pub selectable: Vec<Vec<(usize, usize)>>, |
| 14 | pub hover: Option<(usize, usize)>, // x, y | ||
| 15 | pub selected: Option<(usize, usize)>, | ||
| 10 | pub client: Client, | 16 | pub client: Client, |
| 17 | pub message: Option<String>, | ||
| 18 | pub load: bool, | ||
| 11 | } | 19 | } |
| 12 | 20 | ||
| 13 | impl App { | 21 | impl App { |
| 14 | pub fn new() -> Self { | 22 | pub fn new() -> Self { |
| 23 | let state = MainMenu::default(); | ||
| 24 | let selectable = App::selectable(&state); | ||
| 15 | Self { | 25 | Self { |
| 16 | page: Page::default(), | 26 | page: Page::default(), |
| 17 | selected: None, | 27 | state, |
| 18 | state: MainMenu::default(), | 28 | selectable, |
| 19 | client: Client::new(String::from("https://vid.puffyan.us")), | 29 | client: Client::new(String::from("https://vid.puffyan.us")), |
| 30 | selected: None, | ||
| 31 | hover: None, | ||
| 32 | message: None, | ||
| 33 | load: true, | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | pub fn selectable(state: &Vec<Row>) -> Vec<Vec<(usize, usize)>> { | ||
| 38 | let mut selectable = Vec::new(); | ||
| 39 | |||
| 40 | for (y, row) in state.iter().enumerate() { | ||
| 41 | let mut row_vec = Vec::new(); | ||
| 42 | for (x, row_item) in row.items.iter().enumerate() { | ||
| 43 | if match &row_item.item { | ||
| 44 | Item::Global(item) => item.selectable(), | ||
| 45 | Item::MainMenu(item) => item.selectable(), | ||
| 46 | } { | ||
| 47 | row_vec.push((x, y)); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | if row_vec.len() != 0 { | ||
| 51 | selectable.push(row_vec); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | |||
| 55 | selectable | ||
| 56 | } | ||
| 57 | |||
| 58 | pub fn key_input(mut self, key: KeyCode) -> App { | ||
| 59 | if let Some((x, y)) = self.selected { | ||
| 60 | if key != KeyCode::Esc { | ||
| 61 | match &mut self.state[y].items[x].item { | ||
| 62 | Item::Global(item) => { | ||
| 63 | let mut item = item.clone(); | ||
| 64 | self = item.key_input(key, self); | ||
| 65 | self.state[y].items[x].item = Item::Global(item); | ||
| 66 | } | ||
| 67 | _ => {} | ||
| 68 | } | ||
| 69 | |||
| 70 | return self; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | |||
| 74 | match key { | ||
| 75 | KeyCode::Enter => { | ||
| 76 | if self.hover.is_some() && self.selected.is_none() { | ||
| 77 | let (mut x, mut y) = self.hover.unwrap(); | ||
| 78 | (x, y) = self.selectable[y][x]; | ||
| 79 | match self.state[y] | ||
| 80 | .items | ||
| 81 | .iter() | ||
| 82 | .nth(x) | ||
| 83 | .clone() | ||
| 84 | .unwrap() | ||
| 85 | .item | ||
| 86 | .clone() | ||
| 87 | { | ||
| 88 | Item::Global(mut item) => { | ||
| 89 | let held = item.select(self); | ||
| 90 | self = held.0; | ||
| 91 | if held.1 { | ||
| 92 | self.selected = Some((x, y)); | ||
| 93 | } | ||
| 94 | } | ||
| 95 | Item::MainMenu(mut item) => { | ||
| 96 | let held = item.select(self); | ||
| 97 | self = held.0; | ||
| 98 | if held.1 { | ||
| 99 | self.selected = Some((x, y)); | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | return self; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | KeyCode::Esc => { | ||
| 108 | if self.selected.is_some() { | ||
| 109 | self.selected = None; | ||
| 110 | return self; | ||
| 111 | } | ||
| 112 | } | ||
| 113 | |||
| 114 | _ => {} | ||
| 115 | } | ||
| 116 | |||
| 117 | match &mut self.hover { | ||
| 118 | Some((x, y)) => match key { | ||
| 119 | KeyCode::Up => { | ||
| 120 | if *y > 0 { | ||
| 121 | let temp_y = *y - 1; | ||
| 122 | if *x > self.selectable[temp_y].len() { | ||
| 123 | let temp_x = self.selectable[temp_y].len(); | ||
| 124 | if temp_x > self.selectable[*y].len() - 1 { | ||
| 125 | *x = self.selectable[*y].len() - 1; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | *y -= 1; | ||
| 129 | if *x > self.selectable[*y].len() - 1 { | ||
| 130 | *x = self.selectable[*y].len() - 1; | ||
| 131 | } | ||
| 132 | } | ||
| 133 | } | ||
| 134 | KeyCode::Down => { | ||
| 135 | if *y < self.selectable.len() - 1 { | ||
| 136 | *y += 1; | ||
| 137 | if *x > self.selectable[*y].len() - 1 { | ||
| 138 | *x = self.selectable[*y].len() - 1; | ||
| 139 | } | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | KeyCode::Left => { | ||
| 144 | if *x > 0 { | ||
| 145 | *x -= 1; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | |||
| 149 | KeyCode::Right => { | ||
| 150 | if *x < self.selectable[*y].len() - 1 { | ||
| 151 | *x += 1; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | |||
| 155 | _ => {} | ||
| 156 | }, | ||
| 157 | None => match key { | ||
| 158 | KeyCode::Up => { | ||
| 159 | self.hover = Some((0, 0)); | ||
| 160 | } | ||
| 161 | KeyCode::Down => { | ||
| 162 | self.hover = Some((0, self.selectable.len() - 1)); | ||
| 163 | } | ||
| 164 | KeyCode::Left => { | ||
| 165 | self.hover = Some((0, 0)); | ||
| 166 | } | ||
| 167 | KeyCode::Right => { | ||
| 168 | self.hover = Some((0, self.selectable.len() - 1)); | ||
| 169 | } | ||
| 170 | _ => {} | ||
| 171 | }, | ||
| 20 | } | 172 | } |
| 173 | |||
| 174 | self | ||
| 21 | } | 175 | } |
| 22 | } | 176 | } |
| 23 | 177 | ||
| @@ -34,18 +188,12 @@ impl Page { | |||
| 34 | } | 188 | } |
| 35 | } | 189 | } |
| 36 | 190 | ||
| 37 | #[derive(Debug)] | 191 | #[derive(Debug, Clone)] |
| 38 | pub enum Item { | 192 | pub enum Item { |
| 39 | Global(GlobalItem), | 193 | Global(GlobalItem), |
| 40 | MainMenu(MainMenuItem), | 194 | MainMenu(MainMenuItem), |
| 41 | } | 195 | } |
| 42 | 196 | ||
| 43 | #[derive(Debug)] | ||
| 44 | pub enum GlobalItem { | ||
| 45 | SearchBar(Vec<char>), | ||
| 46 | MessageBar(Option<String>), | ||
| 47 | } | ||
| 48 | |||
| 49 | #[derive(Debug)] | 197 | #[derive(Debug)] |
| 50 | pub struct Row { | 198 | pub struct Row { |
| 51 | pub items: Vec<RowItem>, | 199 | pub items: Vec<RowItem>, |
| @@ -57,4 +205,4 @@ pub struct Row { | |||
| 57 | pub struct RowItem { | 205 | pub struct RowItem { |
| 58 | pub item: Item, | 206 | pub item: Item, |
| 59 | pub constraint: Constraint, | 207 | pub constraint: Constraint, |
| 60 | }\ No newline at end of file | 208 | } |
src/app/pages/global.rs added +86
| @@ -0,0 +1,86 @@ | |||
| 1 | use crossterm::event::KeyCode; | ||
| 2 | use invidious::structs::{hidden::TrendingVideo, video::Video}; | ||
| 3 | |||
| 4 | use crate::{ | ||
| 5 | app::app::App, | ||
| 6 | traits::{KeyInput, SelectItem}, | ||
| 7 | }; | ||
| 8 | |||
| 9 | #[derive(Debug, Clone)] | ||
| 10 | pub enum GlobalItem { | ||
| 11 | SearchBar(String), | ||
| 12 | MessageBar, | ||
| 13 | } | ||
| 14 | |||
| 15 | impl SelectItem for GlobalItem { | ||
| 16 | fn select(&mut self, app: App) -> (App, bool) { | ||
| 17 | let selected = match self { | ||
| 18 | GlobalItem::SearchBar(_) => true, | ||
| 19 | _ => false, | ||
| 20 | }; | ||
| 21 | |||
| 22 | (app, selected) | ||
| 23 | } | ||
| 24 | |||
| 25 | fn selectable(&self) -> bool { | ||
| 26 | match self { | ||
| 27 | GlobalItem::SearchBar(_) => true, | ||
| 28 | _ => false, | ||
| 29 | } | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | impl KeyInput for GlobalItem { | ||
| 34 | fn key_input(&mut self, key: KeyCode, app: App) -> App { | ||
| 35 | match self { | ||
| 36 | GlobalItem::SearchBar(search_bar) => match key { | ||
| 37 | KeyCode::Char(c) => { | ||
| 38 | search_bar.push(c); | ||
| 39 | } | ||
| 40 | KeyCode::Backspace => { | ||
| 41 | search_bar.pop(); | ||
| 42 | } | ||
| 43 | KeyCode::Enter => {} | ||
| 44 | _ => {} | ||
| 45 | }, | ||
| 46 | _ => {} | ||
| 47 | } | ||
| 48 | |||
| 49 | app | ||
| 50 | } | ||
| 51 | } | ||
| 52 | |||
| 53 | #[derive(Debug, Clone)] | ||
| 54 | pub enum ListItem { | ||
| 55 | Video(MiniVideo), | ||
| 56 | PageTurner(bool), // true: +1 | false: -1 | ||
| 57 | } | ||
| 58 | |||
| 59 | #[derive(Debug, Clone)] | ||
| 60 | pub struct MiniVideo { | ||
| 61 | pub title: String, | ||
| 62 | pub video_id: String, | ||
| 63 | pub video_thumbnail: String, | ||
| 64 | pub length: String, | ||
| 65 | pub view_count: u64, | ||
| 66 | pub author: String, | ||
| 67 | pub author_url: String, | ||
| 68 | pub published: String, | ||
| 69 | pub description: String, | ||
| 70 | } | ||
| 71 | |||
| 72 | impl From<TrendingVideo> for MiniVideo { | ||
| 73 | fn from(original: TrendingVideo) -> Self { | ||
| 74 | MiniVideo { | ||
| 75 | title: original.title, | ||
| 76 | video_id: original.videoId, | ||
| 77 | video_thumbnail: original.videoThumbnails[0].url.clone(), | ||
| 78 | length: hrtime::from_sec(original.lengthSeconds as u64), | ||
| 79 | view_count: original.viewCount, | ||
| 80 | author: original.author, | ||
| 81 | author_url: original.authorUrl, | ||
| 82 | published: original.publishedText, | ||
| 83 | description: original.description, | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
src/app/pages/main_menu.rs +131 −29
| @@ -1,15 +1,121 @@ | |||
| 1 | use crate::app::app::{GlobalItem, Item, Row, RowItem}; | 1 | use std::{collections::LinkedList, error::Error}; |
| 2 | use invidious::structs::video::Video; | ||
| 3 | use tui::layout::Constraint; | ||
| 4 | 2 | ||
| 5 | #[derive(Debug)] | 3 | use crate::{ |
| 4 | app::{ | ||
| 5 | app::{App, Item, Page, Row, RowItem}, | ||
| 6 | pages::global::{GlobalItem, ListItem, MiniVideo}, | ||
| 7 | }, | ||
| 8 | traits::{KeyInput, LoadItem, SelectItem}, | ||
| 9 | widgets::text_list::TextList, | ||
| 10 | }; | ||
| 11 | use crossterm::event::KeyCode; | ||
| 12 | use invidious::{blocking::Client, structs::video::Video}; | ||
| 13 | use tui::layout::Constraint; | ||
| 14 | #[derive(Debug, Clone)] | ||
| 6 | pub enum MainMenuItem { | 15 | pub enum MainMenuItem { |
| 7 | SeletorTab(MainMenuSelector), | 16 | SeletorTab(MainMenuSelector), |
| 8 | VideoList(Option<Vec<Video>>, Option<usize>), // Videos, index | 17 | VideoList(Option<(LinkedList<ListItem>, TextList, usize)>), // Videos, List, page |
| 9 | VideoDetails(Option<Video>), // Single video | 18 | } |
| 19 | |||
| 20 | // app.client.trending(None) | ||
| 21 | |||
| 22 | impl SelectItem for MainMenuItem { | ||
| 23 | fn select(&mut self, mut app: App) -> (App, bool) { | ||
| 24 | let selected = match self { | ||
| 25 | MainMenuItem::SeletorTab(selector) => { | ||
| 26 | app.page = Page::MainMenu { tab: *selector }; | ||
| 27 | false | ||
| 28 | } | ||
| 29 | |||
| 30 | _ => true, | ||
| 31 | }; | ||
| 32 | |||
| 33 | (app, selected) | ||
| 34 | } | ||
| 35 | |||
| 36 | fn selectable(&self) -> bool { | ||
| 37 | true | ||
| 38 | } | ||
| 39 | } | ||
| 40 | |||
| 41 | impl KeyInput for MainMenuItem { | ||
| 42 | fn key_input(&mut self, key: KeyCode, app: App) -> App { | ||
| 43 | match self { | ||
| 44 | _ => {} | ||
| 45 | } | ||
| 46 | |||
| 47 | app | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | impl LoadItem for MainMenuItem { | ||
| 52 | fn load_item(&self, app: &App) -> Result<Box<Self>, Box<dyn Error>> { | ||
| 53 | let mut this = self.clone(); | ||
| 54 | |||
| 55 | match &mut this { | ||
| 56 | MainMenuItem::VideoList(enum_items) => match app.page { | ||
| 57 | Page::MainMenu { tab } => match tab { | ||
| 58 | MainMenuSelector::Trending => { | ||
| 59 | let mut list: LinkedList<ListItem> = app | ||
| 60 | .client | ||
| 61 | .trending(None)? | ||
| 62 | .videos | ||
| 63 | .into_iter() | ||
| 64 | .map(|t| ListItem::Video(t.into())) | ||
| 65 | .collect(); | ||
| 66 | |||
| 67 | list.push_back(ListItem::PageTurner(true)); | ||
| 68 | |||
| 69 | let mut text_list = TextList::default(); | ||
| 70 | |||
| 71 | if let Some((video_list, text_list, page)) = enum_items { | ||
| 72 | if *page > 0 { | ||
| 73 | list.push_front(ListItem::PageTurner(false)); | ||
| 74 | } | ||
| 75 | |||
| 76 | *text_list = TextList::default(); | ||
| 77 | text_list.items(textlist_from_video_list(&list)); | ||
| 78 | |||
| 79 | *video_list = list; | ||
| 80 | } else { | ||
| 81 | text_list.items(textlist_from_video_list(&list)); | ||
| 82 | *enum_items = Some((list, text_list, 0)); | ||
| 83 | }; | ||
| 84 | |||
| 85 | //let text_list = TextList::default().items(); | ||
| 86 | } | ||
| 87 | _ => {} | ||
| 88 | }, | ||
| 89 | }, | ||
| 90 | _ => {} | ||
| 91 | } | ||
| 92 | |||
| 93 | Ok(Box::new(this)) | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | fn textlist_from_video_list(original: &LinkedList<ListItem>) -> Vec<String> { | ||
| 98 | original | ||
| 99 | .iter() | ||
| 100 | .map(|item| match item { | ||
| 101 | ListItem::Video(video) => video | ||
| 102 | .title | ||
| 103 | .clone() | ||
| 104 | .chars() | ||
| 105 | .map(|c| if c.is_ascii() { c } else { '?' }) | ||
| 106 | .collect(), | ||
| 107 | ListItem::PageTurner(b) => { | ||
| 108 | if *b { | ||
| 109 | "Next Page".to_string() | ||
| 110 | } else { | ||
| 111 | "Previous Page".to_string() | ||
| 112 | } | ||
| 113 | } | ||
| 114 | }) | ||
| 115 | .collect() | ||
| 10 | } | 116 | } |
| 11 | 117 | ||
| 12 | #[derive(Debug)] | 118 | #[derive(Debug, Clone, Copy)] |
| 13 | pub enum MainMenuSelector { | 119 | pub enum MainMenuSelector { |
| 14 | Trending, | 120 | Trending, |
| 15 | Popular, | 121 | Popular, |
| @@ -29,45 +135,41 @@ impl MainMenu { | |||
| 29 | vec![ | 135 | vec![ |
| 30 | Row { | 136 | Row { |
| 31 | items: vec![RowItem { | 137 | items: vec![RowItem { |
| 32 | item: Item::Global(GlobalItem::SearchBar(Vec::new())), | 138 | item: Item::Global(GlobalItem::SearchBar(String::new())), |
| 33 | constraint: Constraint::Percentage(100), | 139 | constraint: Constraint::Percentage(100), |
| 34 | }], | 140 | }], |
| 35 | centered: false, | 141 | centered: false, |
| 36 | height: Constraint::Length(3), | 142 | height: Constraint::Length(3), |
| 37 | }, | 143 | }, |
| 38 | Row { | ||
| 39 | items: vec![RowItem { | ||
| 40 | item: Item::MainMenu(MainMenuItem::SeletorTab(MainMenuSelector::Trending)), | ||
| 41 | constraint: Constraint::Length(15), | ||
| 42 | }, | ||
| 43 | RowItem { | ||
| 44 | item: Item::MainMenu(MainMenuItem::SeletorTab(MainMenuSelector::Popular)), | ||
| 45 | constraint: Constraint::Length(15), | ||
| 46 | }, | ||
| 47 | RowItem { | ||
| 48 | item: Item::MainMenu(MainMenuItem::SeletorTab(MainMenuSelector::History)), | ||
| 49 | constraint: Constraint::Length(15), | ||
| 50 | }], | ||
| 51 | centered: true, | ||
| 52 | height: Constraint::Length(3), | ||
| 53 | }, | ||
| 54 | Row { | 144 | Row { |
| 55 | items: vec![ | 145 | items: vec![ |
| 56 | RowItem { | 146 | RowItem { |
| 57 | item: Item::MainMenu(MainMenuItem::VideoList(None, None)), | 147 | item: Item::MainMenu(MainMenuItem::SeletorTab(MainMenuSelector::Trending)), |
| 58 | constraint: Constraint::Percentage(40), | 148 | constraint: Constraint::Length(15), |
| 149 | }, | ||
| 150 | RowItem { | ||
| 151 | item: Item::MainMenu(MainMenuItem::SeletorTab(MainMenuSelector::Popular)), | ||
| 152 | constraint: Constraint::Length(15), | ||
| 59 | }, | 153 | }, |
| 60 | RowItem { | 154 | RowItem { |
| 61 | item: Item::MainMenu(MainMenuItem::VideoDetails(None)), | 155 | item: Item::MainMenu(MainMenuItem::SeletorTab(MainMenuSelector::History)), |
| 62 | constraint: Constraint::Percentage(60), | 156 | constraint: Constraint::Length(15), |
| 63 | }, | 157 | }, |
| 64 | ], | 158 | ], |
| 159 | centered: true, | ||
| 160 | height: Constraint::Length(3), | ||
| 161 | }, | ||
| 162 | Row { | ||
| 163 | items: vec![RowItem { | ||
| 164 | item: Item::MainMenu(MainMenuItem::VideoList(None)), | ||
| 165 | constraint: Constraint::Percentage(100), | ||
| 166 | }], | ||
| 65 | centered: false, | 167 | centered: false, |
| 66 | height: Constraint::Min(6), | 168 | height: Constraint::Min(6), |
| 67 | }, | 169 | }, |
| 68 | Row { | 170 | Row { |
| 69 | items: vec![RowItem { | 171 | items: vec![RowItem { |
| 70 | item: Item::Global(GlobalItem::MessageBar(None)), | 172 | item: Item::Global(GlobalItem::MessageBar), |
| 71 | constraint: Constraint::Percentage(100), | 173 | constraint: Constraint::Percentage(100), |
| 72 | }], | 174 | }], |
| 73 | centered: false, | 175 | centered: false, |
src/app/pages/mod.rs +2 −1
| @@ -1 +1,2 @@ | |||
| 1 | pub mod main_menu;\ No newline at end of file | 1 | pub mod main_menu; |
| 2 | pub mod global;\ No newline at end of file | ||
src/lib.rs +1
| @@ -1,3 +1,4 @@ | |||
| 1 | pub mod app; | 1 | pub mod app; |
| 2 | pub mod traits; | 2 | pub mod traits; |
| 3 | pub mod widgets; | ||
| 3 | mod render;\ No newline at end of file | 4 | mod render;\ No newline at end of file |
src/main.rs +80 −17
| @@ -1,23 +1,21 @@ | |||
| 1 | use crossterm::{ | 1 | use crossterm::{ |
| 2 | event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode}, | 2 | event::{self, DisableMouseCapture, EnableMouseCapture, KeyCode}, |
| 3 | execute, | 3 | execute, |
| 4 | terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, | 4 | terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen}, |
| 5 | }; | 5 | }; |
| 6 | use std::{error::Error, io}; | 6 | use std::{error::Error, io}; |
| 7 | use tui::{ | 7 | use tui::{ |
| 8 | backend::{Backend, CrosstermBackend}, | 8 | backend::{Backend, CrosstermBackend}, |
| 9 | layout::{Alignment, Constraint, Direction, Layout}, | 9 | Terminal, |
| 10 | style::{Color, Modifier, Style}, | 10 | }; |
| 11 | text::Span, | 11 | use youtube_tui::{ |
| 12 | widgets::{Block, BorderType, Borders}, | 12 | app::app::{App, Item, Row, RowItem}, |
| 13 | Frame, Terminal, | 13 | traits::LoadItem, |
| 14 | }; | 14 | }; |
| 15 | use youtube_tui::app::app::App; | ||
| 16 | 15 | ||
| 17 | fn main() -> Result<(), Box<dyn Error>> { | 16 | fn main() -> Result<(), Box<dyn Error>> { |
| 18 | let app = App::new(); | 17 | let app = App::new(); |
| 19 | 18 | ||
| 20 | // setup terminal | ||
| 21 | enable_raw_mode()?; | 19 | enable_raw_mode()?; |
| 22 | let mut stdout = io::stdout(); | 20 | let mut stdout = io::stdout(); |
| 23 | execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?; | 21 | execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?; |
| @@ -26,25 +24,90 @@ fn main() -> Result<(), Box<dyn Error>> { | |||
| 26 | 24 | ||
| 27 | let res = run_app(&mut terminal, app); | 25 | let res = run_app(&mut terminal, app); |
| 28 | 26 | ||
| 29 | execute!(terminal.backend_mut(), LeaveAlternateScreen, DisableMouseCapture)?; | 27 | execute!( |
| 28 | terminal.backend_mut(), | ||
| 29 | LeaveAlternateScreen, | ||
| 30 | DisableMouseCapture | ||
| 31 | )?; | ||
| 30 | disable_raw_mode()?; | 32 | disable_raw_mode()?; |
| 31 | 33 | ||
| 32 | res | 34 | res |
| 33 | } | 35 | } |
| 34 | 36 | ||
| 35 | fn run_app<B: Backend>(terminal: &mut Terminal<B>, app: App) -> Result<(), Box<dyn Error>> { | 37 | fn run_app<B: Backend>(mut terminal: &mut Terminal<B>, mut app: App) -> Result<(), Box<dyn Error>> { |
| 38 | let mut render = true; | ||
| 39 | app.message = Some(String::from("Loading videos...")); | ||
| 36 | loop { | 40 | loop { |
| 37 | terminal.draw(|mut frame| { | 41 | if render { |
| 38 | app.render(&mut frame); | 42 | ui(&mut terminal, &mut app)?; |
| 39 | })?; | 43 | app.message = None; |
| 44 | render = false; | ||
| 45 | } | ||
| 46 | |||
| 47 | if app.load { | ||
| 48 | let mut new_state = Vec::new(); | ||
| 49 | for row in app.state.iter() { | ||
| 50 | let mut row_vec = Vec::new(); | ||
| 51 | for row_item in row.items.iter() { | ||
| 52 | match &row_item.item { | ||
| 53 | Item::MainMenu(item) => match item.load_item(&app) { | ||
| 54 | Ok(new) => { | ||
| 55 | row_vec.push(RowItem { | ||
| 56 | item: Item::MainMenu(*new), | ||
| 57 | ..*row_item | ||
| 58 | }); | ||
| 59 | } | ||
| 60 | _ => { | ||
| 61 | row_vec.push(RowItem { | ||
| 62 | item: Item::MainMenu(item.clone()), | ||
| 63 | ..*row_item | ||
| 64 | }); | ||
| 65 | app.message = | ||
| 66 | Some(String::from("An error occurred while loading videos")); | ||
| 67 | } | ||
| 68 | }, | ||
| 69 | _ => { | ||
| 70 | row_vec.push(RowItem { | ||
| 71 | item: row_item.item.clone(), | ||
| 72 | ..*row_item | ||
| 73 | }); | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | new_state.push(Row { | ||
| 79 | items: row_vec, | ||
| 80 | ..*row | ||
| 81 | }); | ||
| 82 | } | ||
| 40 | 83 | ||
| 41 | if let event::Event::Key(key) = event::read()? { | 84 | app.state = new_state; |
| 42 | match key.code { | 85 | |
| 43 | KeyCode::Char('q') => break, | 86 | app.load = false; |
| 87 | render = true; | ||
| 88 | } else { | ||
| 89 | match event::read()? { | ||
| 90 | event::Event::Key(key) => { | ||
| 91 | if KeyCode::Char('q') == key.code && app.selected.is_none() { | ||
| 92 | return Ok(()); | ||
| 93 | } else { | ||
| 94 | app = app.key_input(key.code); | ||
| 95 | render = true; | ||
| 96 | } | ||
| 97 | } | ||
| 98 | event::Event::Resize(_, _) => { | ||
| 99 | render = true; | ||
| 100 | } | ||
| 44 | _ => {} | 101 | _ => {} |
| 45 | } | 102 | } |
| 46 | } | 103 | } |
| 47 | } | 104 | } |
| 105 | } | ||
| 106 | |||
| 107 | fn ui<B: Backend>(terminal: &mut Terminal<B>, app: &mut App) -> Result<(), Box<dyn Error>> { | ||
| 108 | terminal.draw(|mut frame| { | ||
| 109 | app.render(&mut frame); | ||
| 110 | })?; | ||
| 48 | 111 | ||
| 49 | Ok(()) | 112 | Ok(()) |
| 50 | }\ No newline at end of file | 113 | } |
src/render/global.rs deleted −31
| @@ -1,31 +0,0 @@ | |||
| 1 | use crate::{app::app::GlobalItem, traits::RenderItem}; | ||
| 2 | use tui::{ | ||
| 3 | backend::Backend, | ||
| 4 | layout::{Rect, Alignment}, | ||
| 5 | widgets::{Block, BorderType, Borders, Paragraph}, | ||
| 6 | Frame, | ||
| 7 | }; | ||
| 8 | |||
| 9 | impl RenderItem for GlobalItem { | ||
| 10 | fn render_item<B: Backend>(&self, frame: &mut Frame<B>, rect: Rect) { | ||
| 11 | match self { | ||
| 12 | GlobalItem::SearchBar(q) => { | ||
| 13 | let block = Block::default() | ||
| 14 | .border_type(BorderType::Rounded) | ||
| 15 | .borders(Borders::ALL) | ||
| 16 | .title("Search YouTube") | ||
| 17 | .title_alignment(Alignment::Center); | ||
| 18 | let paragraph = Paragraph::new(q.iter().collect::<String>()).block(block); | ||
| 19 | frame.render_widget(paragraph, rect); | ||
| 20 | } | ||
| 21 | GlobalItem::MessageBar(m) => { | ||
| 22 | let block = Block::default() | ||
| 23 | .border_type(BorderType::Rounded) | ||
| 24 | .borders(Borders::ALL); | ||
| 25 | let paragraph = | ||
| 26 | Paragraph::new(m.clone().unwrap_or(String::from("All good :)"))).block(block); | ||
| 27 | frame.render_widget(paragraph, rect); | ||
| 28 | } | ||
| 29 | } | ||
| 30 | } | ||
| 31 | } | ||
src/render/main_menu.rs deleted −47
| @@ -1,47 +0,0 @@ | |||
| 1 | use crate::{ | ||
| 2 | app::pages::main_menu::{MainMenuItem, MainMenuSelector}, | ||
| 3 | traits::RenderItem, | ||
| 4 | }; | ||
| 5 | use tui::{ | ||
| 6 | backend::Backend, | ||
| 7 | layout::{Rect, Alignment}, | ||
| 8 | widgets::{Block, BorderType, Borders, Paragraph}, | ||
| 9 | Frame, | ||
| 10 | }; | ||
| 11 | |||
| 12 | impl RenderItem for MainMenuItem { | ||
| 13 | fn render_item<B: Backend>(&self, frame: &mut Frame<B>, rect: Rect) { | ||
| 14 | match self { | ||
| 15 | MainMenuItem::SeletorTab(selector) => { | ||
| 16 | let text = match selector { | ||
| 17 | MainMenuSelector::Trending => "Trending", | ||
| 18 | MainMenuSelector::Popular => "Popular", | ||
| 19 | MainMenuSelector::History => "History", | ||
| 20 | }; | ||
| 21 | |||
| 22 | let block = Block::default() | ||
| 23 | .border_type(BorderType::Rounded) | ||
| 24 | .borders(Borders::ALL); | ||
| 25 | let paragraph = Paragraph::new(text).block(block).alignment(Alignment::Center); | ||
| 26 | |||
| 27 | frame.render_widget(paragraph, rect); | ||
| 28 | } | ||
| 29 | MainMenuItem::VideoList(videos, index) => { | ||
| 30 | let block = Block::default() | ||
| 31 | .border_type(BorderType::Rounded) | ||
| 32 | .borders(Borders::ALL); | ||
| 33 | let paragraph = Paragraph::new("").block(block); | ||
| 34 | |||
| 35 | frame.render_widget(paragraph, rect); | ||
| 36 | } | ||
| 37 | MainMenuItem::VideoDetails(video) => { | ||
| 38 | let block = Block::default() | ||
| 39 | .border_type(BorderType::Rounded) | ||
| 40 | .borders(Borders::ALL); | ||
| 41 | let paragraph = Paragraph::new("").block(block); | ||
| 42 | |||
| 43 | frame.render_widget(paragraph, rect); | ||
| 44 | } | ||
| 45 | } | ||
| 46 | } | ||
| 47 | } | ||
src/render/mod.rs +2 −3
| @@ -1,3 +1,2 @@ | |||
| 1 | mod main_menu; | 1 | mod pages; |
| 2 | mod render; | 2 | mod render;\ No newline at end of file |
| 3 | mod global;\ No newline at end of file | ||
src/render/pages/global.rs added +50
| @@ -0,0 +1,50 @@ | |||
| 1 | use crate::{app::{pages::global::GlobalItem, app::App}, traits::RenderItem}; | ||
| 2 | use tui::{ | ||
| 3 | backend::Backend, | ||
| 4 | layout::{Alignment, Rect}, | ||
| 5 | style::{Color, Style}, | ||
| 6 | // style::{Color, Style}, | ||
| 7 | widgets::{Block, BorderType, Borders, Paragraph}, | ||
| 8 | Frame, | ||
| 9 | }; | ||
| 10 | |||
| 11 | impl GlobalItem { | ||
| 12 | pub fn render_item<B: Backend>( | ||
| 13 | &self, | ||
| 14 | frame: &mut Frame<B>, | ||
| 15 | rect: Rect, | ||
| 16 | selected: bool, | ||
| 17 | hover: bool, | ||
| 18 | message: &Option<String>, | ||
| 19 | ) { | ||
| 20 | match self { | ||
| 21 | GlobalItem::SearchBar(search) => { | ||
| 22 | let style = Style::default().fg(if selected { | ||
| 23 | Color::LightBlue | ||
| 24 | } else if hover { | ||
| 25 | Color::LightRed | ||
| 26 | } else { | ||
| 27 | Color::Reset | ||
| 28 | }); | ||
| 29 | let block = Block::default() | ||
| 30 | .border_type(BorderType::Rounded) | ||
| 31 | .borders(Borders::ALL) | ||
| 32 | .style(style) | ||
| 33 | .title("Search YouTube") | ||
| 34 | .title_alignment(Alignment::Center); | ||
| 35 | let paragraph = Paragraph::new(search.clone()).block(block); | ||
| 36 | frame.render_widget(paragraph, rect); | ||
| 37 | } | ||
| 38 | GlobalItem::MessageBar => { | ||
| 39 | // let color = Color::LightYellow; | ||
| 40 | let block = Block::default() | ||
| 41 | .border_type(BorderType::Rounded) | ||
| 42 | .borders(Borders::ALL); | ||
| 43 | let paragraph = | ||
| 44 | Paragraph::new(message.clone().unwrap_or(String::from("All good :)"))) | ||
| 45 | .block(block); | ||
| 46 | frame.render_widget(paragraph, rect); | ||
| 47 | } | ||
| 48 | } | ||
| 49 | } | ||
| 50 | } | ||
src/render/pages/main_menu.rs added +74
| @@ -0,0 +1,74 @@ | |||
| 1 | use crate::{ | ||
| 2 | app::{pages::main_menu::{MainMenuItem, MainMenuSelector}, app::App}, | ||
| 3 | traits::RenderItem, | ||
| 4 | }; | ||
| 5 | use tui::{ | ||
| 6 | backend::Backend, | ||
| 7 | layout::{Alignment, Constraint, Layout, Rect, Direction}, | ||
| 8 | style::{Color, Style}, | ||
| 9 | widgets::{Block, BorderType, Borders, Paragraph}, | ||
| 10 | Frame, | ||
| 11 | }; | ||
| 12 | |||
| 13 | impl RenderItem for MainMenuItem { | ||
| 14 | fn render_item<B: Backend>( | ||
| 15 | &mut self, | ||
| 16 | frame: &mut Frame<B>, | ||
| 17 | rect: Rect, | ||
| 18 | selected: bool, | ||
| 19 | hover: bool, | ||
| 20 | ) { | ||
| 21 | let style = Style::default().fg(if selected { | ||
| 22 | Color::LightBlue | ||
| 23 | } else if hover { | ||
| 24 | Color::LightRed | ||
| 25 | } else { | ||
| 26 | Color::Reset | ||
| 27 | }); | ||
| 28 | |||
| 29 | match self { | ||
| 30 | MainMenuItem::SeletorTab(selector) => { | ||
| 31 | let text = match selector { | ||
| 32 | MainMenuSelector::Trending => "Trending", | ||
| 33 | MainMenuSelector::Popular => "Popular", | ||
| 34 | MainMenuSelector::History => "History", | ||
| 35 | }; | ||
| 36 | |||
| 37 | let block = Block::default() | ||
| 38 | .border_type(BorderType::Rounded) | ||
| 39 | .borders(Borders::ALL) | ||
| 40 | .border_style(style); | ||
| 41 | let paragraph = Paragraph::new(text) | ||
| 42 | .block(block) | ||
| 43 | .alignment(Alignment::Center); | ||
| 44 | |||
| 45 | frame.render_widget(paragraph, rect); | ||
| 46 | } | ||
| 47 | MainMenuItem::VideoList(data) => { | ||
| 48 | let chunks = Layout::default() | ||
| 49 | .constraints([Constraint::Percentage(40), Constraint::Percentage(60)].as_ref()) | ||
| 50 | .direction(Direction::Horizontal) | ||
| 51 | .split(rect); | ||
| 52 | |||
| 53 | let block = Block::default() | ||
| 54 | .border_type(BorderType::Rounded) | ||
| 55 | .borders(Borders::ALL) | ||
| 56 | .border_style(style); | ||
| 57 | |||
| 58 | let inner = block.inner(chunks[0]); | ||
| 59 | |||
| 60 | frame.render_widget(block, chunks[0]); | ||
| 61 | |||
| 62 | let block = Block::default() | ||
| 63 | .border_type(BorderType::Rounded) | ||
| 64 | .borders(Borders::ALL); | ||
| 65 | frame.render_widget(block, chunks[1]); | ||
| 66 | |||
| 67 | if let Some((videos, list, _)) = data { | ||
| 68 | list.area(inner); | ||
| 69 | frame.render_widget(list.clone(), inner); | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
src/render/pages/mod.rs added +2
| @@ -0,0 +1,2 @@ | |||
| 1 | pub mod global; | ||
| 2 | pub mod main_menu;\ No newline at end of file | ||
src/render/render.rs +30 −7
| @@ -6,13 +6,26 @@ use std::collections::LinkedList; | |||
| 6 | use tui::{ | 6 | use tui::{ |
| 7 | backend::Backend, | 7 | backend::Backend, |
| 8 | layout::{Constraint, Direction, Layout}, | 8 | layout::{Constraint, Direction, Layout}, |
| 9 | widgets::{Block, BorderType}, | 9 | widgets::{Block, Paragraph, Wrap}, |
| 10 | Frame, | 10 | Frame, style::{Style, Color}, |
| 11 | }; | 11 | }; |
| 12 | 12 | ||
| 13 | impl App { | 13 | impl App { |
| 14 | pub fn render<B: Backend>(&self, frame: &mut Frame<B>) { | 14 | pub fn render<B: Backend>(&mut self, frame: &mut Frame<B>) { |
| 15 | let size = frame.size(); | 15 | let size = frame.size(); |
| 16 | |||
| 17 | if size.width < 45 || size.height < 16 { | ||
| 18 | let paragraph = Paragraph::new(format!("Window too small. Minimum size 45 x 12. Current size is {} x {}", size.width, size.height)).block(Block::default()).style(Style::default().fg(Color::Red)).wrap(Wrap{trim: true}); | ||
| 19 | frame.render_widget(paragraph, size); | ||
| 20 | return; | ||
| 21 | } | ||
| 22 | |||
| 23 | let hover_selected = if let Some((x, y)) = self.hover { | ||
| 24 | Some(self.selectable[y][x]) | ||
| 25 | } else { | ||
| 26 | None | ||
| 27 | }; | ||
| 28 | |||
| 16 | let vertical_chunks = Layout::default() | 29 | let vertical_chunks = Layout::default() |
| 17 | .direction(Direction::Vertical) | 30 | .direction(Direction::Vertical) |
| 18 | .constraints( | 31 | .constraints( |
| @@ -23,7 +36,12 @@ impl App { | |||
| 23 | ) | 36 | ) |
| 24 | .split(size); | 37 | .split(size); |
| 25 | 38 | ||
| 26 | for (row, row_chunk) in self.state.iter().zip(vertical_chunks.clone().into_iter()) { | 39 | for (y, (row, row_chunk)) in self |
| 40 | .state | ||
| 41 | .iter_mut() | ||
| 42 | .zip(vertical_chunks.clone().into_iter()) | ||
| 43 | .enumerate() | ||
| 44 | { | ||
| 27 | let mut constraints = LinkedList::new(); | 45 | let mut constraints = LinkedList::new(); |
| 28 | let mut length = match row.centered { | 46 | let mut length = match row.centered { |
| 29 | true => Some(0), | 47 | true => Some(0), |
| @@ -59,13 +77,18 @@ impl App { | |||
| 59 | 77 | ||
| 60 | frame.render_widget(Block::default(), chunks.next().unwrap()); | 78 | frame.render_widget(Block::default(), chunks.next().unwrap()); |
| 61 | 79 | ||
| 62 | for (chunk, item) in chunks.zip(row.items.iter().map(|i| &i.item)) { | 80 | |
| 81 | for (x, (chunk, item)) in chunks.zip(row.items.iter_mut().map(|i| &mut i.item)).enumerate() { | ||
| 82 | let selected = self.selected == Some((x, y)); | ||
| 83 | |||
| 84 | let hover = hover_selected == Some((x, y)); | ||
| 85 | |||
| 63 | match item { | 86 | match item { |
| 64 | Item::Global(i) => { | 87 | Item::Global(i) => { |
| 65 | i.render_item(frame, chunk); | 88 | i.render_item(frame, chunk, selected, hover, &self.message); |
| 66 | } | 89 | } |
| 67 | Item::MainMenu(i) => { | 90 | Item::MainMenu(i) => { |
| 68 | i.render_item(frame, chunk); | 91 | i.render_item(frame, chunk, selected, hover); |
| 69 | } | 92 | } |
| 70 | } | 93 | } |
| 71 | } | 94 | } |
src/traits/key_input.rs added +6
| @@ -0,0 +1,6 @@ | |||
| 1 | use crossterm::event::KeyCode; | ||
| 2 | use crate::app::app::App; | ||
| 3 | |||
| 4 | pub trait KeyInput { | ||
| 5 | fn key_input(&mut self, key: KeyCode, app: App) -> App ; | ||
| 6 | }\ No newline at end of file | ||
src/traits/load_item.rs added +7
| @@ -0,0 +1,7 @@ | |||
| 1 | use std::error::Error; | ||
| 2 | |||
| 3 | use crate::app::app::App; | ||
| 4 | |||
| 5 | pub trait LoadItem { | ||
| 6 | fn load_item(&self, app: &App) -> Result<Box<Self>, Box<dyn Error>>; | ||
| 7 | }\ No newline at end of file | ||
src/traits/mod.rs +10 −1
| @@ -1,2 +1,11 @@ | |||
| 1 | mod render_item; | 1 | mod render_item; |
| 2 | pub use render_item::*;\ No newline at end of file | 2 | pub use render_item::*; |
| 3 | |||
| 4 | mod select_item; | ||
| 5 | pub use select_item::*; | ||
| 6 | |||
| 7 | mod key_input; | ||
| 8 | pub use key_input::*; | ||
| 9 | |||
| 10 | mod load_item; | ||
| 11 | pub use load_item::*;\ No newline at end of file | ||
src/traits/render_item.rs +4 −2
| @@ -1,5 +1,7 @@ | |||
| 1 | use tui::{backend::Backend, layout::Rect, Frame}; | 1 | use tui::{backend::Backend, layout::Rect, Frame}; |
| 2 | 2 | ||
| 3 | use crate::app::app::App; | ||
| 4 | |||
| 3 | pub trait RenderItem { | 5 | pub trait RenderItem { |
| 4 | fn render_item<B: Backend>(&self, frame: &mut Frame<B>, rect: Rect); | 6 | fn render_item<B: Backend>(&mut self, frame: &mut Frame<B>, rect: Rect, selected: bool, hover: bool); |
| 5 | } | 7 | }\ No newline at end of file |
src/traits/select_item.rs added +6
| @@ -0,0 +1,6 @@ | |||
| 1 | use crate::app::app::App; | ||
| 2 | |||
| 3 | pub trait SelectItem { | ||
| 4 | fn select(&mut self, app: App) -> (App, bool); | ||
| 5 | fn selectable(&self) -> bool; | ||
| 6 | }\ No newline at end of file | ||
src/widgets/mod.rs +1
| @@ -0,0 +1 @@ | |||
| 1 | pub mod text_list;\ No newline at end of file | ||
src/widgets/text_list.rs added +149
| @@ -0,0 +1,149 @@ | |||
| 1 | use tui::{ | ||
| 2 | buffer::Buffer, | ||
| 3 | layout::Rect, | ||
| 4 | style::{Color, Style}, | ||
| 5 | symbols::line::{ | ||
| 6 | HORIZONTAL, ROUNDED_BOTTOM_LEFT, ROUNDED_BOTTOM_RIGHT, ROUNDED_TOP_LEFT, ROUNDED_TOP_RIGHT, | ||
| 7 | VERTICAL, | ||
| 8 | }, | ||
| 9 | widgets::Widget, | ||
| 10 | }; | ||
| 11 | |||
| 12 | #[derive(Debug, Clone)] | ||
| 13 | pub struct TextList { | ||
| 14 | pub items: Vec<String>, | ||
| 15 | pub selected: usize, | ||
| 16 | pub scroll: usize, | ||
| 17 | pub area: Option<Rect>, | ||
| 18 | } | ||
| 19 | |||
| 20 | impl Widget for TextList { | ||
| 21 | fn render(self, area: Rect, buf: &mut Buffer) { | ||
| 22 | if self.items.len() == 0 { | ||
| 23 | return; | ||
| 24 | } | ||
| 25 | |||
| 26 | let width_minus_2 = (area.width - 2) as usize; | ||
| 27 | let width_minus_5 = (area.width - 5) as usize; | ||
| 28 | let mut y = area.y; | ||
| 29 | |||
| 30 | for (i, mut item) in self | ||
| 31 | .items | ||
| 32 | .into_iter() | ||
| 33 | .enumerate() | ||
| 34 | .skip(self.scroll) | ||
| 35 | .take(area.height as usize - 2) | ||
| 36 | { | ||
| 37 | if item.len() > width_minus_2 { | ||
| 38 | item = format!("{}...", &item[0..width_minus_5]); | ||
| 39 | } | ||
| 40 | |||
| 41 | if i == self.selected { | ||
| 42 | let horizontal = HORIZONTAL.to_string().repeat(width_minus_2); | ||
| 43 | |||
| 44 | let lines = format!( | ||
| 45 | "{}{}{}\n{}{}{}{}\n{}{}{}", | ||
| 46 | ROUNDED_TOP_LEFT, | ||
| 47 | horizontal, | ||
| 48 | ROUNDED_TOP_RIGHT, | ||
| 49 | VERTICAL, | ||
| 50 | item, | ||
| 51 | " ".repeat(area.width as usize - 2 - item.len()), | ||
| 52 | VERTICAL, | ||
| 53 | ROUNDED_BOTTOM_LEFT, | ||
| 54 | horizontal, | ||
| 55 | ROUNDED_BOTTOM_RIGHT, | ||
| 56 | ); | ||
| 57 | |||
| 58 | let color = Color::LightYellow; | ||
| 59 | |||
| 60 | let mut rect = Rect { | ||
| 61 | x: area.x, | ||
| 62 | y: y, | ||
| 63 | width: area.width, | ||
| 64 | height: 1, | ||
| 65 | }; | ||
| 66 | buf.set_style(rect, Style::default().fg(color)); | ||
| 67 | |||
| 68 | rect = Rect { | ||
| 69 | x: area.x, | ||
| 70 | y: y + 1, | ||
| 71 | width: 1, | ||
| 72 | height: 1, | ||
| 73 | }; | ||
| 74 | buf.set_style(rect, Style::default().fg(color)); | ||
| 75 | |||
| 76 | rect = Rect { | ||
| 77 | x: area.x + area.width - 1, | ||
| 78 | y: y + 1, | ||
| 79 | width: 1, | ||
| 80 | height: 1, | ||
| 81 | }; | ||
| 82 | buf.set_style(rect, Style::default().fg(color)); | ||
| 83 | |||
| 84 | rect = Rect { | ||
| 85 | x: area.x, | ||
| 86 | y: y + 2, | ||
| 87 | width: area.width, | ||
| 88 | height: 1, | ||
| 89 | }; | ||
| 90 | buf.set_style(rect, Style::default().fg(color)); | ||
| 91 | |||
| 92 | for line in lines.lines() { | ||
| 93 | for (x, c) in (0..area.width).zip(line.chars()) { | ||
| 94 | buf.get_mut(area.x + x, y).set_char(c); | ||
| 95 | } | ||
| 96 | y += 1; | ||
| 97 | } | ||
| 98 | } else { | ||
| 99 | for (x, c) in (0..area.width).zip(item.chars()) { | ||
| 100 | buf.get_mut(area.x + x + 1, y).set_char(c); | ||
| 101 | } | ||
| 102 | y += 1; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | impl Default for TextList { | ||
| 109 | fn default() -> Self { | ||
| 110 | Self { | ||
| 111 | items: Vec::new(), | ||
| 112 | selected: 0, | ||
| 113 | scroll: 0, | ||
| 114 | area: None, | ||
| 115 | } | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | impl TextList { | ||
| 120 | pub fn area(&mut self, area: Rect) { | ||
| 121 | self.area = Some(area); | ||
| 122 | } | ||
| 123 | |||
| 124 | pub fn items(&mut self, items: Vec<String>) { | ||
| 125 | self.items = items; | ||
| 126 | } | ||
| 127 | |||
| 128 | pub fn down(&mut self) { | ||
| 129 | if self.selected < self.items.len() - 1 { | ||
| 130 | self.selected += 1; | ||
| 131 | |||
| 132 | if let Some(area) = self.area { | ||
| 133 | if self.selected > self.scroll + area.height as usize - 3 { | ||
| 134 | //panic!("Selected"); | ||
| 135 | self.scroll = self.selected + 3 - area.height as usize; | ||
| 136 | } | ||
| 137 | } | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | pub fn up(&mut self) { | ||
| 142 | if self.selected > 0 { | ||
| 143 | self.selected -= 1; | ||
| 144 | if self.selected < self.scroll { | ||
| 145 | self.scroll = self.selected; | ||
| 146 | } | ||
| 147 | } | ||
| 148 | } | ||
| 149 | } | ||