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[[package]] 1521[[package]]
1522name = "youtube-tui" 1522name = "youtube-tui"
1523version = "0.7.3" 1523version = "0.7.4"
1524dependencies = [ 1524dependencies = [
1525 "chrono", 1525 "chrono",
1526 "clipboard", 1526 "clipboard",
Cargo.toml +1 −1
@@ -1,6 +1,6 @@
1[package] 1[package]
2name = "youtube-tui" 2name = "youtube-tui"
3version = "0.7.3" 3version = "0.7.4"
4authors = ["Siriusmart <siriusmart@tuta.io>"] 4authors = ["Siriusmart <siriusmart@tuta.io>"]
5edition = "2021" 5edition = "2021"
6description = "An aesthetically pleasing YouTube TUI written in Rust" 6description = "An aesthetically pleasing YouTube TUI written in Rust"
src/config/keybindings.rs +9 −1
@@ -111,11 +111,19 @@ impl Default for KeyBindingsConfigSerde {
111 KeyCodeSerde::KeyVariants(KeyVariantsSerde::Home), 111 KeyCodeSerde::KeyVariants(KeyVariantsSerde::Home),
112 HashMap::from([(0, KeyAction::FirstHistory)]), 112 HashMap::from([(0, KeyAction::FirstHistory)]),
113 ), 113 ),
114 // clipboard 114 // text editing
115 ( 115 (
116 KeyCodeSerde::Char('v'), 116 KeyCodeSerde::Char('v'),
117 HashMap::from([(2, KeyAction::Paste)]), 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/// handles key input when the user is entering commands 9/// handles key input when the user is entering commands
10pub fn command_capture(framework: &mut FrameworkClean, key: KeyEvent) -> bool { 10pub fn command_capture(framework: &mut FrameworkClean, key: KeyEvent) -> bool {
11 #[cfg(feature = "clipboard")] 11 #[cfg(feature = "clipboard")]
12 if framework 12 match framework
13 .data 13 .data
14 .global 14 .global
15 .get::<KeyBindingsConfig>() 15 .get::<KeyBindingsConfig>()
16 .unwrap() 16 .unwrap()
17 .get(key) 17 .get(key)
18 == Some(KeyAction::Paste)
19 { 18 {
20 let textfield = framework 19 Some(KeyAction::Paste) => {
21 .data 20 let textfield = framework
22 .global 21 .data
23 .get_mut::<Status>() 22 .global
24 .unwrap() 23 .get_mut::<Status>()
25 .command_capture 24 .unwrap()
26 .as_mut() 25 .command_capture
27 .unwrap(); 26 .as_mut()
28 // a textfield must have a width set, because we don't know the width yet just set it to the 27 .unwrap();
29 // maximum value possible, so that the cursor will not be moved because of the small value 28 // a textfield must have a width set, because we don't know the width yet just set it to the
30 textfield.set_width(u16::MAX); 29 // maximum value possible, so that the cursor will not be moved because of the small value
31 let content = get_clipboard(); 30 textfield.set_width(u16::MAX);
32 if content.is_empty() { 31 let content = get_clipboard();
33 *framework.data.global.get_mut::<Message>().unwrap() = 32 if content.is_empty() {
34 Message::Error(String::from("Clipboard 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 return true; 41 return true;
36 } 42 }
37 43 Some(KeyAction::RemoveWord) => {
38 content.chars().for_each(|c| { 44 remove_word(
39 let _ = textfield.push(c); 45 framework
40 }); 46 .data
41 return true; 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 let textfield = framework 72 let textfield = framework
src/global/functions/mod.rs +2
@@ -29,3 +29,5 @@ mod init_move;
29pub use init_move::*; 29pub use init_move::*;
30mod singleitem_load; 30mod singleitem_load;
31pub use singleitem_load::*; 31pub use singleitem_load::*;
32mod remove_word;
33pub use remove_word::*;
src/global/functions/remove_word.rs added +19
@@ -0,0 +1,19 @@
1use tui_additions::widgets::TextField;
2
3pub 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 StartCommandCapture, 30 StartCommandCapture,
31 /// paste text 31 /// paste text
32 Paste, 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 key: crossterm::event::KeyEvent, 77 key: crossterm::event::KeyEvent,
78 _info: tui_additions::framework::ItemInfo, 78 _info: tui_additions::framework::ItemInfo,
79 ) -> Result<(), Box<dyn Error>> { 79 ) -> Result<(), Box<dyn Error>> {
80 // pasting clipboard content 80 match framework
81 #[cfg(feature = "clipboard")]
82 if framework
83 .data 81 .data
84 .global 82 .global
85 .get::<KeyBindingsConfig>() 83 .get::<KeyBindingsConfig>()
86 .unwrap() 84 .unwrap()
87 .get(key) 85 .get(key)
88 == Some(KeyAction::Paste)
89 { 86 {
90 let content = get_clipboard(); 87 #[cfg(feature = "clipboard")]
91 if content.is_empty() { 88 Some(KeyAction::Paste) => {
92 *framework.data.global.get_mut::<Message>().unwrap() = 89 let content = get_clipboard();
93 Message::Error(String::from("Clipboard empty")); 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 framework 107 framework
95 .data 108 .data
96 .state 109 .state
@@ -100,20 +113,33 @@ impl FrameworkItem for SearchBar {
100 .push(Task::RenderAll); 113 .push(Task::RenderAll);
101 return Ok(()); 114 return Ok(());
102 } 115 }
103 116 Some(KeyAction::RemoveWord) => {
104 // push all characters at cursor location 117 remove_word(&mut self.text_field);
105 content.chars().for_each(|c| { 118 framework
106 let _ = self.text_field.push(c); 119 .data
107 }); 120 .state
108 framework 121 .get_mut::<Tasks>()
109 .data 122 .unwrap()
110 .state 123 .priority
111 .get_mut::<Tasks>() 124 .push(Task::RenderAll);
112 .unwrap() 125 return Ok(());
113 .priority 126 }
114 .push(Task::RenderAll); 127 Some(KeyAction::ClearLine) => {
115 return Ok(()); 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 let updated = match key.code { 144 let updated = match key.code {
119 KeyCode::Backspace => self.text_field.remove(self.text_field.cursor).is_ok(), 145 KeyCode::Backspace => self.text_field.remove(self.text_field.cursor).is_ok(),