Commit 8b4931ead5
Unsigned
Layout: unified · split
Cargo.lock +11 −11
| @@ -70,9 +70,9 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610" | |||
| 70 | 70 | ||
| 71 | [[package]] | 71 | [[package]] |
| 72 | name = "bytes" | 72 | name = "bytes" |
| 73 | version = "1.2.1" | 73 | version = "1.3.0" |
| 74 | source = "registry+https://github.com/rust-lang/crates.io-index" | 74 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 75 | checksum = "ec8a7b6a70fde80372154c65702f00a0f56f3e1c36abbc6c440484be248856db" | 75 | checksum = "dfb24e866b15a1af2a1b663f10c6b6b8f397a84aadb828f12e5b289ec23a3a3c" |
| 76 | 76 | ||
| 77 | [[package]] | 77 | [[package]] |
| 78 | name = "cassowary" | 78 | name = "cassowary" |
| @@ -82,9 +82,9 @@ checksum = "df8670b8c7b9dae1793364eafadf7239c40d669904660c5960d74cfd80b46a53" | |||
| 82 | 82 | ||
| 83 | [[package]] | 83 | [[package]] |
| 84 | name = "cc" | 84 | name = "cc" |
| 85 | version = "1.0.76" | 85 | version = "1.0.77" |
| 86 | source = "registry+https://github.com/rust-lang/crates.io-index" | 86 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 87 | checksum = "76a284da2e6fe2092f2353e51713435363112dfd60030e22add80be333fb928f" | 87 | checksum = "e9f73505338f7d905b19d18738976aae232eb46b8efc15554ffc56deb5d9ebe4" |
| 88 | 88 | ||
| 89 | [[package]] | 89 | [[package]] |
| 90 | name = "cfg-if" | 90 | name = "cfg-if" |
| @@ -181,9 +181,9 @@ dependencies = [ | |||
| 181 | 181 | ||
| 182 | [[package]] | 182 | [[package]] |
| 183 | name = "crossbeam-epoch" | 183 | name = "crossbeam-epoch" |
| 184 | version = "0.9.11" | 184 | version = "0.9.12" |
| 185 | source = "registry+https://github.com/rust-lang/crates.io-index" | 185 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 186 | checksum = "f916dfc5d356b0ed9dae65f1db9fc9770aa2851d2662b988ccf4fe3516e86348" | 186 | checksum = "96bf8df95e795db1a4aca2957ad884a2df35413b24bbeb3114422f3cc21498e8" |
| 187 | dependencies = [ | 187 | dependencies = [ |
| 188 | "autocfg", | 188 | "autocfg", |
| 189 | "cfg-if", | 189 | "cfg-if", |
| @@ -194,9 +194,9 @@ dependencies = [ | |||
| 194 | 194 | ||
| 195 | [[package]] | 195 | [[package]] |
| 196 | name = "crossbeam-utils" | 196 | name = "crossbeam-utils" |
| 197 | version = "0.8.12" | 197 | version = "0.8.13" |
| 198 | source = "registry+https://github.com/rust-lang/crates.io-index" | 198 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 199 | checksum = "edbafec5fa1f196ca66527c1b12c2ec4745ca14b50f1ad8f9f6f720b55d11fac" | 199 | checksum = "422f23e724af1240ec469ea1e834d87a4b59ce2efe2c6a96256b0c47e2fd86aa" |
| 200 | dependencies = [ | 200 | dependencies = [ |
| 201 | "cfg-if", | 201 | "cfg-if", |
| 202 | ] | 202 | ] |
| @@ -811,9 +811,9 @@ checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" | |||
| 811 | 811 | ||
| 812 | [[package]] | 812 | [[package]] |
| 813 | name = "memoffset" | 813 | name = "memoffset" |
| 814 | version = "0.6.5" | 814 | version = "0.7.1" |
| 815 | source = "registry+https://github.com/rust-lang/crates.io-index" | 815 | source = "registry+https://github.com/rust-lang/crates.io-index" |
| 816 | checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce" | 816 | checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4" |
| 817 | dependencies = [ | 817 | dependencies = [ |
| 818 | "autocfg", | 818 | "autocfg", |
| 819 | ] | 819 | ] |
| @@ -1879,7 +1879,7 @@ dependencies = [ | |||
| 1879 | 1879 | ||
| 1880 | [[package]] | 1880 | [[package]] |
| 1881 | name = "youtube-tui" | 1881 | name = "youtube-tui" |
| 1882 | version = "0.5.1" | 1882 | version = "0.5.2" |
| 1883 | dependencies = [ | 1883 | dependencies = [ |
| 1884 | "chrono", | 1884 | "chrono", |
| 1885 | "crossterm", | 1885 | "crossterm", |
Cargo.toml +1 −1
| @@ -1,6 +1,6 @@ | |||
| 1 | [package] | 1 | [package] |
| 2 | name = "youtube-tui" | 2 | name = "youtube-tui" |
| 3 | version = "0.5.1" | 3 | version = "0.5.2" |
| 4 | authors = ["Siriusmart <siriusmart@tuta.io>"] | 4 | authors = ["Siriusmart <siriusmart@tuta.io>"] |
| 5 | edition = "2021" | 5 | edition = "2021" |
| 6 | description = "An aesthetically pleasing YouTube TUI written in Rust" | 6 | description = "An aesthetically pleasing YouTube TUI written in Rust" |
README.md +5 −3
| @@ -4,13 +4,13 @@ | |||
| 4 | [](https://crates.io/crates/youtube-tui) | 4 | [](https://crates.io/crates/youtube-tui) |
| 5 |  | 5 |  |
| 6 | 6 | ||
| 7 | *Read more in the [user manual](https://siriusmart.github.io/youtube-tui) (including installation gude and documentation).* | 7 | ***Read more in the [user manual](https://siriusmart.github.io/youtube-tui) (including installation gude and documentation).*** |
| 8 | 8 | ||
| 9 | # Overview | 9 | # Overview |
| 10 | 10 | ||
| 11 | Written in Rust, the **YouTube TUI** is a lightweight and user friendly TUI for browsing YouTube content from the terminal. | 11 | Written in Rust, the **YouTube TUI** is a lightweight and user friendly TUI for browsing YouTube content from the terminal. |
| 12 | 12 | ||
| 13 |  | 13 |  |
| 14 | 14 | ||
| 15 | It is like an *app launcher*, it launches other programs to do the heavy lifting (for example, `mpv` for playing videos). | 15 | It is like an *app launcher*, it launches other programs to do the heavy lifting (for example, `mpv` for playing videos). |
| 16 | 16 | ||
| @@ -43,7 +43,7 @@ The YouTube TUI does not work on its own, it is instead like a *TUI frontend* fo | |||
| 43 | 43 | ||
| 44 | However, the programs to launch can be changed, and therefore the YouTube TUI <u>does not rely on any specific dependencies</u> to run. | 44 | However, the programs to launch can be changed, and therefore the YouTube TUI <u>does not rely on any specific dependencies</u> to run. |
| 45 | 45 | ||
| 46 |  | 46 |  |
| 47 | 47 | ||
| 48 | ## Powerful | 48 | ## Powerful |
| 49 | 49 | ||
| @@ -57,6 +57,8 @@ The YouTube TUI allows you to browse YouTube with (almost) all of it's features, | |||
| 57 | ### What it doesn't have | 57 | ### What it doesn't have |
| 58 | 58 | ||
| 59 | - Vim-like commands (first priority) | 59 | - Vim-like commands (first priority) |
| 60 | - Command line args (first priority) | ||
| 61 | - Bootleg Vimscript? (will implement) | ||
| 60 | - Channel search (will implement) | 62 | - Channel search (will implement) |
| 61 | - Channel videos sorting (will implement) | 63 | - Channel videos sorting (will implement) |
| 62 | - Subscriptions (will implement) | 64 | - Subscriptions (will implement) |
docs/src/README.md +3 −3
| @@ -48,9 +48,9 @@ The YouTube TUI allows you to browse YouTube with (almost) all of it's features, | |||
| 48 | 48 | ||
| 49 | ### What it doesn't have | 49 | ### What it doesn't have |
| 50 | 50 | ||
| 51 | - Next page button in pages (first priority) | 51 | - Vim-like commands (first priority) |
| 52 | - Lazy image loading (first priority) | 52 | - Command line args (first priority) |
| 53 | - Vim-like commands (will implement) | 53 | - Bootleg Vimscript? (will implement) |
| 54 | - Channel search (will implement) | 54 | - Channel search (will implement) |
| 55 | - Channel videos sorting (will implement) | 55 | - Channel videos sorting (will implement) |
| 56 | - Subscriptions (will implement) | 56 | - Subscriptions (will implement) |
docs/src/changelogs.md +14 −1
| @@ -1,6 +1,17 @@ | |||
| 1 | # Changelogs | 1 | # Changelogs |
| 2 | 2 | ||
| 3 | ## v0.5.1 | 3 | ## v0.5.2 |
| 4 | |||
| 5 | #### Added | ||
| 6 | |||
| 7 | - Improved config files (including hex colour support) | ||
| 8 | - Page turners (next/prev page in search) | ||
| 9 | |||
| 10 | ### Fixed | ||
| 11 | |||
| 12 | - Where the image and text are displaying in the same place, and the image is displayed over the text | ||
| 13 | |||
| 14 | ### v0.5.1 | ||
| 4 | 15 | ||
| 5 | #### Added | 16 | #### Added |
| 6 | 17 | ||
| @@ -15,6 +26,8 @@ | |||
| 15 | - Sixels image support | 26 | - Sixels image support |
| 16 | - Improved config files | 27 | - Improved config files |
| 17 | 28 | ||
| 29 | <hr> | ||
| 30 | |||
| 18 | ## v0.4.4 | 31 | ## v0.4.4 |
| 19 | 32 | ||
| 20 | #### Added | 33 | #### Added |
docs/src/config/appearance.md +10 −24
| @@ -15,10 +15,7 @@ colors: | |||
| 15 | outline_selected: LightBlue | 15 | outline_selected: LightBlue |
| 16 | outline_hover: LightRed | 16 | outline_hover: LightRed |
| 17 | outline_secondary: LightYellow | 17 | outline_secondary: LightYellow |
| 18 | message_outline: !Rgb # todo: represent colours with hex values or `rgb(r,g,b)` | 18 | message_outline: '#FF7F00' |
| 19 | - 255 | ||
| 20 | - 127 | ||
| 21 | - 0 | ||
| 22 | message_error_outline: LightRed | 19 | message_error_outline: LightRed |
| 23 | message_success_outline: LightGreen | 20 | message_success_outline: LightGreen |
| 24 | item_info: | 21 | item_info: |
| @@ -29,22 +26,11 @@ colors: | |||
| 29 | viewcount: LightYellow | 26 | viewcount: LightYellow |
| 30 | length: LightCyan | 27 | length: LightCyan |
| 31 | published: LightMagenta | 28 | published: LightMagenta |
| 32 | video_count: !Rgb | 29 | video_count: '#838DFF' |
| 33 | - 131 | 30 | sub_count: '#65FFBA' |
| 34 | - 141 | 31 | likes: '#C8FF81' |
| 35 | - 255 | 32 | genre: '#FF75D7' |
| 36 | sub_count: !Rgb | 33 | page_turner: Gray |
| 37 | - 101 | ||
| 38 | - 255 | ||
| 39 | - 186 | ||
| 40 | likes: !Rgb | ||
| 41 | - 200 | ||
| 42 | - 255 | ||
| 43 | - 129 | ||
| 44 | genre: !Rgb | ||
| 45 | - 255 | ||
| 46 | - 121 | ||
| 47 | - 215 | ||
| 48 | ``` | 34 | ``` |
| 49 | 35 | ||
| 50 | <hr> | 36 | <hr> |
| @@ -63,10 +49,10 @@ Any colours, here are the 2 main represenations of colours, for more check out t | |||
| 63 | 49 | ||
| 64 | #### Terminal colour | 50 | #### Terminal colour |
| 65 | 51 | ||
| 66 | This can be modified by the themes of your terminal. | 52 | This can be modified by the themes of your terminal (e.g. white, green, etc). |
| 67 | 53 | ||
| 68 | #### RGB | 54 | #### Hex |
| 69 | 55 | ||
| 70 | RGB colour values, requires TrueColour support in terminal (for instance Windows CMD cannot display RBG colours). | 56 | Hex should be a string that starts with the `#` character, and can be from `000000` (black) to `FFFFFF` (white). |
| 71 | 57 | ||
| 72 | > Hex colour values to be implemented. | 58 | > RGB color values has been deprecated |
docs/src/config/keybindings.md +30 −8
| @@ -5,26 +5,48 @@ The keybindings config binds each key to an action, or multiple keys for the sam | |||
| 5 | ## Example keybindings config | 5 | ## Example keybindings config |
| 6 | 6 | ||
| 7 | ```yaml | 7 | ```yaml |
| 8 | 'q': | ||
| 9 | 0: Exit | ||
| 10 | Down: | ||
| 11 | 0: MoveDown | ||
| 12 | 'r': | ||
| 13 | 2: Reload | ||
| 14 | Enter: | ||
| 15 | 0: Select | ||
| 16 | 'l': | ||
| 17 | 0: MoveRight | ||
| 8 | Up: | 18 | Up: |
| 9 | 0: MoveUp | 19 | 0: MoveUp |
| 10 | Down: | 20 | 'j': |
| 11 | 0: MoveDown | 21 | 0: MoveDown |
| 22 | End: | ||
| 23 | 0: ClearHistory | ||
| 12 | Right: | 24 | Right: |
| 13 | 0: MoveRight | 25 | 0: MoveRight |
| 14 | Left: | 26 | Backspace: |
| 27 | 0: Back | ||
| 28 | 'h': | ||
| 15 | 0: MoveLeft | 29 | 0: MoveLeft |
| 16 | 4: Back | 30 | F5: |
| 31 | 0: Reload | ||
| 32 | 'k': | ||
| 33 | 0: MoveUp | ||
| 17 | Esc: | 34 | Esc: |
| 18 | 0: Deselect | 35 | 0: Deselect |
| 19 | Backspace: | 36 | Home: |
| 20 | 0: Back | 37 | 0: FirstHistory |
| 21 | Enter: | 38 | Left: |
| 22 | 0: Select | 39 | 0: MoveLeft |
| 40 | 4: Back | ||
| 23 | ``` | 41 | ``` |
| 24 | 42 | ||
| 25 | ## Keys | 43 | ## Keys |
| 26 | 44 | ||
| 27 | Keys are the name of the keys that are pressed in the keyboard, all keys are avaliable <a href="https://docs.rs/crossterm/latest/crossterm/event/enum.KeyCode.html" target=_blank>*here*</a>. | 45 | Keys can be: |
| 46 | |||
| 47 | - A single character (e.g. `'q'`) | ||
| 48 | - Named keys (e.g. `Up`, `Down`) | ||
| 49 | - Function keys (e.g. `F5`) | ||
| 28 | 50 | ||
| 29 | ## Key modifiers | 51 | ## Key modifiers |
| 30 | 52 | ||
docs/src/config/pages.md +8 −4
| @@ -7,16 +7,20 @@ The layout config determins the position of where each item is placed, mainly th | |||
| 7 | ```yaml | 7 | ```yaml |
| 8 | main_menu: | 8 | main_menu: |
| 9 | layout: | 9 | layout: |
| 10 | - !NonCenteredRow | 10 | - type: NonCenteredRow |
| 11 | items: | ||
| 11 | - SearchBar | 12 | - SearchBar |
| 12 | - SearchFilters | 13 | - SearchFilters |
| 13 | - !CenteredRow | 14 | - type: CenteredRow |
| 15 | items: | ||
| 14 | - Popular | 16 | - Popular |
| 15 | - Trending | 17 | - Trending |
| 16 | - History | 18 | - History |
| 17 | - !NonCenteredRow | 19 | - type: NonCenteredRow |
| 20 | items: | ||
| 18 | - ItemList | 21 | - ItemList |
| 19 | - !NonCenteredRow | 22 | - type: NonCenteredRow |
| 23 | items: | ||
| 20 | - MessageBar | 24 | - MessageBar |
| 21 | message: Loading main menu... | 25 | message: Loading main menu... |
| 22 | 26 | ||
src/config/appearance.rs +217 −80
| @@ -1,5 +1,12 @@ | |||
| 1 | use super::serde::BorderTypeSerde; | 1 | use std::{ |
| 2 | use crate::global::traits::ConfigTrait; | 2 | error::Error, |
| 3 | fs::{self, OpenOptions}, | ||
| 4 | io::Write, | ||
| 5 | }; | ||
| 6 | |||
| 7 | use super::serde::{BorderTypeSerde, ColorSerde, ColorVariantSerde}; | ||
| 8 | use crate::global::traits::{ConfigTrait, EXTENSION}; | ||
| 9 | use home::home_dir; | ||
| 3 | use serde::{Deserialize, Serialize}; | 10 | use serde::{Deserialize, Serialize}; |
| 4 | use tui::{style::Color, widgets::BorderType}; | 11 | use tui::{style::Color, widgets::BorderType}; |
| 5 | use typemap::Key; | 12 | use typemap::Key; |
| @@ -10,7 +17,7 @@ pub struct AppearanceConfigSerde { | |||
| 10 | #[serde(default)] | 17 | #[serde(default)] |
| 11 | pub borders: BorderTypeSerde, | 18 | pub borders: BorderTypeSerde, |
| 12 | #[serde(default)] | 19 | #[serde(default)] |
| 13 | pub colors: ColorsConfig, | 20 | pub colors: ColorsConfigSerde, |
| 14 | } | 21 | } |
| 15 | 22 | ||
| 16 | /// `appearance.yml`, this struct is stored in `data.global` | 23 | /// `appearance.yml`, this struct is stored in `data.global` |
| @@ -20,63 +27,188 @@ pub struct AppearanceConfig { | |||
| 20 | pub colors: ColorsConfig, | 27 | pub colors: ColorsConfig, |
| 21 | } | 28 | } |
| 22 | 29 | ||
| 30 | impl AppearanceConfig { | ||
| 31 | /// generates a new file if the original one is invalid | ||
| 32 | pub fn load() -> Result<Self, Box<dyn Error>> { | ||
| 33 | let serde = *AppearanceConfigSerde::load()?; | ||
| 34 | let try_into = Self::from(serde); | ||
| 35 | |||
| 36 | // check if it be converted from `AppearanceConfigSerde` to `AppearanceConfig` | ||
| 37 | if let Some(appearance) = try_into { | ||
| 38 | return Ok(appearance); | ||
| 39 | } | ||
| 40 | |||
| 41 | let config_path = home_dir().unwrap().join(format!( | ||
| 42 | ".config/youtube-tui/{}.{}", | ||
| 43 | AppearanceConfigSerde::LABEL, | ||
| 44 | EXTENSION | ||
| 45 | )); | ||
| 46 | |||
| 47 | // if it cannot, back it up and regenerate it | ||
| 48 | let mut new_path = config_path.clone(); | ||
| 49 | new_path.pop(); | ||
| 50 | new_path.push(format!( | ||
| 51 | "{}-{}.{}", | ||
| 52 | AppearanceConfigSerde::LABEL, | ||
| 53 | chrono::offset::Local::now(), | ||
| 54 | EXTENSION | ||
| 55 | )); | ||
| 56 | fs::rename(&config_path, &new_path)?; | ||
| 57 | |||
| 58 | // here generates a default and write it to the file | ||
| 59 | let default = AppearanceConfigSerde::default(); | ||
| 60 | |||
| 61 | let mut file = OpenOptions::new() | ||
| 62 | .write(true) | ||
| 63 | .create(true) | ||
| 64 | .truncate(true) | ||
| 65 | .open(&config_path)?; | ||
| 66 | |||
| 67 | file.write_all(serde_yaml::to_string(&default)?.as_bytes())?; | ||
| 68 | |||
| 69 | match Self::from(default) { | ||
| 70 | Some(appearance) => Ok(appearance), | ||
| 71 | // if the default still cannot be converted, panic and exit the program | ||
| 72 | None => panic!( | ||
| 73 | "the default for {}.{} is invalid", | ||
| 74 | AppearanceConfigSerde::LABEL, | ||
| 75 | EXTENSION | ||
| 76 | ), | ||
| 77 | } | ||
| 78 | } | ||
| 79 | } | ||
| 80 | |||
| 23 | /// Includes all configurable colors | 81 | /// Includes all configurable colors |
| 24 | #[derive(Clone, Copy, Serialize, Deserialize)] | 82 | #[derive(Clone, Copy)] |
| 25 | pub struct ColorsConfig { | 83 | pub struct ColorsConfig { |
| 26 | #[serde(default = "text_default")] | ||
| 27 | pub text: Color, | 84 | pub text: Color, |
| 28 | #[serde(default = "text_special_default")] | ||
| 29 | pub text_special: Color, | 85 | pub text_special: Color, |
| 30 | #[serde(default = "text_secondary_default")] | ||
| 31 | pub text_secondary: Color, | 86 | pub text_secondary: Color, |
| 32 | #[serde(default = "text_error_default")] | ||
| 33 | pub text_error: Color, | 87 | pub text_error: Color, |
| 34 | #[serde(default = "outline_default")] | ||
| 35 | pub outline: Color, | 88 | pub outline: Color, |
| 36 | #[serde(default = "outline_selected_default")] | ||
| 37 | pub outline_selected: Color, | 89 | pub outline_selected: Color, |
| 38 | #[serde(default = "outline_hover_default")] | ||
| 39 | pub outline_hover: Color, | 90 | pub outline_hover: Color, |
| 40 | #[serde(default = "outline_secondary_default")] | ||
| 41 | pub outline_secondary: Color, | 91 | pub outline_secondary: Color, |
| 42 | #[serde(default = "message_outline_default")] | ||
| 43 | pub message_outline: Color, | 92 | pub message_outline: Color, |
| 44 | #[serde(default = "message_error_outline_default")] | ||
| 45 | pub message_error_outline: Color, | 93 | pub message_error_outline: Color, |
| 46 | #[serde(default = "message_success_outline_default")] | ||
| 47 | pub message_success_outline: Color, | 94 | pub message_success_outline: Color, |
| 48 | #[serde(default)] | ||
| 49 | pub item_info: ItemInfoColors, | 95 | pub item_info: ItemInfoColors, |
| 50 | } | 96 | } |
| 51 | 97 | ||
| 52 | /// Colors used by ItemInfo | 98 | /// Colors used by ItemInfo |
| 53 | #[derive(Clone, Copy, Serialize, Deserialize)] | 99 | #[derive(Clone, Copy)] |
| 54 | pub struct ItemInfoColors { | 100 | pub struct ItemInfoColors { |
| 55 | #[serde(default = "tag_default")] | ||
| 56 | pub tag: Color, | 101 | pub tag: Color, |
| 57 | #[serde(default = "title_default")] | ||
| 58 | pub title: Color, | 102 | pub title: Color, |
| 59 | #[serde(default = "description_default")] | ||
| 60 | pub description: Color, | 103 | pub description: Color, |
| 61 | #[serde(default = "author_default")] | ||
| 62 | pub author: Color, | 104 | pub author: Color, |
| 63 | #[serde(default = "viewcount_default")] | ||
| 64 | pub viewcount: Color, | 105 | pub viewcount: Color, |
| 65 | #[serde(default = "length_default")] | ||
| 66 | pub length: Color, | 106 | pub length: Color, |
| 67 | #[serde(default = "published_default")] | ||
| 68 | pub published: Color, | 107 | pub published: Color, |
| 69 | #[serde(default = "video_count_default")] | ||
| 70 | pub video_count: Color, | 108 | pub video_count: Color, |
| 71 | #[serde(default = "sub_count_default")] | ||
| 72 | pub sub_count: Color, | 109 | pub sub_count: Color, |
| 73 | #[serde(default = "likes_default")] | ||
| 74 | pub likes: Color, | 110 | pub likes: Color, |
| 75 | #[serde(default = "genre_default")] | ||
| 76 | pub genre: Color, | 111 | pub genre: Color, |
| 112 | pub page_turner: Color, | ||
| 113 | } | ||
| 114 | |||
| 115 | #[derive(Clone, Serialize, Deserialize)] | ||
| 116 | pub struct ColorsConfigSerde { | ||
| 117 | #[serde(default = "text_default")] | ||
| 118 | pub text: ColorSerde, | ||
| 119 | #[serde(default = "text_special_default")] | ||
| 120 | pub text_special: ColorSerde, | ||
| 121 | #[serde(default = "text_secondary_default")] | ||
| 122 | pub text_secondary: ColorSerde, | ||
| 123 | #[serde(default = "text_error_default")] | ||
| 124 | pub text_error: ColorSerde, | ||
| 125 | #[serde(default = "outline_default")] | ||
| 126 | pub outline: ColorSerde, | ||
| 127 | #[serde(default = "outline_selected_default")] | ||
| 128 | pub outline_selected: ColorSerde, | ||
| 129 | #[serde(default = "outline_hover_default")] | ||
| 130 | pub outline_hover: ColorSerde, | ||
| 131 | #[serde(default = "outline_secondary_default")] | ||
| 132 | pub outline_secondary: ColorSerde, | ||
| 133 | #[serde(default = "message_outline_default")] | ||
| 134 | pub message_outline: ColorSerde, | ||
| 135 | #[serde(default = "message_error_outline_default")] | ||
| 136 | pub message_error_outline: ColorSerde, | ||
| 137 | #[serde(default = "message_success_outline_default")] | ||
| 138 | pub message_success_outline: ColorSerde, | ||
| 139 | #[serde(default)] | ||
| 140 | pub item_info: ItemInfoColorsSerde, | ||
| 141 | } | ||
| 142 | |||
| 143 | #[derive(Clone, Serialize, Deserialize)] | ||
| 144 | pub struct ItemInfoColorsSerde { | ||
| 145 | #[serde(default = "tag_default")] | ||
| 146 | pub tag: ColorSerde, | ||
| 147 | #[serde(default = "title_default")] | ||
| 148 | pub title: ColorSerde, | ||
| 149 | #[serde(default = "description_default")] | ||
| 150 | pub description: ColorSerde, | ||
| 151 | #[serde(default = "author_default")] | ||
| 152 | pub author: ColorSerde, | ||
| 153 | #[serde(default = "viewcount_default")] | ||
| 154 | pub viewcount: ColorSerde, | ||
| 155 | #[serde(default = "length_default")] | ||
| 156 | pub length: ColorSerde, | ||
| 157 | #[serde(default = "published_default")] | ||
| 158 | pub published: ColorSerde, | ||
| 159 | #[serde(default = "video_count_default")] | ||
| 160 | pub video_count: ColorSerde, | ||
| 161 | #[serde(default = "sub_count_default")] | ||
| 162 | pub sub_count: ColorSerde, | ||
| 163 | #[serde(default = "likes_default")] | ||
| 164 | pub likes: ColorSerde, | ||
| 165 | #[serde(default = "genre_default")] | ||
| 166 | pub genre: ColorSerde, | ||
| 167 | #[serde(default = "page_turner_default")] | ||
| 168 | pub page_turner: ColorSerde, | ||
| 77 | } | 169 | } |
| 78 | 170 | ||
| 79 | impl Default for ColorsConfig { | 171 | // uses a custom `into` for Option<T> instead of T so that we can know that the config is invalid |
| 172 | // without panicking | ||
| 173 | impl ColorsConfigSerde { | ||
| 174 | pub fn into(self) -> Option<ColorsConfig> { | ||
| 175 | Some(ColorsConfig { | ||
| 176 | text: self.text.to_color()?, | ||
| 177 | text_special: self.text_special.to_color()?, | ||
| 178 | text_secondary: self.text_secondary.to_color()?, | ||
| 179 | text_error: self.text_error.to_color()?, | ||
| 180 | outline: self.outline.to_color()?, | ||
| 181 | outline_selected: self.outline_selected.to_color()?, | ||
| 182 | outline_hover: self.outline_hover.to_color()?, | ||
| 183 | outline_secondary: self.outline_secondary.to_color()?, | ||
| 184 | message_outline: self.message_outline.to_color()?, | ||
| 185 | message_error_outline: self.message_error_outline.to_color()?, | ||
| 186 | message_success_outline: self.message_success_outline.to_color()?, | ||
| 187 | item_info: self.item_info.into()?, | ||
| 188 | }) | ||
| 189 | } | ||
| 190 | } | ||
| 191 | |||
| 192 | impl ItemInfoColorsSerde { | ||
| 193 | pub fn into(self) -> Option<ItemInfoColors> { | ||
| 194 | Some(ItemInfoColors { | ||
| 195 | tag: self.tag.to_color()?, | ||
| 196 | title: self.title.to_color()?, | ||
| 197 | description: self.description.to_color()?, | ||
| 198 | author: self.author.to_color()?, | ||
| 199 | viewcount: self.viewcount.to_color()?, | ||
| 200 | length: self.length.to_color()?, | ||
| 201 | published: self.published.to_color()?, | ||
| 202 | video_count: self.video_count.to_color()?, | ||
| 203 | sub_count: self.sub_count.to_color()?, | ||
| 204 | likes: self.likes.to_color()?, | ||
| 205 | genre: self.genre.to_color()?, | ||
| 206 | page_turner: self.page_turner.to_color()?, | ||
| 207 | }) | ||
| 208 | } | ||
| 209 | } | ||
| 210 | |||
| 211 | impl Default for ColorsConfigSerde { | ||
| 80 | fn default() -> Self { | 212 | fn default() -> Self { |
| 81 | Self { | 213 | Self { |
| 82 | text: text_default(), | 214 | text: text_default(), |
| @@ -90,12 +222,12 @@ impl Default for ColorsConfig { | |||
| 90 | message_error_outline: message_error_outline_default(), | 222 | message_error_outline: message_error_outline_default(), |
| 91 | message_success_outline: message_success_outline_default(), | 223 | message_success_outline: message_success_outline_default(), |
| 92 | text_error: text_error_default(), | 224 | text_error: text_error_default(), |
| 93 | item_info: ItemInfoColors::default(), | 225 | item_info: ItemInfoColorsSerde::default(), |
| 94 | } | 226 | } |
| 95 | } | 227 | } |
| 96 | } | 228 | } |
| 97 | 229 | ||
| 98 | impl Default for ItemInfoColors { | 230 | impl Default for ItemInfoColorsSerde { |
| 99 | fn default() -> Self { | 231 | fn default() -> Self { |
| 100 | Self { | 232 | Self { |
| 101 | tag: tag_default(), | 233 | tag: tag_default(), |
| @@ -109,16 +241,17 @@ impl Default for ItemInfoColors { | |||
| 109 | sub_count: sub_count_default(), | 241 | sub_count: sub_count_default(), |
| 110 | likes: likes_default(), | 242 | likes: likes_default(), |
| 111 | genre: genre_default(), | 243 | genre: genre_default(), |
| 244 | page_turner: page_turner_default(), | ||
| 112 | } | 245 | } |
| 113 | } | 246 | } |
| 114 | } | 247 | } |
| 115 | 248 | ||
| 116 | impl From<AppearanceConfigSerde> for AppearanceConfig { | 249 | impl AppearanceConfig { |
| 117 | fn from(original: AppearanceConfigSerde) -> Self { | 250 | fn from(original: AppearanceConfigSerde) -> Option<Self> { |
| 118 | Self { | 251 | Some(Self { |
| 119 | borders: original.borders.into(), | 252 | borders: original.borders.into(), |
| 120 | colors: original.colors, | 253 | colors: original.colors.into()?, |
| 121 | } | 254 | }) |
| 122 | } | 255 | } |
| 123 | } | 256 | } |
| 124 | 257 | ||
| @@ -132,90 +265,94 @@ impl Key for AppearanceConfig { | |||
| 132 | 265 | ||
| 133 | // defaults | 266 | // defaults |
| 134 | 267 | ||
| 135 | fn text_default() -> Color { | 268 | fn text_default() -> ColorSerde { |
| 136 | Color::Reset | 269 | ColorSerde::ColorVariant(ColorVariantSerde::Reset) |
| 270 | } | ||
| 271 | |||
| 272 | fn text_special_default() -> ColorSerde { | ||
| 273 | ColorSerde::ColorVariant(ColorVariantSerde::Reset) | ||
| 137 | } | 274 | } |
| 138 | 275 | ||
| 139 | fn text_special_default() -> Color { | 276 | fn text_secondary_default() -> ColorSerde { |
| 140 | Color::Reset | 277 | ColorSerde::ColorVariant(ColorVariantSerde::Reset) |
| 141 | } | 278 | } |
| 142 | 279 | ||
| 143 | fn text_secondary_default() -> Color { | 280 | fn outline_default() -> ColorSerde { |
| 144 | Color::Reset | 281 | ColorSerde::ColorVariant(ColorVariantSerde::Reset) |
| 145 | } | 282 | } |
| 146 | 283 | ||
| 147 | fn outline_default() -> Color { | 284 | fn outline_selected_default() -> ColorSerde { |
| 148 | Color::Reset | 285 | ColorSerde::ColorVariant(ColorVariantSerde::LightBlue) |
| 149 | } | 286 | } |
| 150 | 287 | ||
| 151 | fn outline_selected_default() -> Color { | 288 | fn outline_hover_default() -> ColorSerde { |
| 152 | Color::LightBlue | 289 | ColorSerde::ColorVariant(ColorVariantSerde::LightRed) |
| 153 | } | 290 | } |
| 154 | 291 | ||
| 155 | fn outline_hover_default() -> Color { | 292 | fn outline_secondary_default() -> ColorSerde { |
| 156 | Color::LightRed | 293 | ColorSerde::ColorVariant(ColorVariantSerde::LightYellow) |
| 157 | } | 294 | } |
| 158 | 295 | ||
| 159 | fn outline_secondary_default() -> Color { | 296 | fn message_outline_default() -> ColorSerde { |
| 160 | Color::LightYellow | 297 | ColorSerde::Hex(String::from("#FF7F00")) |
| 161 | } | 298 | } |
| 162 | 299 | ||
| 163 | fn message_outline_default() -> Color { | 300 | fn message_error_outline_default() -> ColorSerde { |
| 164 | Color::Rgb(255, 127, 0) | 301 | ColorSerde::ColorVariant(ColorVariantSerde::LightRed) |
| 165 | } | 302 | } |
| 166 | 303 | ||
| 167 | fn message_error_outline_default() -> Color { | 304 | fn message_success_outline_default() -> ColorSerde { |
| 168 | Color::LightRed | 305 | ColorSerde::ColorVariant(ColorVariantSerde::LightGreen) |
| 169 | } | 306 | } |
| 170 | 307 | ||
| 171 | fn message_success_outline_default() -> Color { | 308 | fn text_error_default() -> ColorSerde { |
| 172 | Color::LightGreen | 309 | ColorSerde::ColorVariant(ColorVariantSerde::LightRed) |
| 173 | } | 310 | } |
| 174 | 311 | ||
| 175 | fn text_error_default() -> Color { | 312 | fn tag_default() -> ColorSerde { |
| 176 | Color::LightRed | 313 | ColorSerde::ColorVariant(ColorVariantSerde::Gray) |
| 177 | } | 314 | } |
| 178 | 315 | ||
| 179 | fn tag_default() -> Color { | 316 | fn title_default() -> ColorSerde { |
| 180 | Color::Gray | 317 | ColorSerde::ColorVariant(ColorVariantSerde::LightBlue) |
| 181 | } | 318 | } |
| 182 | 319 | ||
| 183 | fn title_default() -> Color { | 320 | fn description_default() -> ColorSerde { |
| 184 | Color::LightBlue | 321 | ColorSerde::ColorVariant(ColorVariantSerde::Gray) |
| 185 | } | 322 | } |
| 186 | 323 | ||
| 187 | fn description_default() -> Color { | 324 | fn author_default() -> ColorSerde { |
| 188 | Color::Gray | 325 | ColorSerde::ColorVariant(ColorVariantSerde::LightGreen) |
| 189 | } | 326 | } |
| 190 | 327 | ||
| 191 | fn author_default() -> Color { | 328 | fn viewcount_default() -> ColorSerde { |
| 192 | Color::LightGreen | 329 | ColorSerde::ColorVariant(ColorVariantSerde::LightYellow) |
| 193 | } | 330 | } |
| 194 | 331 | ||
| 195 | fn viewcount_default() -> Color { | 332 | fn length_default() -> ColorSerde { |
| 196 | Color::LightYellow | 333 | ColorSerde::ColorVariant(ColorVariantSerde::LightCyan) |
| 197 | } | 334 | } |
| 198 | 335 | ||
| 199 | fn length_default() -> Color { | 336 | fn published_default() -> ColorSerde { |
| 200 | Color::LightCyan | 337 | ColorSerde::ColorVariant(ColorVariantSerde::LightMagenta) |
| 201 | } | 338 | } |
| 202 | 339 | ||
| 203 | fn published_default() -> Color { | 340 | fn video_count_default() -> ColorSerde { |
| 204 | Color::LightMagenta | 341 | ColorSerde::Hex(String::from("#838DFF")) |
| 205 | } | 342 | } |
| 206 | 343 | ||
| 207 | fn video_count_default() -> Color { | 344 | fn sub_count_default() -> ColorSerde { |
| 208 | Color::Rgb(131, 141, 255) | 345 | ColorSerde::Hex(String::from("#65FFBA")) |
| 209 | } | 346 | } |
| 210 | 347 | ||
| 211 | fn sub_count_default() -> Color { | 348 | fn likes_default() -> ColorSerde { |
| 212 | Color::Rgb(101, 255, 186) | 349 | ColorSerde::Hex(String::from("#C8FF81")) |
| 213 | } | 350 | } |
| 214 | 351 | ||
| 215 | fn likes_default() -> Color { | 352 | fn genre_default() -> ColorSerde { |
| 216 | Color::Rgb(200, 255, 129) | 353 | ColorSerde::Hex(String::from("#FF75D7")) |
| 217 | } | 354 | } |
| 218 | 355 | ||
| 219 | fn genre_default() -> Color { | 356 | fn page_turner_default() -> ColorSerde { |
| 220 | Color::Rgb(255, 121, 215) | 357 | ColorSerde::ColorVariant(ColorVariantSerde::Gray) |
| 221 | } | 358 | } |
src/config/keybindings.rs +138 −23
| @@ -1,7 +1,17 @@ | |||
| 1 | use crate::global::{structs::KeyAction, traits::ConfigTrait}; | 1 | use super::serde::{KeyCodeSerde, KeyVariantsSerde}; |
| 2 | use crate::global::{ | ||
| 3 | structs::KeyAction, | ||
| 4 | traits::{ConfigTrait, EXTENSION}, | ||
| 5 | }; | ||
| 2 | use crossterm::event::KeyCode; | 6 | use crossterm::event::KeyCode; |
| 7 | use home::home_dir; | ||
| 3 | use serde::{Deserialize, Serialize}; | 8 | use serde::{Deserialize, Serialize}; |
| 4 | use std::collections::HashMap; | 9 | use std::{ |
| 10 | collections::HashMap, | ||
| 11 | error::Error, | ||
| 12 | fs::{self, OpenOptions}, | ||
| 13 | io::Write, | ||
| 14 | }; | ||
| 5 | use typemap::Key; | 15 | use typemap::Key; |
| 6 | 16 | ||
| 7 | // The outer HashMap<KeyCode, T> gets a value for whatever code is being pressed | 17 | // The outer HashMap<KeyCode, T> gets a value for whatever code is being pressed |
| @@ -9,51 +19,156 @@ use typemap::Key; | |||
| 9 | // The u8 for each modifier key can be found in https://docs.rs/crossterm/latest/src/crossterm/event.rs.html#587-603 | 19 | // The u8 for each modifier key can be found in https://docs.rs/crossterm/latest/src/crossterm/event.rs.html#587-603 |
| 10 | // The sum of u8 for each key modifier would mean having all of those modifiers at the same time | 20 | // The sum of u8 for each key modifier would mean having all of those modifiers at the same time |
| 11 | /// `keybindings.yml`, contains a hashmap of key + key modifier to action pair | 21 | /// `keybindings.yml`, contains a hashmap of key + key modifier to action pair |
| 12 | #[derive(Serialize, Deserialize, Clone)] | 22 | #[derive(Clone)] |
| 13 | pub struct KeyBindingsConfig(pub HashMap<KeyCode, HashMap<u8, KeyAction>>); | 23 | pub struct KeyBindingsConfig(pub HashMap<KeyCode, HashMap<u8, KeyAction>>); |
| 14 | 24 | ||
| 15 | impl ConfigTrait for KeyBindingsConfig { | ||
| 16 | const LABEL: &'static str = "keybindings"; | ||
| 17 | } | ||
| 18 | |||
| 19 | impl Key for KeyBindingsConfig { | 25 | impl Key for KeyBindingsConfig { |
| 20 | type Value = Self; | 26 | type Value = Self; |
| 21 | } | 27 | } |
| 22 | 28 | ||
| 23 | impl Default for KeyBindingsConfig { | 29 | #[derive(Serialize, Deserialize, Clone)] |
| 30 | pub struct KeyBindingsConfigSerde(pub HashMap<KeyCodeSerde, HashMap<u8, KeyAction>>); | ||
| 31 | |||
| 32 | impl ConfigTrait for KeyBindingsConfigSerde { | ||
| 33 | const LABEL: &'static str = "keybindings"; | ||
| 34 | } | ||
| 35 | |||
| 36 | impl Default for KeyBindingsConfigSerde { | ||
| 24 | fn default() -> Self { | 37 | fn default() -> Self { |
| 25 | Self(HashMap::from([ | 38 | Self(HashMap::from([ |
| 26 | // movement keys // Alt left should be back | 39 | // movement keys // Alt left should be back |
| 27 | ( | 40 | ( |
| 28 | KeyCode::Left, | 41 | KeyCodeSerde::KeyVariants(KeyVariantsSerde::Left), |
| 29 | HashMap::from([(0, KeyAction::MoveLeft), (4, KeyAction::Back)]), | 42 | HashMap::from([(0, KeyAction::MoveLeft), (4, KeyAction::Back)]), |
| 30 | ), | 43 | ), |
| 31 | (KeyCode::Right, HashMap::from([(0, KeyAction::MoveRight)])), | 44 | ( |
| 32 | (KeyCode::Up, HashMap::from([(0, KeyAction::MoveUp)])), | 45 | KeyCodeSerde::KeyVariants(KeyVariantsSerde::Right), |
| 33 | (KeyCode::Down, HashMap::from([(0, KeyAction::MoveDown)])), | 46 | HashMap::from([(0, KeyAction::MoveRight)]), |
| 47 | ), | ||
| 48 | ( | ||
| 49 | KeyCodeSerde::KeyVariants(KeyVariantsSerde::Up), | ||
| 50 | HashMap::from([(0, KeyAction::MoveUp)]), | ||
| 51 | ), | ||
| 52 | ( | ||
| 53 | KeyCodeSerde::KeyVariants(KeyVariantsSerde::Down), | ||
| 54 | HashMap::from([(0, KeyAction::MoveDown)]), | ||
| 55 | ), | ||
| 34 | // vim keybindings | 56 | // vim keybindings |
| 35 | ( | 57 | ( |
| 36 | KeyCode::Char('h'), | 58 | KeyCodeSerde::Char('h'), |
| 37 | HashMap::from([(0, KeyAction::MoveLeft)]), | 59 | HashMap::from([(0, KeyAction::MoveLeft)]), |
| 38 | ), | 60 | ), |
| 39 | ( | 61 | ( |
| 40 | KeyCode::Char('j'), | 62 | KeyCodeSerde::Char('j'), |
| 41 | HashMap::from([(0, KeyAction::MoveDown)]), | 63 | HashMap::from([(0, KeyAction::MoveDown)]), |
| 42 | ), | 64 | ), |
| 43 | (KeyCode::Char('k'), HashMap::from([(0, KeyAction::MoveUp)])), | ||
| 44 | ( | 65 | ( |
| 45 | KeyCode::Char('l'), | 66 | KeyCodeSerde::Char('k'), |
| 67 | HashMap::from([(0, KeyAction::MoveUp)]), | ||
| 68 | ), | ||
| 69 | ( | ||
| 70 | KeyCodeSerde::Char('l'), | ||
| 46 | HashMap::from([(0, KeyAction::MoveRight)]), | 71 | HashMap::from([(0, KeyAction::MoveRight)]), |
| 47 | ), | 72 | ), |
| 48 | // functional keys | 73 | // functional keys |
| 49 | (KeyCode::Enter, HashMap::from([(0, KeyAction::Select)])), | 74 | ( |
| 50 | (KeyCode::Esc, HashMap::from([(0, KeyAction::Deselect)])), | 75 | KeyCodeSerde::KeyVariants(KeyVariantsSerde::Enter), |
| 51 | (KeyCode::Char('q'), HashMap::from([(0, KeyAction::Exit)])), | 76 | HashMap::from([(0, KeyAction::Select)]), |
| 52 | (KeyCode::Char('r'), HashMap::from([(2, KeyAction::Reload)])), | 77 | ), |
| 78 | ( | ||
| 79 | KeyCodeSerde::KeyVariants(KeyVariantsSerde::Esc), | ||
| 80 | HashMap::from([(0, KeyAction::Deselect)]), | ||
| 81 | ), | ||
| 82 | ( | ||
| 83 | KeyCodeSerde::F(String::from("F5")), | ||
| 84 | HashMap::from([(0, KeyAction::Reload)]), | ||
| 85 | ), | ||
| 86 | ( | ||
| 87 | KeyCodeSerde::Char('q'), | ||
| 88 | HashMap::from([(0, KeyAction::Exit)]), | ||
| 89 | ), | ||
| 90 | ( | ||
| 91 | KeyCodeSerde::Char('r'), | ||
| 92 | HashMap::from([(2, KeyAction::Reload)]), | ||
| 93 | ), | ||
| 53 | // history | 94 | // history |
| 54 | (KeyCode::Backspace, HashMap::from([(0, KeyAction::Back)])), | 95 | ( |
| 55 | (KeyCode::End, HashMap::from([(0, KeyAction::ClearHistory)])), | 96 | KeyCodeSerde::KeyVariants(KeyVariantsSerde::Backspace), |
| 56 | (KeyCode::Home, HashMap::from([(0, KeyAction::FirstHistory)])), | 97 | HashMap::from([(0, KeyAction::Back)]), |
| 98 | ), | ||
| 99 | ( | ||
| 100 | KeyCodeSerde::KeyVariants(KeyVariantsSerde::End), | ||
| 101 | HashMap::from([(0, KeyAction::ClearHistory)]), | ||
| 102 | ), | ||
| 103 | ( | ||
| 104 | KeyCodeSerde::KeyVariants(KeyVariantsSerde::Home), | ||
| 105 | HashMap::from([(0, KeyAction::FirstHistory)]), | ||
| 106 | ), | ||
| 57 | ])) | 107 | ])) |
| 58 | } | 108 | } |
| 59 | } | 109 | } |
| 110 | |||
| 111 | impl KeyBindingsConfigSerde { | ||
| 112 | /// converts KeyBindingsConfigSerde into KeyBindingsConfig | ||
| 113 | pub fn into(self) -> Option<KeyBindingsConfig> { | ||
| 114 | let mut out = HashMap::new(); | ||
| 115 | |||
| 116 | // simply loops over the serde hashmap and insert them into the new one | ||
| 117 | for (keycodeserde, map) in self.0.into_iter() { | ||
| 118 | out.insert(keycodeserde.to_keycode()?, map); | ||
| 119 | } | ||
| 120 | |||
| 121 | Some(KeyBindingsConfig(out)) | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | impl KeyBindingsConfig { | ||
| 126 | /// generates a new file if the original one is invalid | ||
| 127 | pub fn load() -> Result<Self, Box<dyn Error>> { | ||
| 128 | let serde = *KeyBindingsConfigSerde::load()?; | ||
| 129 | // check if it be converted from `KeyBindingsConfigSerde` to `KeyBindingsConfig` | ||
| 130 | let try_into = serde.into(); | ||
| 131 | |||
| 132 | if let Some(keybindings) = try_into { | ||
| 133 | return Ok(keybindings); | ||
| 134 | } | ||
| 135 | |||
| 136 | let config_path = home_dir().unwrap().join(format!( | ||
| 137 | ".config/youtube-tui/{}.{}", | ||
| 138 | KeyBindingsConfigSerde::LABEL, | ||
| 139 | EXTENSION | ||
| 140 | )); | ||
| 141 | |||
| 142 | // if it cannot, back it up and regenerate it | ||
| 143 | let mut new_path = config_path.clone(); | ||
| 144 | new_path.pop(); | ||
| 145 | new_path.push(format!( | ||
| 146 | "{}-{}.{}", | ||
| 147 | KeyBindingsConfigSerde::LABEL, | ||
| 148 | chrono::offset::Local::now(), | ||
| 149 | EXTENSION | ||
| 150 | )); | ||
| 151 | fs::rename(&config_path, &new_path)?; | ||
| 152 | |||
| 153 | // here generates a default and write it to the file | ||
| 154 | let default = KeyBindingsConfigSerde::default(); | ||
| 155 | |||
| 156 | let mut file = OpenOptions::new() | ||
| 157 | .write(true) | ||
| 158 | .create(true) | ||
| 159 | .truncate(true) | ||
| 160 | .open(&config_path)?; | ||
| 161 | |||
| 162 | file.write_all(serde_yaml::to_string(&default)?.as_bytes())?; | ||
| 163 | |||
| 164 | match default.into() { | ||
| 165 | Some(keybindings) => Ok(keybindings), | ||
| 166 | // if the default still cannot be converted, panic and exit the program | ||
| 167 | None => panic!( | ||
| 168 | "the default for {}.{} is invalid", | ||
| 169 | KeyBindingsConfigSerde::LABEL, | ||
| 170 | EXTENSION | ||
| 171 | ), | ||
| 172 | } | ||
| 173 | } | ||
| 174 | } | ||
src/config/pages.rs +1
| @@ -122,6 +122,7 @@ impl PageConfig { | |||
| 122 | 122 | ||
| 123 | /// CenteredRow will have its items centered, while NonCenteredRow will align to the left | 123 | /// CenteredRow will have its items centered, while NonCenteredRow will align to the left |
| 124 | #[derive(Clone, Serialize, Deserialize)] | 124 | #[derive(Clone, Serialize, Deserialize)] |
| 125 | #[serde(tag = "type", content = "items")] | ||
| 125 | pub enum PageRow { | 126 | pub enum PageRow { |
| 126 | CenteredRow(Vec<PageItems>), | 127 | CenteredRow(Vec<PageItems>), |
| 127 | NonCenteredRow(Vec<PageItems>), | 128 | NonCenteredRow(Vec<PageItems>), |
src/config/search.rs +24 −9
| @@ -7,10 +7,21 @@ use typemap::Key; | |||
| 7 | 7 | ||
| 8 | // can be turned into URL Params for the search term with filters | 8 | // can be turned into URL Params for the search term with filters |
| 9 | /// Search query & filters | 9 | /// Search query & filters |
| 10 | #[derive(Clone, Default, Debug, Serialize, Deserialize, PartialEq, Eq)] | 10 | #[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] |
| 11 | pub struct Search { | 11 | pub struct Search { |
| 12 | pub query: String, | 12 | pub query: String, |
| 13 | pub filters: SearchFilters, | 13 | pub filters: SearchFilters, |
| 14 | pub page: u16, | ||
| 15 | } | ||
| 16 | |||
| 17 | impl Default for Search { | ||
| 18 | fn default() -> Self { | ||
| 19 | Self { | ||
| 20 | query: String::new(), | ||
| 21 | filters: SearchFilters::default(), | ||
| 22 | page: 1, | ||
| 23 | } | ||
| 24 | } | ||
| 14 | } | 25 | } |
| 15 | 26 | ||
| 16 | impl Key for Search { | 27 | impl Key for Search { |
| @@ -23,14 +34,18 @@ impl ConfigTrait for Search { | |||
| 23 | 34 | ||
| 24 | impl ToString for Search { | 35 | impl ToString for Search { |
| 25 | fn to_string(&self) -> String { | 36 | fn to_string(&self) -> String { |
| 26 | vec![ | 37 | format!( |
| 27 | format!("q={}", self.query), | 38 | "{}&page={}", |
| 28 | self.filters.sort.as_url_string(), | 39 | vec![ |
| 29 | self.filters.date.as_url_string(), | 40 | format!("q={}", self.query), |
| 30 | self.filters.duration.as_url_string(), | 41 | self.filters.sort.as_url_string(), |
| 31 | self.filters.r#type.as_url_string(), | 42 | self.filters.date.as_url_string(), |
| 32 | ] | 43 | self.filters.duration.as_url_string(), |
| 33 | .join("&") | 44 | self.filters.r#type.as_url_string(), |
| 45 | ] | ||
| 46 | .join("&"), | ||
| 47 | self.page | ||
| 48 | ) | ||
| 34 | } | 49 | } |
| 35 | } | 50 | } |
| 36 | 51 | ||
src/config/serde.rs +175 −2
| @@ -1,7 +1,7 @@ | |||
| 1 | //! Structs that impl `Into<T>` because `T` does not impl Serde but is used in config files | 1 | //! Structs that impl `Into<T>` because `T` does not impl Serde but is used in config files |
| 2 | 2 | use crossterm::event::KeyCode; | |
| 3 | use serde::{Deserialize, Serialize}; | 3 | use serde::{Deserialize, Serialize}; |
| 4 | use tui::widgets::BorderType; | 4 | use tui::{style::Color, widgets::BorderType}; |
| 5 | 5 | ||
| 6 | /// `BorderType` but impl `serde` | 6 | /// `BorderType` but impl `serde` |
| 7 | #[derive(Serialize, Deserialize, Clone, Copy)] | 7 | #[derive(Serialize, Deserialize, Clone, Copy)] |
| @@ -28,3 +28,176 @@ impl From<BorderTypeSerde> for BorderType { | |||
| 28 | } | 28 | } |
| 29 | } | 29 | } |
| 30 | } | 30 | } |
| 31 | |||
| 32 | /// all terminal colors | ||
| 33 | #[derive(Serialize, Deserialize, Clone)] | ||
| 34 | pub enum ColorVariantSerde { | ||
| 35 | Reset, | ||
| 36 | Black, | ||
| 37 | Red, | ||
| 38 | Green, | ||
| 39 | Yellow, | ||
| 40 | Blue, | ||
| 41 | Magenta, | ||
| 42 | Cyan, | ||
| 43 | Gray, | ||
| 44 | DarkGray, | ||
| 45 | LightRed, | ||
| 46 | LightGreen, | ||
| 47 | LightYellow, | ||
| 48 | LightBlue, | ||
| 49 | LightMagenta, | ||
| 50 | LightCyan, | ||
| 51 | White, | ||
| 52 | } | ||
| 53 | |||
| 54 | /// a single color | ||
| 55 | #[derive(Serialize, Deserialize, Clone)] | ||
| 56 | #[serde(untagged)] | ||
| 57 | pub enum ColorSerde { | ||
| 58 | ColorVariant(ColorVariantSerde), | ||
| 59 | Hex(String), | ||
| 60 | } | ||
| 61 | |||
| 62 | impl ColorSerde { | ||
| 63 | pub fn to_color(self) -> Option<Color> { | ||
| 64 | match self { | ||
| 65 | Self::ColorVariant(ColorVariantSerde::Reset) => Some(Color::Reset), | ||
| 66 | Self::ColorVariant(ColorVariantSerde::Black) => Some(Color::Black), | ||
| 67 | Self::ColorVariant(ColorVariantSerde::Red) => Some(Color::Red), | ||
| 68 | Self::ColorVariant(ColorVariantSerde::Green) => Some(Color::Green), | ||
| 69 | Self::ColorVariant(ColorVariantSerde::Yellow) => Some(Color::Yellow), | ||
| 70 | Self::ColorVariant(ColorVariantSerde::Blue) => Some(Color::Blue), | ||
| 71 | Self::ColorVariant(ColorVariantSerde::Magenta) => Some(Color::Magenta), | ||
| 72 | Self::ColorVariant(ColorVariantSerde::Cyan) => Some(Color::Cyan), | ||
| 73 | Self::ColorVariant(ColorVariantSerde::Gray) => Some(Color::Gray), | ||
| 74 | Self::ColorVariant(ColorVariantSerde::DarkGray) => Some(Color::DarkGray), | ||
| 75 | Self::ColorVariant(ColorVariantSerde::LightRed) => Some(Color::LightRed), | ||
| 76 | Self::ColorVariant(ColorVariantSerde::LightGreen) => Some(Color::LightGreen), | ||
| 77 | Self::ColorVariant(ColorVariantSerde::LightYellow) => Some(Color::LightYellow), | ||
| 78 | Self::ColorVariant(ColorVariantSerde::LightBlue) => Some(Color::LightBlue), | ||
| 79 | Self::ColorVariant(ColorVariantSerde::LightMagenta) => Some(Color::LightMagenta), | ||
| 80 | Self::ColorVariant(ColorVariantSerde::LightCyan) => Some(Color::LightCyan), | ||
| 81 | Self::ColorVariant(ColorVariantSerde::White) => Some(Color::White), | ||
| 82 | Self::Hex(s) => { | ||
| 83 | if s.len() != 7 || &s[0..1] != "#" { | ||
| 84 | return None; | ||
| 85 | } | ||
| 86 | |||
| 87 | Some(Color::Rgb( | ||
| 88 | from_hex(&s[1..3])?, | ||
| 89 | from_hex(&s[3..5])?, | ||
| 90 | from_hex(&s[5..7])?, | ||
| 91 | )) | ||
| 92 | } | ||
| 93 | } | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | /// converts a 2 digit hex number (00 - FF) to u8 | ||
| 98 | fn from_hex(s: &str) -> Option<u8> { | ||
| 99 | Some(from_hex_digit(&s[0..1])? * 16 + from_hex_digit(&s[1..2])?) | ||
| 100 | } | ||
| 101 | |||
| 102 | /// converts a single hex digit to u8 | ||
| 103 | fn from_hex_digit(d: &str) -> Option<u8> { | ||
| 104 | match d.to_uppercase().as_str() { | ||
| 105 | "0" => Some(0), | ||
| 106 | "1" => Some(1), | ||
| 107 | "2" => Some(2), | ||
| 108 | "3" => Some(3), | ||
| 109 | "4" => Some(4), | ||
| 110 | "5" => Some(5), | ||
| 111 | "6" => Some(6), | ||
| 112 | "7" => Some(7), | ||
| 113 | "8" => Some(8), | ||
| 114 | "9" => Some(9), | ||
| 115 | "A" => Some(10), | ||
| 116 | "B" => Some(11), | ||
| 117 | "C" => Some(12), | ||
| 118 | "D" => Some(13), | ||
| 119 | "E" => Some(14), | ||
| 120 | "F" => Some(15), | ||
| 121 | _ => None, | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | /// serde version of a single key code | ||
| 126 | #[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq)] | ||
| 127 | #[serde(untagged)] | ||
| 128 | pub enum KeyCodeSerde { | ||
| 129 | Char(char), | ||
| 130 | KeyVariants(KeyVariantsSerde), | ||
| 131 | F(String), | ||
| 132 | } | ||
| 133 | |||
| 134 | /// values of all default keys (excluding chars and function keys) | ||
| 135 | #[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq)] | ||
| 136 | pub enum KeyVariantsSerde { | ||
| 137 | Backspace, | ||
| 138 | Enter, | ||
| 139 | Left, | ||
| 140 | Right, | ||
| 141 | Up, | ||
| 142 | Down, | ||
| 143 | Home, | ||
| 144 | End, | ||
| 145 | PageUp, | ||
| 146 | PageDown, | ||
| 147 | Tab, | ||
| 148 | BackTab, | ||
| 149 | Delete, | ||
| 150 | Insert, | ||
| 151 | Null, | ||
| 152 | Esc, | ||
| 153 | CapsLock, | ||
| 154 | ScrollLock, | ||
| 155 | NumLock, | ||
| 156 | PrintScreen, | ||
| 157 | Pause, | ||
| 158 | Menu, | ||
| 159 | KeypadBegin, | ||
| 160 | } | ||
| 161 | |||
| 162 | impl KeyCodeSerde { | ||
| 163 | pub fn to_keycode(self) -> Option<KeyCode> { | ||
| 164 | match self { | ||
| 165 | Self::KeyVariants(KeyVariantsSerde::Backspace) => Some(KeyCode::Backspace), | ||
| 166 | Self::KeyVariants(KeyVariantsSerde::Enter) => Some(KeyCode::Enter), | ||
| 167 | Self::KeyVariants(KeyVariantsSerde::Left) => Some(KeyCode::Left), | ||
| 168 | Self::KeyVariants(KeyVariantsSerde::Right) => Some(KeyCode::Right), | ||
| 169 | Self::KeyVariants(KeyVariantsSerde::Up) => Some(KeyCode::Up), | ||
| 170 | Self::KeyVariants(KeyVariantsSerde::Down) => Some(KeyCode::Down), | ||
| 171 | Self::KeyVariants(KeyVariantsSerde::Home) => Some(KeyCode::Home), | ||
| 172 | Self::KeyVariants(KeyVariantsSerde::End) => Some(KeyCode::End), | ||
| 173 | Self::KeyVariants(KeyVariantsSerde::PageUp) => Some(KeyCode::PageUp), | ||
| 174 | Self::KeyVariants(KeyVariantsSerde::PageDown) => Some(KeyCode::PageDown), | ||
| 175 | Self::KeyVariants(KeyVariantsSerde::Tab) => Some(KeyCode::Tab), | ||
| 176 | Self::KeyVariants(KeyVariantsSerde::BackTab) => Some(KeyCode::BackTab), | ||
| 177 | Self::KeyVariants(KeyVariantsSerde::Delete) => Some(KeyCode::Delete), | ||
| 178 | Self::KeyVariants(KeyVariantsSerde::Insert) => Some(KeyCode::Insert), | ||
| 179 | Self::KeyVariants(KeyVariantsSerde::Null) => Some(KeyCode::Null), | ||
| 180 | Self::KeyVariants(KeyVariantsSerde::Esc) => Some(KeyCode::Esc), | ||
| 181 | Self::KeyVariants(KeyVariantsSerde::CapsLock) => Some(KeyCode::CapsLock), | ||
| 182 | Self::KeyVariants(KeyVariantsSerde::ScrollLock) => Some(KeyCode::ScrollLock), | ||
| 183 | Self::KeyVariants(KeyVariantsSerde::NumLock) => Some(KeyCode::NumLock), | ||
| 184 | Self::KeyVariants(KeyVariantsSerde::PrintScreen) => Some(KeyCode::PrintScreen), | ||
| 185 | Self::KeyVariants(KeyVariantsSerde::Pause) => Some(KeyCode::Pause), | ||
| 186 | Self::KeyVariants(KeyVariantsSerde::Menu) => Some(KeyCode::Menu), | ||
| 187 | Self::KeyVariants(KeyVariantsSerde::KeypadBegin) => Some(KeyCode::KeypadBegin), | ||
| 188 | Self::Char(c) => Some(KeyCode::Char(c)), | ||
| 189 | Self::F(s) => { | ||
| 190 | if s[0..1].to_uppercase().as_str() != "F" { | ||
| 191 | return None; | ||
| 192 | } | ||
| 193 | |||
| 194 | let parsed = s[1..].parse::<u8>(); | ||
| 195 | |||
| 196 | match parsed { | ||
| 197 | Ok(f) => Some(KeyCode::F(f)), | ||
| 198 | Err(_) => None, | ||
| 199 | } | ||
| 200 | } | ||
| 201 | } | ||
| 202 | } | ||
| 203 | } | ||
src/global/functions/download_all_images.rs +1 −1
| @@ -35,7 +35,7 @@ impl From<&Item> for Option<DownloadRequest> { | |||
| 35 | url: fullchannel.thumbnail_url.clone(), | 35 | url: fullchannel.thumbnail_url.clone(), |
| 36 | id: fullchannel.id.clone(), | 36 | id: fullchannel.id.clone(), |
| 37 | }, | 37 | }, |
| 38 | Item::Unknown(_) => return None, | 38 | Item::Unknown(_) | Item::Page(_) => return None, |
| 39 | }) | 39 | }) |
| 40 | } | 40 | } |
| 41 | } | 41 | } |
src/global/functions/mod.rs +2
| @@ -9,3 +9,5 @@ mod download_all_images; | |||
| 9 | pub use download_all_images::download_all_images; | 9 | pub use download_all_images::download_all_images; |
| 10 | mod popup_area; | 10 | mod popup_area; |
| 11 | pub use popup_area::popup_area; | 11 | pub use popup_area::popup_area; |
| 12 | mod run_command; | ||
| 13 | pub use run_command::run_command; | ||
src/global/functions/run_command.rs added +46
| @@ -0,0 +1,46 @@ | |||
| 1 | use crate::global::structs::{MainMenuPage, Page, Task, Tasks}; | ||
| 2 | use std::io::Stdout; | ||
| 3 | use tui::{backend::CrosstermBackend, Terminal}; | ||
| 4 | use tui_additions::framework::Framework; | ||
| 5 | |||
| 6 | /// WIP text command support | ||
| 7 | pub fn run_command( | ||
| 8 | command: &[&str], | ||
| 9 | framework: &mut Framework, | ||
| 10 | terminal: &mut Terminal<CrosstermBackend<Stdout>>, | ||
| 11 | ) { | ||
| 12 | // match a command splitted by space to a bunch of avaliable commands | ||
| 13 | match command { | ||
| 14 | [] => {} | ||
| 15 | ["loadpage"] => { /* help message */ } | ||
| 16 | ["loadpage", page] => { | ||
| 17 | let page = match *page { | ||
| 18 | "popular" => Some(Page::MainMenu(MainMenuPage::Popular)), | ||
| 19 | _ => None, | ||
| 20 | }; | ||
| 21 | |||
| 22 | if let Some(page) = page { | ||
| 23 | framework | ||
| 24 | .data | ||
| 25 | .state | ||
| 26 | .get_mut::<Tasks>() | ||
| 27 | .unwrap() | ||
| 28 | .priority | ||
| 29 | .push(Task::LoadPage(page)) | ||
| 30 | } | ||
| 31 | } | ||
| 32 | ["history"] => { /* help message */ } | ||
| 33 | ["history", "clear"] => { | ||
| 34 | framework.clear_history(); | ||
| 35 | } | ||
| 36 | ["flush"] => loop { | ||
| 37 | // runs all stacked actions | ||
| 38 | if let Some(tasks) = framework.data.state.get_mut::<Tasks>().unwrap().pop() { | ||
| 39 | let _res = tasks.run(framework, terminal); | ||
| 40 | continue; | ||
| 41 | } | ||
| 42 | break; | ||
| 43 | }, | ||
| 44 | _ => {} | ||
| 45 | } | ||
| 46 | } | ||
src/global/structs/item.rs +9 −1
| @@ -24,6 +24,7 @@ pub enum Item { | |||
| 24 | FullVideo(FullVideoItem), | 24 | FullVideo(FullVideoItem), |
| 25 | FullPlaylist(FullPlaylistItem), | 25 | FullPlaylist(FullPlaylistItem), |
| 26 | FullChannel(FullChannelItem), | 26 | FullChannel(FullChannelItem), |
| 27 | Page(bool), // true: next false: prev | ||
| 27 | Unknown(SearchItemTransition), | 28 | Unknown(SearchItemTransition), |
| 28 | } | 29 | } |
| 29 | 30 | ||
| @@ -113,6 +114,13 @@ impl Display for Item { | |||
| 113 | Self::FullVideo(video) => &video.title, | 114 | Self::FullVideo(video) => &video.title, |
| 114 | Self::FullPlaylist(playlist) => &playlist.title, | 115 | Self::FullPlaylist(playlist) => &playlist.title, |
| 115 | Self::FullChannel(channel) => &channel.name, | 116 | Self::FullChannel(channel) => &channel.name, |
| 117 | Self::Page(b) => { | ||
| 118 | if *b { | ||
| 119 | "Next page" | ||
| 120 | } else { | ||
| 121 | "Previous page" | ||
| 122 | } | ||
| 123 | } | ||
| 116 | Self::Unknown(_) => "Unknown item", | 124 | Self::Unknown(_) => "Unknown item", |
| 117 | }) | 125 | }) |
| 118 | } | 126 | } |
| @@ -183,7 +191,7 @@ impl Item { | |||
| 183 | Self::FullVideo(video) => &video.id, | 191 | Self::FullVideo(video) => &video.id, |
| 184 | Self::FullPlaylist(playlist) => &playlist.id, | 192 | Self::FullPlaylist(playlist) => &playlist.id, |
| 185 | Self::FullChannel(channel) => &channel.id, | 193 | Self::FullChannel(channel) => &channel.id, |
| 186 | Self::Unknown(_) => "invalid", | 194 | Self::Unknown(_) | Self::Page(_) => "invalid", |
| 187 | } | 195 | } |
| 188 | } | 196 | } |
| 189 | 197 | ||
src/global/traits/configtrait.rs +1 −1
| @@ -6,7 +6,7 @@ use std::{ | |||
| 6 | io::Write, | 6 | io::Write, |
| 7 | }; | 7 | }; |
| 8 | 8 | ||
| 9 | const EXTENSION: &str = "yml"; | 9 | pub const EXTENSION: &str = "yml"; |
| 10 | 10 | ||
| 11 | /// Trait for loading, saving config files | 11 | /// Trait for loading, saving config files |
| 12 | pub trait ConfigTrait { | 12 | pub trait ConfigTrait { |
src/init.rs +14 −6
| @@ -1,15 +1,17 @@ | |||
| 1 | use crate::{ | 1 | use crate::{ |
| 2 | config::{ | 2 | config::{ |
| 3 | AppearanceConfig, AppearanceConfigSerde, CommandsConfig, CommandsConfigSerde, | 3 | AppearanceConfig, CommandsConfig, CommandsConfigSerde, KeyBindingsConfig, MainConfig, |
| 4 | KeyBindingsConfig, MainConfig, MinDimentions, PagesConfig, Search, | 4 | MinDimentions, PagesConfig, Search, |
| 5 | }, | 5 | }, |
| 6 | global::{ | 6 | global::{ |
| 7 | functions::run_command, | ||
| 7 | structs::{InvidiousClient, Message, Page, Status, Tasks, WatchHistory}, | 8 | structs::{InvidiousClient, Message, Page, Status, Tasks, WatchHistory}, |
| 8 | traits::ConfigTrait, | 9 | traits::ConfigTrait, |
| 9 | }, | 10 | }, |
| 10 | }; | 11 | }; |
| 11 | use home::home_dir; | 12 | use home::home_dir; |
| 12 | use std::{error::Error, fs}; | 13 | use std::{error::Error, fs, io::Stdout}; |
| 14 | use tui::{backend::CrosstermBackend, Terminal}; | ||
| 13 | use tui_additions::framework::Framework; | 15 | use tui_additions::framework::Framework; |
| 14 | 16 | ||
| 15 | /// app to run before the app starts | 17 | /// app to run before the app starts |
| @@ -17,7 +19,10 @@ use tui_additions::framework::Framework; | |||
| 17 | // - create folders like `~/.config/youtube-tui/` and `~/.cache/youtube-tui/thumbnails/` | 19 | // - create folders like `~/.config/youtube-tui/` and `~/.cache/youtube-tui/thumbnails/` |
| 18 | // - load all config files | 20 | // - load all config files |
| 19 | // - insert data | 21 | // - insert data |
| 20 | pub fn init(framework: &mut Framework) -> Result<(), Box<dyn Error>> { | 22 | pub fn init( |
| 23 | framework: &mut Framework, | ||
| 24 | terminal: &mut Terminal<CrosstermBackend<Stdout>>, | ||
| 25 | ) -> Result<(), Box<dyn Error>> { | ||
| 21 | // creating files | 26 | // creating files |
| 22 | let home_dir = home_dir().unwrap(); | 27 | let home_dir = home_dir().unwrap(); |
| 23 | let config_path = home_dir.join(".config/youtube-tui/"); | 28 | let config_path = home_dir.join(".config/youtube-tui/"); |
| @@ -54,7 +59,7 @@ pub fn init(framework: &mut Framework) -> Result<(), Box<dyn Error>> { | |||
| 54 | framework | 59 | framework |
| 55 | .data | 60 | .data |
| 56 | .global | 61 | .global |
| 57 | .insert::<AppearanceConfig>(AppearanceConfig::from(*AppearanceConfigSerde::load()?)); | 62 | .insert::<AppearanceConfig>(AppearanceConfig::load()?); |
| 58 | framework.data.global.insert::<MainConfig>(main_config); | 63 | framework.data.global.insert::<MainConfig>(main_config); |
| 59 | framework | 64 | framework |
| 60 | .data | 65 | .data |
| @@ -63,7 +68,7 @@ pub fn init(framework: &mut Framework) -> Result<(), Box<dyn Error>> { | |||
| 63 | framework | 68 | framework |
| 64 | .data | 69 | .data |
| 65 | .global | 70 | .global |
| 66 | .insert::<KeyBindingsConfig>(*KeyBindingsConfig::load()?); | 71 | .insert::<KeyBindingsConfig>(KeyBindingsConfig::load()?); |
| 67 | framework.data.global.insert::<Message>(Message::None); | 72 | framework.data.global.insert::<Message>(Message::None); |
| 68 | framework | 73 | framework |
| 69 | .data | 74 | .data |
| @@ -79,5 +84,8 @@ pub fn init(framework: &mut Framework) -> Result<(), Box<dyn Error>> { | |||
| 79 | .state | 84 | .state |
| 80 | .insert::<MinDimentions>(MinDimentions::default()); | 85 | .insert::<MinDimentions>(MinDimentions::default()); |
| 81 | 86 | ||
| 87 | run_command(&["loadpage", "popular"], framework, terminal); | ||
| 88 | run_command(&["flush"], framework, terminal); | ||
| 89 | run_command(&["history", "clear"], framework, terminal); | ||
| 82 | Ok(()) | 90 | Ok(()) |
| 83 | } | 91 | } |
src/items/iteminfo.rs +16 −3
| @@ -48,7 +48,11 @@ impl FrameworkItem for ItemInfo { | |||
| 48 | && main_config.images.display() | 48 | && main_config.images.display() |
| 49 | && !status.popup_opened | 49 | && !status.popup_opened |
| 50 | { | 50 | { |
| 51 | let mut scroll = 0; | 51 | #[cfg(any(feature = "sixel", feature = "halfblock"))] |
| 52 | let scroll; | ||
| 53 | |||
| 54 | #[cfg(not(any(feature = "sixel", feature = "halfblock")))] | ||
| 55 | let scroll = 0; | ||
| 52 | 56 | ||
| 53 | #[cfg(any(feature = "sixel", feature = "halfblock"))] | 57 | #[cfg(any(feature = "sixel", feature = "halfblock"))] |
| 54 | { | 58 | { |
| @@ -85,13 +89,15 @@ impl FrameworkItem for ItemInfo { | |||
| 85 | .priority | 89 | .priority |
| 86 | .push(Task::LazyRendered); | 90 | .push(Task::LazyRendered); |
| 87 | } else { | 91 | } else { |
| 88 | scroll = 0; | 92 | scroll = self.lazy_scroll; |
| 89 | } | 93 | } |
| 94 | } else { | ||
| 95 | scroll = self.lazy_scroll; | ||
| 90 | } | 96 | } |
| 91 | } | 97 | } |
| 92 | scroll | 98 | scroll |
| 93 | } else { | 99 | } else { |
| 94 | 0 | 100 | self.lazy_scroll |
| 95 | }; | 101 | }; |
| 96 | 102 | ||
| 97 | self.lazy_scroll = scroll; | 103 | self.lazy_scroll = scroll; |
| @@ -325,6 +331,13 @@ impl FrameworkItem for ItemInfo { | |||
| 325 | Style::default().fg(appearance.colors.item_info.description), | 331 | Style::default().fg(appearance.colors.item_info.description), |
| 326 | )), | 332 | )), |
| 327 | ), | 333 | ), |
| 334 | Item::Page(b) => ( | ||
| 335 | vec![( | ||
| 336 | if *b { "Next page" } else { "Previous page" }.to_string(), | ||
| 337 | Style::default().fg(appearance.colors.item_info.page_turner), | ||
| 338 | )], | ||
| 339 | None, | ||
| 340 | ), | ||
| 328 | }; | 341 | }; |
| 329 | 342 | ||
| 330 | let mut y = if scroll >= area.height { 0 } else { scroll } + area.y; | 343 | let mut y = if scroll >= area.height { 0 } else { scroll } + area.y; |
src/items/itemlist.rs +46 −17
| @@ -8,13 +8,14 @@ use tui_additions::{ | |||
| 8 | 8 | ||
| 9 | use super::ItemInfo; | 9 | use super::ItemInfo; |
| 10 | use crate::{ | 10 | use crate::{ |
| 11 | config::{AppearanceConfig, KeyBindingsConfig, MainConfig}, | 11 | config::{AppearanceConfig, KeyBindingsConfig, MainConfig, Search}, |
| 12 | global::{ | 12 | global::{ |
| 13 | functions::download_all_images, | 13 | functions::download_all_images, |
| 14 | structs::{ | 14 | structs::{ |
| 15 | ChannelDisplayPage, ChannelDisplayPageType, FullPlaylistItem, FullVideoItem, | 15 | ChannelDisplayPage, ChannelDisplayPageType, FullChannelItem, FullPlaylistItem, |
| 16 | InvidiousClient, Item, KeyAction, MainMenuPage, MiniChannelItem, MiniPlaylistItem, | 16 | FullVideoItem, InvidiousClient, Item, KeyAction, MainMenuPage, Message, |
| 17 | MiniVideoItem, Page, SingleItemPage, Status, Task, Tasks, WatchHistory, | 17 | MiniChannelItem, MiniPlaylistItem, MiniVideoItem, Page, SingleItemPage, Status, Task, |
| 18 | Tasks, WatchHistory, | ||
| 18 | }, | 19 | }, |
| 19 | }, | 20 | }, |
| 20 | }; | 21 | }; |
| @@ -180,6 +181,12 @@ impl FrameworkItem for ItemList { | |||
| 180 | .into_iter() | 181 | .into_iter() |
| 181 | .map(|item| Item::from_search_item(item, image_index)) | 182 | .map(|item| Item::from_search_item(item, image_index)) |
| 182 | .collect(); | 183 | .collect(); |
| 184 | if !self.items.is_empty() { | ||
| 185 | self.items.push(Item::Page(true)); | ||
| 186 | } | ||
| 187 | if search.page != 1 { | ||
| 188 | self.items.insert(0, Item::Page(false)); | ||
| 189 | } | ||
| 183 | } | 190 | } |
| 184 | _ => unreachable!("item `ItemList` cannot be used in `{page:?}`"), | 191 | _ => unreachable!("item `ItemList` cannot be used in `{page:?}`"), |
| 185 | } | 192 | } |
| @@ -236,27 +243,49 @@ impl FrameworkItem for ItemList { | |||
| 236 | let page_to_load = match &self.items[self.textlist.selected] { | 243 | let page_to_load = match &self.items[self.textlist.selected] { |
| 237 | Item::MiniVideo(MiniVideoItem { id, .. }) | 244 | Item::MiniVideo(MiniVideoItem { id, .. }) |
| 238 | | Item::FullVideo(FullVideoItem { id, .. }) => { | 245 | | Item::FullVideo(FullVideoItem { id, .. }) => { |
| 239 | Page::SingleItem(SingleItemPage::Video(id.clone())) | 246 | Some(Page::SingleItem(SingleItemPage::Video(id.clone()))) |
| 240 | } | 247 | } |
| 241 | Item::MiniPlaylist(MiniPlaylistItem { id, .. }) | 248 | Item::MiniPlaylist(MiniPlaylistItem { id, .. }) |
| 242 | | Item::FullPlaylist(FullPlaylistItem { id, .. }) => { | 249 | | Item::FullPlaylist(FullPlaylistItem { id, .. }) => { |
| 243 | Page::SingleItem(SingleItemPage::Playlist(id.clone())) | 250 | Some(Page::SingleItem(SingleItemPage::Playlist(id.clone()))) |
| 244 | } | 251 | } |
| 245 | Item::MiniChannel(MiniChannelItem { id, .. }) => { | 252 | Item::MiniChannel(MiniChannelItem { id, .. }) |
| 246 | Page::ChannelDisplay(ChannelDisplayPage { | 253 | | Item::FullChannel(FullChannelItem { id, .. }) => { |
| 254 | Some(Page::ChannelDisplay(ChannelDisplayPage { | ||
| 247 | id: id.clone(), | 255 | id: id.clone(), |
| 248 | r#type: ChannelDisplayPageType::Main, | 256 | r#type: ChannelDisplayPageType::Main, |
| 249 | }) | 257 | })) |
| 250 | } | 258 | } |
| 251 | _ => panic!("wtf did you just selected"), | 259 | Item::Unknown(_) => { |
| 260 | *framework.data.global.get_mut::<Message>().unwrap() = | ||
| 261 | Message::Message(String::from("Unknown item")); | ||
| 262 | framework | ||
| 263 | .data | ||
| 264 | .state | ||
| 265 | .get_mut::<Tasks>() | ||
| 266 | .unwrap() | ||
| 267 | .priority | ||
| 268 | .push(Task::RenderAll); | ||
| 269 | None | ||
| 270 | } | ||
| 271 | Item::Page(b) => match framework.data.state.get::<Page>().unwrap() { | ||
| 272 | Page::Search(search) => Some(Page::Search(Search { | ||
| 273 | page: if *b { search.page + 1 } else { search.page - 1 }, | ||
| 274 | ..search.clone() | ||
| 275 | })), | ||
| 276 | _ => unreachable!("Page turners can only be used in search pages"), | ||
| 277 | }, | ||
| 252 | }; | 278 | }; |
| 253 | framework | 279 | |
| 254 | .data | 280 | if let Some(page_to_load) = page_to_load { |
| 255 | .state | 281 | framework |
| 256 | .get_mut::<Tasks>() | 282 | .data |
| 257 | .unwrap() | 283 | .state |
| 258 | .priority | 284 | .get_mut::<Tasks>() |
| 259 | .push(Task::LoadPage(page_to_load)); | 285 | .unwrap() |
| 286 | .priority | ||
| 287 | .push(Task::LoadPage(page_to_load)); | ||
| 288 | } | ||
| 260 | false | 289 | false |
| 261 | } | 290 | } |
| 262 | _ => false, | 291 | _ => false, |
src/main.rs +5 −2
| @@ -16,7 +16,6 @@ use youtube_tui::{exit, init, run}; | |||
| 16 | fn main() -> Result<(), Box<dyn Error>> { | 16 | fn main() -> Result<(), Box<dyn Error>> { |
| 17 | let state = State(Vec::new()); | 17 | let state = State(Vec::new()); |
| 18 | let mut framework = Framework::new(state); | 18 | let mut framework = Framework::new(state); |
| 19 | init(&mut framework)?; | ||
| 20 | 19 | ||
| 21 | enable_raw_mode()?; | 20 | enable_raw_mode()?; |
| 22 | let mut stdout = io::stdout(); | 21 | let mut stdout = io::stdout(); |
| @@ -24,7 +23,11 @@ fn main() -> Result<(), Box<dyn Error>> { | |||
| 24 | let backend = CrosstermBackend::new(stdout); | 23 | let backend = CrosstermBackend::new(stdout); |
| 25 | let mut terminal = Terminal::new(backend)?; | 24 | let mut terminal = Terminal::new(backend)?; |
| 26 | 25 | ||
| 27 | let res = run(&mut terminal, &mut framework); | 26 | let res = (|| -> Result<(), Box<dyn Error>> { |
| 27 | init(&mut framework, &mut terminal)?; | ||
| 28 | run(&mut terminal, &mut framework)?; | ||
| 29 | Ok(()) | ||
| 30 | })(); | ||
| 28 | 31 | ||
| 29 | disable_raw_mode()?; | 32 | disable_raw_mode()?; |
| 30 | execute!( | 33 | execute!( |
src/run.rs +1 −9
| @@ -5,7 +5,7 @@ use tui_additions::framework::{Framework, FrameworkDirection}; | |||
| 5 | 5 | ||
| 6 | use crate::{ | 6 | use crate::{ |
| 7 | config::KeyBindingsConfig, | 7 | config::KeyBindingsConfig, |
| 8 | global::structs::{KeyAction, MainMenuPage, Message, Page, Status, Task, Tasks}, | 8 | global::structs::{KeyAction, Message, Status, Task, Tasks}, |
| 9 | }; | 9 | }; |
| 10 | 10 | ||
| 11 | /// the main event loop of the program | 11 | /// the main event loop of the program |
| @@ -13,14 +13,6 @@ pub fn run( | |||
| 13 | terminal: &mut Terminal<CrosstermBackend<Stdout>>, | 13 | terminal: &mut Terminal<CrosstermBackend<Stdout>>, |
| 14 | framework: &mut Framework, | 14 | framework: &mut Framework, |
| 15 | ) -> Result<(), Box<dyn Error>> { | 15 | ) -> Result<(), Box<dyn Error>> { |
| 16 | // push the initial page load | ||
| 17 | let tasks = framework.data.state.get_mut::<Tasks>().unwrap(); | ||
| 18 | tasks | ||
| 19 | .priority | ||
| 20 | .push(Task::LoadPage(Page::MainMenu(MainMenuPage::Popular))); | ||
| 21 | tasks.pop().unwrap().run(framework, terminal)?; | ||
| 22 | framework.clear_history(); | ||
| 23 | |||
| 24 | loop { | 16 | loop { |
| 25 | // repeat forever until all tasks are ran (and Tasks is cleared) | 17 | // repeat forever until all tasks are ran (and Tasks is cleared) |
| 26 | if let Some(tasks) = framework.data.state.get_mut::<Tasks>().unwrap().pop() { | 18 | if let Some(tasks) = framework.data.state.get_mut::<Tasks>().unwrap().pop() { |