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 = [
15201520
15211521[[package]]
15221522name = "youtube-tui"
1523version = "0.7.3"
1523version = "0.7.4"
15241524dependencies = [
15251525 "chrono",
15261526 "clipboard",
Cargo.toml +1 −1
@@ -1,6 +1,6 @@
11[package]
22name = "youtube-tui"
3version = "0.7.3"
3version = "0.7.4"
44authors = ["Siriusmart <siriusmart@tuta.io>"]
55edition = "2021"
66description = "An aesthetically pleasing YouTube TUI written in Rust"
src/config/keybindings.rs +9 −1
@@ -111,11 +111,19 @@ impl Default for KeyBindingsConfigSerde {
111111 KeyCodeSerde::KeyVariants(KeyVariantsSerde::Home),
112112 HashMap::from([(0, KeyAction::FirstHistory)]),
113113 ),
114 // clipboard
114 // text editing
115115 (
116116 KeyCodeSerde::Char('v'),
117117 HashMap::from([(2, KeyAction::Paste)]),
118118 ),
119 (
120 KeyCodeSerde::Char('w'),
121 HashMap::from([(2, KeyAction::RemoveWord)]),
122 ),
123 (
124 KeyCodeSerde::Char('u'),
125 HashMap::from([(2, KeyAction::ClearLine)]),
126 ),
119127 ]))
120128 }
121129}
src/global/functions/command_capture.rs +50 −22
@@ -9,36 +9,64 @@ use tui_additions::framework::FrameworkClean;
99/// handles key input when the user is entering commands
1010pub fn command_capture(framework: &mut FrameworkClean, key: KeyEvent) -> bool {
1111 #[cfg(feature = "clipboard")]
12 if framework
12 match framework
1313 .data
1414 .global
1515 .get::<KeyBindingsConfig>()
1616 .unwrap()
1717 .get(key)
18 == Some(KeyAction::Paste)
1918 {
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 });
3541 return true;
3642 }
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 _ => {}
4270 }
4371
4472 let textfield = framework
src/global/functions/mod.rs +2
@@ -29,3 +29,5 @@ mod init_move;
2929pub use init_move::*;
3030mod singleitem_load;
3131pub 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 {
3030 StartCommandCapture,
3131 /// paste text
3232 Paste,
33 /// remove a word
34 RemoveWord,
35 /// removes all content in textfield
36 ClearLine,
3337}
src/items/searchbar.rs +47 −21
@@ -77,20 +77,33 @@ impl FrameworkItem for SearchBar {
7777 key: crossterm::event::KeyEvent,
7878 _info: tui_additions::framework::ItemInfo,
7979 ) -> Result<(), Box<dyn Error>> {
80 // pasting clipboard content
81 #[cfg(feature = "clipboard")]
82 if framework
80 match framework
8381 .data
8482 .global
8583 .get::<KeyBindingsConfig>()
8684 .unwrap()
8785 .get(key)
88 == Some(KeyAction::Paste)
8986 {
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 });
94107 framework
95108 .data
96109 .state
@@ -100,20 +113,33 @@ impl FrameworkItem for SearchBar {
100113 .push(Task::RenderAll);
101114 return Ok(());
102115 }
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 _ => {}
116141 }
142 // pasting clipboard content
117143
118144 let updated = match key.code {
119145 KeyCode::Backspace => self.text_field.remove(self.text_field.cursor).is_ok(),