Commit dcb033702a
Unsigned
Layout: unified · split
README.md +2 −2
| @@ -77,9 +77,9 @@ It also includes features like: | ||
| 77 | 77 | ### What it doesn't have |
| 78 | 78 | |
| 79 | 79 | - [URGENT] Replace the no-longer-going-to-compile `typemap` dependency with either a self implementation of it or a useable crate. |
| 80 | - Strip down some libraries to reduce compile speed. | |
| 80 | - Strip down some libraries to reduce compile speed, specifically removing. | |
| 81 | 81 | - Add command section for channel main page (first priority) |
| 82 | - Up arrow command history + Ctrl arrow keys move between words | |
| 82 | - Add NewPipe as a source alongside Invidious | |
| 83 | 83 | |
| 84 | 84 | ## How to contribute |
| 85 | 85 | |
docs/src/commands.md +5
| @@ -42,6 +42,7 @@ flush | ||
| 42 | 42 | quit (alt `q`, `exit`, `x`) |
| 43 | 43 | run [command] |
| 44 | 44 | parrun [command] |
| 45 | key [keycode] [keymodifier] | |
| 45 | 46 | ``` |
| 46 | 47 | |
| 47 | 48 | > The `flush` command is used to run all tasks in queue immediately, this is usually done automatically. |
| @@ -50,6 +51,10 @@ parrun [command] | ||
| 50 | 51 | |
| 51 | 52 | > `run` is used for running *blocking commands*, while `parrun` is non-blocking. |
| 52 | 53 | |
| 54 | > Valid keycodes are the same as in [`keybindings.yml`](./config/keybindings.md) and [`commandbindings.yml`](./config/commandbindings.md). For a full list of keys, check out [`KeyCodeSerde`](https://docs.rs/youtube-tui/latest/youtube_tui/config/serde/enum.KeyCodeSerde.html) in [`/src/config/serde.rs`](https://github.com/Siriusmart/youtube-tui/blob/master/src/config/serde.rs). | |
| 55 | > | |
| 56 | > More about keymodifiers can be found in the doc page for [`keybindings.yml`](./config/keybindings.md) | |
| 57 | ||
| 53 | 58 | ## Library |
| 54 | 59 | |
| 55 | 60 | ```vim |
docs/src/config/keybindings.md +3 −3
| @@ -58,9 +58,9 @@ Each modifier has its own code, for instance `Shift` would be `1` and `Ctrl` wou | ||
| 58 | 58 | |
| 59 | 59 | All possible keys can be found <a href="https://docs.rs/crossterm/latest/crossterm/event/enum.KeyCode.html" target=_blank>*here*</a>. |
| 60 | 60 | |
| 61 | > Enums are represented using the character `!`, for example the `q` key would be `!Char 'q'` | |
| 62 | > | |
| 63 | > `Shift + Q` however would be `!Char 'Q'` with `0` as modifier code as `Shift` turns `q` into an upper case character. | |
| 61 | ### Key actions | |
| 62 | ||
| 63 | All key actions can be found in [`KeyAction`](https://docs.rs/youtube-tui/latest/youtube_tui/global/structs/enum.KeyAction.html) under [`/src/global/structs/keyaction.rs`](https://github.com/Siriusmart/youtube-tui/blob/master/src/global/structs/keyaction.rs). | |
| 64 | 64 | |
| 65 | 65 | ### Modifiers reference |
| 66 | 66 | |
docs/src/config/main.md +35 −1
| @@ -82,6 +82,30 @@ Whether to refresh the current search page after search filters are modified | ||
| 82 | 82 | |
| 83 | 83 | *Accept: `true`/`false`* |
| 84 | 84 | |
| 85 | ### download_images | |
| 86 | ||
| 87 | Whether to download images for all subscription videos | |
| 88 | ||
| 89 | *Accept: `true`/`false`* | |
| 90 | ||
| 91 | ### sync_channel_info | |
| 92 | ||
| 93 | Whether to also request an updated version of the channel info when syncing feeds. Disabling this halves the number of requests sent to Invidious, but channel info will not be updated (the videos will). | |
| 94 | ||
| 95 | *Accept: `true`/`false`* | |
| 96 | ||
| 97 | ### sync_channel_cooldown_secs | |
| 98 | ||
| 99 | Sync all do not sync channel info if it has been synced in the past *n* seconds. | |
| 100 | ||
| 101 | *Accept: integer value seconds* | |
| 102 | ||
| 103 | ### sync_videos_cooldown_secs | |
| 104 | ||
| 105 | Sync all do not sync videos from channel if it has been synced in the past *n* seconds. | |
| 106 | ||
| 107 | *Accept: integer value seconds* | |
| 108 | ||
| 85 | 109 | ### image_index |
| 86 | 110 | |
| 87 | 111 | The index in the array of thumbnail qualities you want to download |
| @@ -104,7 +128,7 @@ Usually you don't want to use the max resolution as it will create a large gap b | ||
| 104 | 128 | |
| 105 | 129 | *Accept: integer that is a valid index* |
| 106 | 130 | |
| 107 | ### Provider | |
| 131 | ### provider | |
| 108 | 132 | |
| 109 | 133 | This changes the `${url}` and `${embed-url}` of videos, allowing you to watch videos from Invidious if it is restricted on YouTube. |
| 110 | 134 | |
| @@ -112,6 +136,16 @@ This changes the `${url}` and `${embed-url}` of videos, allowing you to watch vi | ||
| 112 | 136 | |
| 113 | 137 | *Accept: `YouTube`/`Invidious`* |
| 114 | 138 | |
| 139 | ### shell | |
| 140 | ||
| 141 | Shell used to run commands, such as `sh` (default), `bash` or even `zsh`. | |
| 142 | ||
| 143 | *Accept: any valid path to a shell binary* | |
| 144 | ||
| 145 | ### legacy_input_handling | |
| 146 | ||
| 147 | Switch back the old key input handling where the selected item captures all input (except escape). | |
| 148 | ||
| 115 | 149 | ### env |
| 116 | 150 | |
| 117 | 151 | Env are variables that can be used in `commands.yml`, this allows you to change multiple commands by modifying just one env variable. And not to be confused with system/terminal environment variables, these are just *"a thing"* that you can use in the TUI. |
docs/src/config/pages.md +5
| @@ -23,6 +23,7 @@ main_menu: | ||
| 23 | 23 | items: |
| 24 | 24 | - MessageBar |
| 25 | 25 | message: Loading main menu... |
| 26 | command: key Esc 0 ;; key Up 0 ;; key Left 0 ;; key Enter 0 # this focuses on the search bar | |
| 26 | 27 | |
| 27 | 28 | # and much more ... |
| 28 | 29 | ``` |
| @@ -43,6 +44,10 @@ Each item are ordered from left to right. | ||
| 43 | 44 | |
| 44 | 45 | The message to display when loading the page. |
| 45 | 46 | |
| 47 | ## Command | |
| 48 | ||
| 49 | Command to run when the page loads, you can use the `key` command to select an item on load. | |
| 50 | ||
| 46 | 51 | ### Items reference |
| 47 | 52 | |
| 48 | 53 | |Item|Can be used in page|Description| |
src/config/keybindings.rs +8 −3
| @@ -39,14 +39,19 @@ impl ConfigTrait for KeyBindingsConfigSerde { | ||
| 39 | 39 | impl Default for KeyBindingsConfigSerde { |
| 40 | 40 | fn default() -> Self { |
| 41 | 41 | Self(HashMap::from([ |
| 42 | // movement keys // Alt left should be back | |
| 42 | // movement keys | |
| 43 | // Alt left should be back | |
| 43 | 44 | ( |
| 44 | 45 | KeyCodeSerde::KeyVariants(KeyVariantsSerde::Left), |
| 45 | HashMap::from([(0, KeyAction::MoveLeft), (4, KeyAction::Back)]), | |
| 46 | HashMap::from([ | |
| 47 | (0, KeyAction::MoveLeft), | |
| 48 | (2, KeyAction::PreviousWord), | |
| 49 | (4, KeyAction::Back), | |
| 50 | ]), | |
| 46 | 51 | ), |
| 47 | 52 | ( |
| 48 | 53 | KeyCodeSerde::KeyVariants(KeyVariantsSerde::Right), |
| 49 | HashMap::from([(0, KeyAction::MoveRight)]), | |
| 54 | HashMap::from([(0, KeyAction::MoveRight), (2, KeyAction::NextWord)]), | |
| 50 | 55 | ), |
| 51 | 56 | ( |
| 52 | 57 | KeyCodeSerde::KeyVariants(KeyVariantsSerde::Up), |
src/config/pages.rs +6
| @@ -66,6 +66,7 @@ impl Default for PagesConfig { | ||
| 66 | 66 | pub struct PageConfig { |
| 67 | 67 | pub layout: Vec<PageRow>, |
| 68 | 68 | pub message: String, |
| 69 | pub command: String, | |
| 69 | 70 | } |
| 70 | 71 | |
| 71 | 72 | impl PageConfig { |
| @@ -290,6 +291,7 @@ fn main_menu_default() -> PageConfig { | ||
| 290 | 291 | PageRow::from_vec(vec![PageItems::MessageBar], false), |
| 291 | 292 | ], |
| 292 | 293 | message: String::from("Loading main menu..."), |
| 294 | command: String::from("key Esc 0 ;; key Up 0 ;; key Left 0 ;; key Enter 0"), | |
| 293 | 295 | } |
| 294 | 296 | } |
| 295 | 297 | |
| @@ -301,6 +303,7 @@ fn search_default() -> PageConfig { | ||
| 301 | 303 | PageRow::from_vec(vec![PageItems::MessageBar], false), |
| 302 | 304 | ], |
| 303 | 305 | message: String::from("Loading search results..."), |
| 306 | command: String::from("key Esc 0 ;; key Down 0 ;; key Enter 0"), | |
| 304 | 307 | } |
| 305 | 308 | } |
| 306 | 309 | |
| @@ -312,6 +315,7 @@ fn singleitem_default() -> PageConfig { | ||
| 312 | 315 | PageRow::from_vec(vec![PageItems::MessageBar], false), |
| 313 | 316 | ], |
| 314 | 317 | message: String::from("Loading item details..."), |
| 318 | command: String::from("key Esc 0 ;; key Down 0 ;; key Enter 0"), | |
| 315 | 319 | } |
| 316 | 320 | } |
| 317 | 321 | |
| @@ -331,6 +335,7 @@ fn channeldisplay_default() -> PageConfig { | ||
| 331 | 335 | PageRow::from_vec(vec![PageItems::MessageBar], false), |
| 332 | 336 | ], |
| 333 | 337 | message: String::from("Loading channel details..."), |
| 338 | command: String::from("key Esc 0 ;; key Down 0 ;; key Enter 0"), | |
| 334 | 339 | } |
| 335 | 340 | } |
| 336 | 341 | |
| @@ -347,5 +352,6 @@ fn feed_default() -> PageConfig { | ||
| 347 | 352 | PageRow::from_vec(vec![PageItems::MessageBar], false), |
| 348 | 353 | ], |
| 349 | 354 | message: String::from("Loading feed..."), |
| 355 | command: String::from("key Esc 0 ;; key Down 0 ;; key Down 0 ;; key Enter 0"), | |
| 350 | 356 | } |
| 351 | 357 | } |
src/config/serde.rs +2 −2
| @@ -123,7 +123,7 @@ fn from_hex_digit(d: &str) -> Option<u8> { | ||
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | /// serde version of a single key code |
| 126 | #[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq)] | |
| 126 | #[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq, Debug)] | |
| 127 | 127 | #[serde(untagged)] |
| 128 | 128 | pub enum KeyCodeSerde { |
| 129 | 129 | Char(char), |
| @@ -132,7 +132,7 @@ pub enum KeyCodeSerde { | ||
| 132 | 132 | } |
| 133 | 133 | |
| 134 | 134 | /// values of all default keys (excluding chars and function keys) |
| 135 | #[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq)] | |
| 135 | #[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq, Debug)] | |
| 136 | 136 | pub enum KeyVariantsSerde { |
| 137 | 137 | Backspace, |
| 138 | 138 | Enter, |
src/global/functions/command_capture.rs +40
| @@ -66,6 +66,46 @@ pub fn command_capture(framework: &mut FrameworkClean, key: KeyEvent) -> bool { | ||
| 66 | 66 | .clear(); |
| 67 | 67 | return true; |
| 68 | 68 | } |
| 69 | Some(KeyAction::PreviousWord) => { | |
| 70 | previous_word( | |
| 71 | framework | |
| 72 | .data | |
| 73 | .global | |
| 74 | .get_mut::<Status>() | |
| 75 | .unwrap() | |
| 76 | .command_capture | |
| 77 | .as_mut() | |
| 78 | .unwrap(), | |
| 79 | ); | |
| 80 | framework | |
| 81 | .data | |
| 82 | .state | |
| 83 | .get_mut::<Tasks>() | |
| 84 | .unwrap() | |
| 85 | .priority | |
| 86 | .push(Task::RenderAll); | |
| 87 | return true; | |
| 88 | } | |
| 89 | Some(KeyAction::NextWord) => { | |
| 90 | next_word( | |
| 91 | framework | |
| 92 | .data | |
| 93 | .global | |
| 94 | .get_mut::<Status>() | |
| 95 | .unwrap() | |
| 96 | .command_capture | |
| 97 | .as_mut() | |
| 98 | .unwrap(), | |
| 99 | ); | |
| 100 | framework | |
| 101 | .data | |
| 102 | .state | |
| 103 | .get_mut::<Tasks>() | |
| 104 | .unwrap() | |
| 105 | .priority | |
| 106 | .push(Task::RenderAll); | |
| 107 | return true; | |
| 108 | } | |
| 69 | 109 | _ => {} |
| 70 | 110 | } |
| 71 | 111 | |
src/global/functions/key_input.rs added +214
| @@ -0,0 +1,214 @@ | ||
| 1 | use std::{any::TypeId, io::Stdout}; | |
| 2 | ||
| 3 | use crossterm::event::KeyEvent; | |
| 4 | use ratatui::{backend::CrosstermBackend, Terminal}; | |
| 5 | use tui_additions::{ | |
| 6 | framework::{Framework, FrameworkDirection}, | |
| 7 | widgets::TextField, | |
| 8 | }; | |
| 9 | ||
| 10 | use crate::{config::*, global::structs::*, items::SearchBar}; | |
| 11 | ||
| 12 | use super::*; | |
| 13 | ||
| 14 | pub fn key_input( | |
| 15 | key: KeyEvent, | |
| 16 | framework: &mut Framework, | |
| 17 | terminal: &mut Terminal<CrosstermBackend<Stdout>>, | |
| 18 | ) { | |
| 19 | // check if key binds to any commands | |
| 20 | let command_to_run = framework | |
| 21 | .data | |
| 22 | .global | |
| 23 | .get::<CommandBindings>() | |
| 24 | .unwrap() | |
| 25 | .get_command(&key, framework.data.state.get::<Page>().unwrap()); | |
| 26 | if !command_to_run.is_empty() { | |
| 27 | run_command(&apply_envs(command_to_run), framework, terminal); | |
| 28 | framework | |
| 29 | .data | |
| 30 | .state | |
| 31 | .get_mut::<Tasks>() | |
| 32 | .unwrap() | |
| 33 | .priority | |
| 34 | .push(Task::RenderAll); | |
| 35 | } | |
| 36 | ||
| 37 | // 1. get the corresponding action | |
| 38 | // 2. check if action is deselect, if yes, deselect | |
| 39 | // 3. check is anything is selected, if yes, run `.key_event()` with the key | |
| 40 | // 4. if nothing is selected, do stuff like moving the cursor or exiting | |
| 41 | ||
| 42 | let action = framework | |
| 43 | .data | |
| 44 | .global | |
| 45 | .get::<KeyBindingsConfig>() | |
| 46 | .unwrap() | |
| 47 | .get(key); | |
| 48 | ||
| 49 | if action == Some(KeyAction::Deselect) { | |
| 50 | let _ = framework.deselect(); | |
| 51 | framework | |
| 52 | .data | |
| 53 | .state | |
| 54 | .get_mut::<Tasks>() | |
| 55 | .unwrap() | |
| 56 | .priority | |
| 57 | .push(Task::RenderAll); | |
| 58 | framework | |
| 59 | .data | |
| 60 | .global | |
| 61 | .get_mut::<Status>() | |
| 62 | .unwrap() | |
| 63 | .command_capture = None; | |
| 64 | return; | |
| 65 | } | |
| 66 | ||
| 67 | // the first part check for if the keys should be captured for entering commands | |
| 68 | if framework | |
| 69 | .data | |
| 70 | .global | |
| 71 | .get::<Status>() | |
| 72 | .unwrap() | |
| 73 | .command_capture | |
| 74 | .is_some() | |
| 75 | // the second part runs the command, and see if the returned boolean | |
| 76 | // `is_updated` is true, if so update the screen | |
| 77 | && command_capture(&mut framework.split_clean().0, key) | |
| 78 | { | |
| 79 | framework | |
| 80 | .data | |
| 81 | .state | |
| 82 | .get_mut::<Tasks>() | |
| 83 | .unwrap() | |
| 84 | .priority | |
| 85 | .push(Task::RenderAll); | |
| 86 | return; | |
| 87 | } | |
| 88 | ||
| 89 | // if something is selected, pass the key input into that item | |
| 90 | // if nothing is selected, the following big chunk of code handles to movement of | |
| 91 | // cursor and stuff | |
| 92 | if framework.is_selected() { | |
| 93 | let (x, y) = framework.cursor.selected(&framework.selectables).unwrap(); | |
| 94 | if let Err(e) = framework.key_input(key) { | |
| 95 | *framework.data.global.get_mut::<Message>().unwrap() = Message::Error(e.to_string()); | |
| 96 | }; | |
| 97 | ||
| 98 | let selected = framework.state.get_mut(x, y); | |
| 99 | if framework | |
| 100 | .data | |
| 101 | .global | |
| 102 | .get::<MainConfig>() | |
| 103 | .unwrap() | |
| 104 | .legacy_input_handling | |
| 105 | || (*selected).type_id() == TypeId::of::<SearchBar>() | |
| 106 | { | |
| 107 | return; | |
| 108 | } | |
| 109 | } | |
| 110 | ||
| 111 | if let Some(action) = action { | |
| 112 | let mut render = true; | |
| 113 | match action { | |
| 114 | KeyAction::StartCommandCapture => { | |
| 115 | framework | |
| 116 | .data | |
| 117 | .global | |
| 118 | .get_mut::<Status>() | |
| 119 | .unwrap() | |
| 120 | .command_capture = Some(TextField::default()); | |
| 121 | } | |
| 122 | KeyAction::Exit => framework | |
| 123 | .data | |
| 124 | .state | |
| 125 | .get_mut::<Tasks>() | |
| 126 | .unwrap() | |
| 127 | .priority | |
| 128 | .push(Task::Command("q".to_string())), | |
| 129 | KeyAction::MoveUp if !framework.is_selected() => { | |
| 130 | let _ = framework.r#move(FrameworkDirection::Up); | |
| 131 | } | |
| 132 | KeyAction::MoveDown if !framework.is_selected() => { | |
| 133 | let _ = framework.r#move(FrameworkDirection::Down); | |
| 134 | } | |
| 135 | KeyAction::MoveLeft if !framework.is_selected() => { | |
| 136 | let _ = framework.r#move(FrameworkDirection::Left); | |
| 137 | } | |
| 138 | KeyAction::MoveRight if !framework.is_selected() => { | |
| 139 | let _ = framework.r#move(FrameworkDirection::Right); | |
| 140 | } | |
| 141 | KeyAction::Reload => framework | |
| 142 | .data | |
| 143 | .state | |
| 144 | .get_mut::<Tasks>() | |
| 145 | .unwrap() | |
| 146 | .priority | |
| 147 | .push(Task::Reload), | |
| 148 | KeyAction::Back => { | |
| 149 | if !framework.history.is_empty() { | |
| 150 | clear_envs(&mut framework.data.state.get::<StateEnvs>().unwrap().0.clone()); | |
| 151 | } | |
| 152 | if framework.revert_last_history().is_err() { | |
| 153 | *framework.data.global.get_mut::<Message>().unwrap() = | |
| 154 | Message::Error(String::from("This is already the beginning of history")) | |
| 155 | } else { | |
| 156 | let status = framework.data.global.get_mut::<Status>().unwrap(); | |
| 157 | status.render_image = true; | |
| 158 | ||
| 159 | framework | |
| 160 | .data | |
| 161 | .state | |
| 162 | .get_mut::<Tasks>() | |
| 163 | .unwrap() | |
| 164 | .priority | |
| 165 | .push(Task::ClearPage); | |
| 166 | let state_envs = framework.data.state.get_mut::<StateEnvs>().unwrap(); | |
| 167 | set_envs(state_envs.clone().0.into_iter(), &mut state_envs.0); | |
| 168 | update_provider(&mut framework.data); | |
| 169 | } | |
| 170 | } | |
| 171 | KeyAction::FirstHistory => { | |
| 172 | if framework.revert_history(0).is_err() { | |
| 173 | *framework.data.global.get_mut::<Message>().unwrap() = | |
| 174 | Message::Error(String::from("This is already the beginning of history")) | |
| 175 | } else { | |
| 176 | let status = framework.data.global.get_mut::<Status>().unwrap(); | |
| 177 | status.render_image = true; | |
| 178 | ||
| 179 | update_provider(&mut framework.data); | |
| 180 | framework | |
| 181 | .data | |
| 182 | .state | |
| 183 | .get_mut::<Tasks>() | |
| 184 | .unwrap() | |
| 185 | .priority | |
| 186 | .push(Task::ClearPage); | |
| 187 | } | |
| 188 | } | |
| 189 | KeyAction::ClearHistory => { | |
| 190 | if framework.history.is_empty() { | |
| 191 | *framework.data.global.get_mut::<Message>().unwrap() = | |
| 192 | Message::Error(String::from("This is already the beginning of history")) | |
| 193 | } else { | |
| 194 | framework.clear_history(); | |
| 195 | *framework.data.global.get_mut::<Message>().unwrap() = | |
| 196 | Message::Success(String::from("History cleared!")) | |
| 197 | } | |
| 198 | } | |
| 199 | KeyAction::Select if !framework.is_selected() => { | |
| 200 | let _ = framework.select(); | |
| 201 | } | |
| 202 | _ => render = false, | |
| 203 | } | |
| 204 | if render { | |
| 205 | framework | |
| 206 | .data | |
| 207 | .state | |
| 208 | .get_mut::<Tasks>() | |
| 209 | .unwrap() | |
| 210 | .priority | |
| 211 | .push(Task::RenderAll); | |
| 212 | } | |
| 213 | } | |
| 214 | } | |
src/global/functions/mod.rs +2
| @@ -31,3 +31,5 @@ mod singleitem_load; | ||
| 31 | 31 | pub use singleitem_load::*; |
| 32 | 32 | mod remove_word; |
| 33 | 33 | pub use remove_word::*; |
| 34 | mod key_input; | |
| 35 | pub use key_input::*; | |
src/global/functions/remove_word.rs +29
| @@ -17,3 +17,32 @@ pub fn remove_word(field: &mut TextField) { | ||
| 17 | 17 | field.content = format!("{}{}", before, &field.content[field.cursor..]); |
| 18 | 18 | field.cursor = before.len(); |
| 19 | 19 | } |
| 20 | ||
| 21 | pub fn previous_word(field: &mut TextField) { | |
| 22 | let bytes = field.content.as_bytes(); | |
| 23 | while field.cursor != 0 { | |
| 24 | field.cursor -= 1; | |
| 25 | if bytes[field.cursor].is_ascii_whitespace() { | |
| 26 | return; | |
| 27 | } | |
| 28 | } | |
| 29 | ||
| 30 | field.cursor = 0; | |
| 31 | } | |
| 32 | ||
| 33 | pub fn next_word(field: &mut TextField) { | |
| 34 | let bytes = field.content.as_bytes(); | |
| 35 | for (i, c) in bytes | |
| 36 | .iter() | |
| 37 | .enumerate() | |
| 38 | .take(field.content.len()) | |
| 39 | .skip(field.cursor + 1) | |
| 40 | { | |
| 41 | if c.is_ascii_whitespace() { | |
| 42 | field.cursor = i; | |
| 43 | return; | |
| 44 | } | |
| 45 | } | |
| 46 | ||
| 47 | field.cursor = field.content.len(); | |
| 48 | } | |
src/global/functions/run_command.rs +26 −1
| @@ -1,8 +1,9 @@ | ||
| 1 | 1 | use crate::{ |
| 2 | config::*, | |
| 2 | config::{serde::KeyCodeSerde, *}, | |
| 3 | 3 | global::{functions::*, structs::*, traits::*}, |
| 4 | 4 | load_configs, |
| 5 | 5 | }; |
| 6 | use crossterm::event::{KeyEvent, KeyModifiers}; | |
| 6 | 7 | use home::home_dir; |
| 7 | 8 | use ratatui::{backend::CrosstermBackend, Terminal}; |
| 8 | 9 | use std::{ |
| @@ -475,6 +476,29 @@ pub fn run_single_command( | ||
| 475 | 476 | .priority |
| 476 | 477 | .push(Task::RenderAll); |
| 477 | 478 | } |
| 479 | ["key", keycode, modifier] => { | |
| 480 | let (keycodeserde, modifier) = | |
| 481 | match (|| -> Result<(KeyCodeSerde, u8), Box<dyn Error>> { | |
| 482 | Ok((serde_yaml::from_str(keycode)?, modifier.parse()?)) | |
| 483 | })() { | |
| 484 | Ok(stuff) => stuff, | |
| 485 | Err(e) => { | |
| 486 | *framework.data.global.get_mut::<Message>().unwrap() = | |
| 487 | Message::Error(format!("Cannot parse keycode: `{e}`")); | |
| 488 | return; | |
| 489 | } | |
| 490 | }; | |
| 491 | let keycode = match keycodeserde.to_keycode() { | |
| 492 | Some(code) => code, | |
| 493 | None => { | |
| 494 | *framework.data.global.get_mut::<Message>().unwrap() = | |
| 495 | Message::Error("Unknown keycode".to_string()); | |
| 496 | return; | |
| 497 | } | |
| 498 | }; | |
| 499 | let keymodifier = KeyModifiers::from_bits_truncate(modifier); | |
| 500 | key_input(KeyEvent::new(keycode, keymodifier), framework, terminal) | |
| 501 | } | |
| 478 | 502 | _ => { |
| 479 | 503 | *framework.data.global.get_mut::<Message>().unwrap() = |
| 480 | 504 | Message::Error(format!("Unknown command: `{}`", command.join(" "))); |
| @@ -550,6 +574,7 @@ const HELP_MSG: &str = "\x1b[32mYouTube TUI commands\x1b[0m | ||
| 550 | 574 | \x1b[33mrun [command]\x1b[0m Runs a system command (e.g. `run rm -rf / --no-preserve-root`) |
| 551 | 575 | \x1b[33mparrun [command]\x1b[0m Runs a system command non blocking (e.g. `run firefox example.com`) |
| 552 | 576 | \x1b[33mcopy [text]\x1b[0m Copies text to clipboard |
| 577 | \x1b[33mkey [keycode] [keymodifier]\x1b[0m Create a key input event | |
| 553 | 578 | |
| 554 | 579 | \x1b[91mLIBRARY:\x1b[0m |
| 555 | 580 | \x1b[33mbookmark [id]\x1b[0m Bookmark item with ID (item must be already loaded) |
src/global/structs/keyaction.rs +4
| @@ -34,4 +34,8 @@ pub enum KeyAction { | ||
| 34 | 34 | RemoveWord, |
| 35 | 35 | /// removes all content in textfield |
| 36 | 36 | ClearLine, |
| 37 | /// move cursor to previous word in textfield | |
| 38 | PreviousWord, | |
| 39 | /// move cursor to next word in textfield | |
| 40 | NextWord, | |
| 37 | 41 | } |
src/global/structs/tasks.rs +1
| @@ -141,6 +141,7 @@ impl TaskQueue { | ||
| 141 | 141 | status.provider_updated = true; |
| 142 | 142 | status.render_image = true; |
| 143 | 143 | self.render = RenderTask::All; |
| 144 | run_command(&page_config.command, framework, terminal); | |
| 144 | 145 | } |
| 145 | 146 | |
| 146 | 147 | if self.reload { |
src/items/searchbar.rs +22
| @@ -124,6 +124,28 @@ impl FrameworkItem for SearchBar { | ||
| 124 | 124 | .push(Task::RenderAll); |
| 125 | 125 | return Ok(()); |
| 126 | 126 | } |
| 127 | Some(KeyAction::PreviousWord) => { | |
| 128 | previous_word(&mut self.text_field); | |
| 129 | framework | |
| 130 | .data | |
| 131 | .state | |
| 132 | .get_mut::<Tasks>() | |
| 133 | .unwrap() | |
| 134 | .priority | |
| 135 | .push(Task::RenderAll); | |
| 136 | return Ok(()); | |
| 137 | } | |
| 138 | Some(KeyAction::NextWord) => { | |
| 139 | next_word(&mut self.text_field); | |
| 140 | framework | |
| 141 | .data | |
| 142 | .state | |
| 143 | .get_mut::<Tasks>() | |
| 144 | .unwrap() | |
| 145 | .priority | |
| 146 | .push(Task::RenderAll); | |
| 147 | return Ok(()); | |
| 148 | } | |
| 127 | 149 | Some(KeyAction::ClearLine) => { |
| 128 | 150 | self.text_field.content.clear(); |
| 129 | 151 | self.text_field.scroll = 0; |
src/run.rs +3 −206
| @@ -1,15 +1,12 @@ | ||
| 1 | 1 | use crossterm::event::{self, Event, MouseButton, MouseEventKind}; |
| 2 | 2 | use ratatui::{backend::CrosstermBackend, Terminal}; |
| 3 | 3 | use std::{any::TypeId, error::Error, io::Stdout}; |
| 4 | use tui_additions::{ | |
| 5 | framework::{Framework, FrameworkDirection}, | |
| 6 | widgets::TextField, | |
| 7 | }; | |
| 4 | use tui_additions::framework::Framework; | |
| 8 | 5 | |
| 9 | 6 | use crate::{ |
| 10 | 7 | config::*, |
| 11 | 8 | global::{functions::*, structs::*}, |
| 12 | items::{SearchBar, SearchFilter}, | |
| 9 | items::SearchFilter, | |
| 13 | 10 | }; |
| 14 | 11 | |
| 15 | 12 | /// the main event loop of the program |
| @@ -98,207 +95,7 @@ pub fn run( | ||
| 98 | 95 | .push(Task::RenderAll); |
| 99 | 96 | } |
| 100 | 97 | } |
| 101 | Event::Key(key) => { | |
| 102 | // check if key binds to any commands | |
| 103 | let command_to_run = framework | |
| 104 | .data | |
| 105 | .global | |
| 106 | .get::<CommandBindings>() | |
| 107 | .unwrap() | |
| 108 | .get_command(&key, framework.data.state.get::<Page>().unwrap()); | |
| 109 | if !command_to_run.is_empty() { | |
| 110 | run_command(&apply_envs(command_to_run), framework, terminal); | |
| 111 | framework | |
| 112 | .data | |
| 113 | .state | |
| 114 | .get_mut::<Tasks>() | |
| 115 | .unwrap() | |
| 116 | .priority | |
| 117 | .push(Task::RenderAll); | |
| 118 | } | |
| 119 | ||
| 120 | // 1. get the corresponding action | |
| 121 | // 2. check if action is deselect, if yes, deselect | |
| 122 | // 3. check is anything is selected, if yes, run `.key_event()` with the key | |
| 123 | // 4. if nothing is selected, do stuff like moving the cursor or exiting | |
| 124 | ||
| 125 | let action = framework | |
| 126 | .data | |
| 127 | .global | |
| 128 | .get::<KeyBindingsConfig>() | |
| 129 | .unwrap() | |
| 130 | .get(key); | |
| 131 | ||
| 132 | if action == Some(KeyAction::Deselect) { | |
| 133 | let _ = framework.deselect(); | |
| 134 | framework | |
| 135 | .data | |
| 136 | .state | |
| 137 | .get_mut::<Tasks>() | |
| 138 | .unwrap() | |
| 139 | .priority | |
| 140 | .push(Task::RenderAll); | |
| 141 | framework | |
| 142 | .data | |
| 143 | .global | |
| 144 | .get_mut::<Status>() | |
| 145 | .unwrap() | |
| 146 | .command_capture = None; | |
| 147 | continue; | |
| 148 | } | |
| 149 | ||
| 150 | // the first part check for if the keys should be captured for entering commands | |
| 151 | if framework | |
| 152 | .data | |
| 153 | .global | |
| 154 | .get::<Status>() | |
| 155 | .unwrap() | |
| 156 | .command_capture | |
| 157 | .is_some() | |
| 158 | // the second part runs the command, and see if the returned boolean | |
| 159 | // `is_updated` is true, if so update the screen | |
| 160 | && command_capture(&mut framework.split_clean().0, key) | |
| 161 | { | |
| 162 | framework | |
| 163 | .data | |
| 164 | .state | |
| 165 | .get_mut::<Tasks>() | |
| 166 | .unwrap() | |
| 167 | .priority | |
| 168 | .push(Task::RenderAll); | |
| 169 | continue; | |
| 170 | } | |
| 171 | ||
| 172 | // if something is selected, pass the key input into that item | |
| 173 | // if nothing is selected, the following big chunk of code handles to movement of | |
| 174 | // cursor and stuff | |
| 175 | if framework.is_selected() { | |
| 176 | let (x, y) = framework.cursor.selected(&framework.selectables).unwrap(); | |
| 177 | if let Err(e) = framework.key_input(key) { | |
| 178 | *framework.data.global.get_mut::<Message>().unwrap() = | |
| 179 | Message::Error(e.to_string()); | |
| 180 | }; | |
| 181 | ||
| 182 | let selected = framework.state.get_mut(x, y); | |
| 183 | if framework | |
| 184 | .data | |
| 185 | .global | |
| 186 | .get::<MainConfig>() | |
| 187 | .unwrap() | |
| 188 | .legacy_input_handling | |
| 189 | || (*selected).type_id() == TypeId::of::<SearchBar>() | |
| 190 | { | |
| 191 | continue; | |
| 192 | } | |
| 193 | } | |
| 194 | ||
| 195 | if let Some(action) = action { | |
| 196 | let mut render = true; | |
| 197 | match action { | |
| 198 | KeyAction::StartCommandCapture => { | |
| 199 | framework | |
| 200 | .data | |
| 201 | .global | |
| 202 | .get_mut::<Status>() | |
| 203 | .unwrap() | |
| 204 | .command_capture = Some(TextField::default()); | |
| 205 | } | |
| 206 | KeyAction::Exit => break, | |
| 207 | KeyAction::MoveUp if !framework.is_selected() => { | |
| 208 | framework.r#move(FrameworkDirection::Up)? | |
| 209 | } | |
| 210 | KeyAction::MoveDown if !framework.is_selected() => { | |
| 211 | framework.r#move(FrameworkDirection::Down)? | |
| 212 | } | |
| 213 | KeyAction::MoveLeft if !framework.is_selected() => { | |
| 214 | framework.r#move(FrameworkDirection::Left)? | |
| 215 | } | |
| 216 | KeyAction::MoveRight if !framework.is_selected() => { | |
| 217 | framework.r#move(FrameworkDirection::Right)? | |
| 218 | } | |
| 219 | KeyAction::Reload => framework | |
| 220 | .data | |
| 221 | .state | |
| 222 | .get_mut::<Tasks>() | |
| 223 | .unwrap() | |
| 224 | .priority | |
| 225 | .push(Task::Reload), | |
| 226 | KeyAction::Back => { | |
| 227 | if !framework.history.is_empty() { | |
| 228 | clear_envs( | |
| 229 | &mut framework.data.state.get::<StateEnvs>().unwrap().0.clone(), | |
| 230 | ); | |
| 231 | } | |
| 232 | if framework.revert_last_history().is_err() { | |
| 233 | *framework.data.global.get_mut::<Message>().unwrap() = | |
| 234 | Message::Error(String::from( | |
| 235 | "This is already the beginning of history", | |
| 236 | )) | |
| 237 | } else { | |
| 238 | let status = framework.data.global.get_mut::<Status>().unwrap(); | |
| 239 | status.render_image = true; | |
| 240 | ||
| 241 | framework | |
| 242 | .data | |
| 243 | .state | |
| 244 | .get_mut::<Tasks>() | |
| 245 | .unwrap() | |
| 246 | .priority | |
| 247 | .push(Task::ClearPage); | |
| 248 | let state_envs = | |
| 249 | framework.data.state.get_mut::<StateEnvs>().unwrap(); | |
| 250 | set_envs(state_envs.clone().0.into_iter(), &mut state_envs.0); | |
| 251 | update_provider(&mut framework.data); | |
| 252 | } | |
| 253 | } | |
| 254 | KeyAction::FirstHistory => { | |
| 255 | if framework.revert_history(0).is_err() { | |
| 256 | *framework.data.global.get_mut::<Message>().unwrap() = | |
| 257 | Message::Error(String::from( | |
| 258 | "This is already the beginning of history", | |
| 259 | )) | |
| 260 | } else { | |
| 261 | let status = framework.data.global.get_mut::<Status>().unwrap(); | |
| 262 | status.render_image = true; | |
| 263 | ||
| 264 | update_provider(&mut framework.data); | |
| 265 | framework | |
| 266 | .data | |
| 267 | .state | |
| 268 | .get_mut::<Tasks>() | |
| 269 | .unwrap() | |
| 270 | .priority | |
| 271 | .push(Task::ClearPage); | |
| 272 | } | |
| 273 | } | |
| 274 | KeyAction::ClearHistory => { | |
| 275 | if framework.history.is_empty() { | |
| 276 | *framework.data.global.get_mut::<Message>().unwrap() = | |
| 277 | Message::Error(String::from( | |
| 278 | "This is already the beginning of history", | |
| 279 | )) | |
| 280 | } else { | |
| 281 | framework.clear_history(); | |
| 282 | *framework.data.global.get_mut::<Message>().unwrap() = | |
| 283 | Message::Success(String::from("History cleared!")) | |
| 284 | } | |
| 285 | } | |
| 286 | KeyAction::Select if !framework.is_selected() => { | |
| 287 | let _ = framework.select(); | |
| 288 | } | |
| 289 | _ => render = false, | |
| 290 | } | |
| 291 | if render { | |
| 292 | framework | |
| 293 | .data | |
| 294 | .state | |
| 295 | .get_mut::<Tasks>() | |
| 296 | .unwrap() | |
| 297 | .priority | |
| 298 | .push(Task::RenderAll); | |
| 299 | } | |
| 300 | } | |
| 301 | } | |
| 98 | Event::Key(key) => key_input(key, framework, terminal), | |
| 302 | 99 | // always render if there is a screen resize event |
| 303 | 100 | Event::Resize(_, _) => { |
| 304 | 101 | framework |