src/global/functions/command_capture.rs
80 lines · 2528 bytes
1use super::get_clipboard;
2use crate::{
3 config::KeyBindingsConfig,
4 global::structs::{KeyAction, Message, Status, Task, Tasks},
5};
6use crossterm::event::{KeyCode, KeyEvent};
7use tui_additions::framework::FrameworkClean;
8
9/// handles key input when the user is entering commands
10pub fn command_capture(framework: &mut FrameworkClean, key: KeyEvent) -> bool {
11 if framework
12 .data
13 .global
14 .get::<KeyBindingsConfig>()
15 .unwrap()
16 .get(key)
17 == Some(KeyAction::Paste)
18 {
19 let textfield = framework
20 .data
21 .global
22 .get_mut::<Status>()
23 .unwrap()
24 .command_capture
25 .as_mut()
26 .unwrap();
27 // a textfield must have a width set, because we don't know the width yet just set it to the
28 // maximum value possible, so that the cursor will not be moved because of the small value
29 textfield.set_width(u16::MAX);
30 let content = get_clipboard();
31 if content.is_empty() {
32 *framework.data.global.get_mut::<Message>().unwrap() =
33 Message::Error(String::from("Clipboard empty"));
34 return true;
35 }
36
37 content.chars().for_each(|c| {
38 let _ = textfield.push(c);
39 });
40 return true;
41 }
42
43 let textfield = framework
44 .data
45 .global
46 .get_mut::<Status>()
47 .unwrap()
48 .command_capture
49 .as_mut()
50 .unwrap();
51 // a textfield must have a width set, because we don't know the width yet just set it to the
52 // maximum value possible, so that the cursor will not be moved because of the small value
53 textfield.set_width(u16::MAX);
54
55 match key.code {
56 KeyCode::Char(c) => textfield.push(c).is_ok(),
57 KeyCode::Up => textfield.first().is_ok(),
58 KeyCode::Down => textfield.last().is_ok(),
59 KeyCode::Left => textfield.left().is_ok(),
60 KeyCode::Right => textfield.right().is_ok(),
61 KeyCode::Backspace => textfield.pop().is_ok(),
62 KeyCode::Enter => {
63 framework
64 .data
65 .state
66 .get_mut::<Tasks>()
67 .unwrap()
68 .priority
69 .push(Task::Command(textfield.content.clone()));
70 framework
71 .data
72 .global
73 .get_mut::<Status>()
74 .unwrap()
75 .command_capture = None;
76 true
77 }
78 _ => false,
79 }
80}