src/global/functions/command_capture.rs
81 lines · 2538 bytes
1use crate::global::{functions::*, structs::*};
2
3#[cfg(feature = "clipboard")]
4use crate::config::*;
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 #[cfg(feature = "clipboard")]
12 if framework
13 .data
14 .global
15 .get::<KeyBindingsConfig>()
16 .unwrap()
17 .get(key)
18 == Some(KeyAction::Paste)
19 {
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 });
41 return true;
42 }
43
44 let textfield = framework
45 .data
46 .global
47 .get_mut::<Status>()
48 .unwrap()
49 .command_capture
50 .as_mut()
51 .unwrap();
52 // a textfield must have a width set, because we don't know the width yet just set it to the
53 // maximum value possible, so that the cursor will not be moved because of the small value
54 textfield.set_width(u16::MAX);
55
56 match key.code {
57 KeyCode::Char(c) => textfield.push(c).is_ok(),
58 KeyCode::Up => textfield.first().is_ok(),
59 KeyCode::Down => textfield.last().is_ok(),
60 KeyCode::Left => textfield.left().is_ok(),
61 KeyCode::Right => textfield.right().is_ok(),
62 KeyCode::Backspace => textfield.pop().is_ok(),
63 KeyCode::Enter => {
64 framework
65 .data
66 .state
67 .get_mut::<Tasks>()
68 .unwrap()
69 .priority
70 .push(Task::Command(apply_envs(textfield.content.clone())));
71 framework
72 .data
73 .global
74 .get_mut::<Status>()
75 .unwrap()
76 .command_capture = None;
77 true
78 }
79 _ => false,
80 }
81}