Commit d23a77777b
d23a77777b619c6551e979db58019e52fc73dba7
parent: a48c667e95
Unsigned
Siriusmart <71584876+Siriusmart@users.noreply.github.com> · 2023-07-14 10:53 UTC
delete word and clear line
Layout: unified · split
Cargo.lock
+1 −1
| @@ -1520,7 +1520,7 @@ dependencies = [ |
| 1520 | 1520 | |
| 1521 | 1521 | [[package]] |
| 1522 | 1522 | name = "youtube-tui" |
| 1523 | | version = "0.7.3" |
| 1523 | version = "0.7.4" |
| 1524 | 1524 | dependencies = [ |
| 1525 | 1525 | "chrono", |
| 1526 | 1526 | "clipboard", |
Cargo.toml
+1 −1
| @@ -1,6 +1,6 @@ |
| 1 | 1 | [package] |
| 2 | 2 | name = "youtube-tui" |
| 3 | | version = "0.7.3" |
| 3 | version = "0.7.4" |
| 4 | 4 | authors = ["Siriusmart <siriusmart@tuta.io>"] |
| 5 | 5 | edition = "2021" |
| 6 | 6 | description = "An aesthetically pleasing YouTube TUI written in Rust" |
src/config/keybindings.rs
+9 −1
| @@ -111,11 +111,19 @@ impl Default for KeyBindingsConfigSerde { |
| 111 | 111 | KeyCodeSerde::KeyVariants(KeyVariantsSerde::Home), |
| 112 | 112 | HashMap::from([(0, KeyAction::FirstHistory)]), |
| 113 | 113 | ), |
| 114 | | // clipboard |
| 114 | // text editing |
| 115 | 115 | ( |
| 116 | 116 | KeyCodeSerde::Char('v'), |
| 117 | 117 | HashMap::from([(2, KeyAction::Paste)]), |
| 118 | 118 | ), |
| 119 | ( |
| 120 | KeyCodeSerde::Char('w'), |
| 121 | HashMap::from([(2, KeyAction::RemoveWord)]), |
| 122 | ), |
| 123 | ( |
| 124 | KeyCodeSerde::Char('u'), |
| 125 | HashMap::from([(2, KeyAction::ClearLine)]), |
| 126 | ), |
| 119 | 127 | ])) |
| 120 | 128 | } |
| 121 | 129 | } |
src/global/functions/command_capture.rs
+50 −22
| @@ -9,36 +9,64 @@ use tui_additions::framework::FrameworkClean; |
| 9 | 9 | /// handles key input when the user is entering commands |
| 10 | 10 | pub fn command_capture(framework: &mut FrameworkClean, key: KeyEvent) -> bool { |
| 11 | 11 | #[cfg(feature = "clipboard")] |
| 12 | | if framework |
| 12 | match framework |
| 13 | 13 | .data |
| 14 | 14 | .global |
| 15 | 15 | .get::<KeyBindingsConfig>() |
| 16 | 16 | .unwrap() |
| 17 | 17 | .get(key) |
| 18 | | == Some(KeyAction::Paste) |
| 19 | 18 | { |
| 20 | | let textfield = framework |
| 21 | | .data |
| 22 | | .global |
| 23 | | .get_mut::<Status>() |
| 24 | | .unwrap() |
| 25 | | .command_capture |
| 26 | | .as_mut() |
| 27 | | .unwrap(); |
| 28 | | // a textfield must have a width set, because we don't know the width yet just set it to the |
| 29 | | // maximum value possible, so that the cursor will not be moved because of the small value |
| 30 | | textfield.set_width(u16::MAX); |
| 31 | | let content = get_clipboard(); |
| 32 | | if content.is_empty() { |
| 33 | | *framework.data.global.get_mut::<Message>().unwrap() = |
| 34 | | Message::Error(String::from("Clipboard empty")); |
| 19 | Some(KeyAction::Paste) => { |
| 20 | let textfield = framework |
| 21 | .data |
| 22 | .global |
| 23 | .get_mut::<Status>() |
| 24 | .unwrap() |
| 25 | .command_capture |
| 26 | .as_mut() |
| 27 | .unwrap(); |
| 28 | // a textfield must have a width set, because we don't know the width yet just set it to the |
| 29 | // maximum value possible, so that the cursor will not be moved because of the small value |
| 30 | textfield.set_width(u16::MAX); |
| 31 | let content = get_clipboard(); |
| 32 | if content.is_empty() { |
| 33 | *framework.data.global.get_mut::<Message>().unwrap() = |
| 34 | Message::Error(String::from("Clipboard empty")); |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | content.chars().for_each(|c| { |
| 39 | let _ = textfield.push(c); |
| 40 | }); |
| 35 | 41 | return true; |
| 36 | 42 | } |
| 37 | | |
| 38 | | content.chars().for_each(|c| { |
| 39 | | let _ = textfield.push(c); |
| 40 | | }); |
| 41 | | return true; |
| 43 | Some(KeyAction::RemoveWord) => { |
| 44 | remove_word( |
| 45 | framework |
| 46 | .data |
| 47 | .global |
| 48 | .get_mut::<Status>() |
| 49 | .unwrap() |
| 50 | .command_capture |
| 51 | .as_mut() |
| 52 | .unwrap(), |
| 53 | ); |
| 54 | return true; |
| 55 | } |
| 56 | Some(KeyAction::ClearLine) => { |
| 57 | framework |
| 58 | .data |
| 59 | .global |
| 60 | .get_mut::<Status>() |
| 61 | .unwrap() |
| 62 | .command_capture |
| 63 | .as_mut() |
| 64 | .unwrap() |
| 65 | .content |
| 66 | .clear(); |
| 67 | return true; |
| 68 | } |
| 69 | _ => {} |
| 42 | 70 | } |
| 43 | 71 | |
| 44 | 72 | let textfield = framework |
src/global/functions/mod.rs
+2
| @@ -29,3 +29,5 @@ mod init_move; |
| 29 | 29 | pub use init_move::*; |
| 30 | 30 | mod singleitem_load; |
| 31 | 31 | pub use singleitem_load::*; |
| 32 | mod remove_word; |
| 33 | pub use remove_word::*; |
src/global/functions/remove_word.rs
added
+19
| @@ -0,0 +1,19 @@ |
| 1 | use tui_additions::widgets::TextField; |
| 2 | |
| 3 | pub fn remove_word(field: &mut TextField) { |
| 4 | if field.content[0..field.cursor].is_empty() { |
| 5 | return; |
| 6 | } |
| 7 | |
| 8 | let before = field.content[..field.cursor] |
| 9 | .split_whitespace() |
| 10 | .collect::<Vec<_>>(); |
| 11 | let before = if before.is_empty() { |
| 12 | String::new() |
| 13 | } else { |
| 14 | before[0..before.len() - 1].join(" ") |
| 15 | }; |
| 16 | |
| 17 | field.content = format!("{}{}", before, &field.content[field.cursor..]); |
| 18 | field.cursor = before.len(); |
| 19 | } |
src/global/structs/keyaction.rs
+4
| @@ -30,4 +30,8 @@ pub enum KeyAction { |
| 30 | 30 | StartCommandCapture, |
| 31 | 31 | /// paste text |
| 32 | 32 | Paste, |
| 33 | /// remove a word |
| 34 | RemoveWord, |
| 35 | /// removes all content in textfield |
| 36 | ClearLine, |
| 33 | 37 | } |
src/items/searchbar.rs
+47 −21
| @@ -77,20 +77,33 @@ impl FrameworkItem for SearchBar { |
| 77 | 77 | key: crossterm::event::KeyEvent, |
| 78 | 78 | _info: tui_additions::framework::ItemInfo, |
| 79 | 79 | ) -> Result<(), Box<dyn Error>> { |
| 80 | | // pasting clipboard content |
| 81 | | #[cfg(feature = "clipboard")] |
| 82 | | if framework |
| 80 | match framework |
| 83 | 81 | .data |
| 84 | 82 | .global |
| 85 | 83 | .get::<KeyBindingsConfig>() |
| 86 | 84 | .unwrap() |
| 87 | 85 | .get(key) |
| 88 | | == Some(KeyAction::Paste) |
| 89 | 86 | { |
| 90 | | let content = get_clipboard(); |
| 91 | | if content.is_empty() { |
| 92 | | *framework.data.global.get_mut::<Message>().unwrap() = |
| 93 | | Message::Error(String::from("Clipboard empty")); |
| 87 | #[cfg(feature = "clipboard")] |
| 88 | Some(KeyAction::Paste) => { |
| 89 | let content = get_clipboard(); |
| 90 | if content.is_empty() { |
| 91 | *framework.data.global.get_mut::<Message>().unwrap() = |
| 92 | Message::Error(String::from("Clipboard empty")); |
| 93 | framework |
| 94 | .data |
| 95 | .state |
| 96 | .get_mut::<Tasks>() |
| 97 | .unwrap() |
| 98 | .priority |
| 99 | .push(Task::RenderAll); |
| 100 | return Ok(()); |
| 101 | } |
| 102 | |
| 103 | // push all characters at cursor location |
| 104 | content.chars().for_each(|c| { |
| 105 | let _ = self.text_field.push(c); |
| 106 | }); |
| 94 | 107 | framework |
| 95 | 108 | .data |
| 96 | 109 | .state |
| @@ -100,20 +113,33 @@ impl FrameworkItem for SearchBar { |
| 100 | 113 | .push(Task::RenderAll); |
| 101 | 114 | return Ok(()); |
| 102 | 115 | } |
| 103 | | |
| 104 | | // push all characters at cursor location |
| 105 | | content.chars().for_each(|c| { |
| 106 | | let _ = self.text_field.push(c); |
| 107 | | }); |
| 108 | | framework |
| 109 | | .data |
| 110 | | .state |
| 111 | | .get_mut::<Tasks>() |
| 112 | | .unwrap() |
| 113 | | .priority |
| 114 | | .push(Task::RenderAll); |
| 115 | | return Ok(()); |
| 116 | Some(KeyAction::RemoveWord) => { |
| 117 | remove_word(&mut self.text_field); |
| 118 | framework |
| 119 | .data |
| 120 | .state |
| 121 | .get_mut::<Tasks>() |
| 122 | .unwrap() |
| 123 | .priority |
| 124 | .push(Task::RenderAll); |
| 125 | return Ok(()); |
| 126 | } |
| 127 | Some(KeyAction::ClearLine) => { |
| 128 | self.text_field.content.clear(); |
| 129 | self.text_field.scroll = 0; |
| 130 | self.text_field.cursor = 0; |
| 131 | framework |
| 132 | .data |
| 133 | .state |
| 134 | .get_mut::<Tasks>() |
| 135 | .unwrap() |
| 136 | .priority |
| 137 | .push(Task::RenderAll); |
| 138 | return Ok(()); |
| 139 | } |
| 140 | _ => {} |
| 116 | 141 | } |
| 142 | // pasting clipboard content |
| 117 | 143 | |
| 118 | 144 | let updated = match key.code { |
| 119 | 145 | KeyCode::Backspace => self.text_field.remove(self.text_field.cursor).is_ok(), |