src/global/functions/command_capture.rs

149 lines · 4480 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    match framework
 13        .data
 14        .global
 15        .get::<KeyBindingsConfig>()
 16        .unwrap()
 17        .get(key)
 18    {
 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            });
 41            return true;
 42        }
 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        Some(KeyAction::PreviousWord) => {
 70            previous_word(
 71                framework
 72                    .data
 73                    .global
 74                    .get_mut::<Status>()
 75                    .unwrap()
 76                    .command_capture
 77                    .as_mut()
 78                    .unwrap(),
 79            );
 80            framework
 81                .data
 82                .state
 83                .get_mut::<Tasks>()
 84                .unwrap()
 85                .priority
 86                .push(Task::RenderAll);
 87            return true;
 88        }
 89        Some(KeyAction::NextWord) => {
 90            next_word(
 91                framework
 92                    .data
 93                    .global
 94                    .get_mut::<Status>()
 95                    .unwrap()
 96                    .command_capture
 97                    .as_mut()
 98                    .unwrap(),
 99            );
100            framework
101                .data
102                .state
103                .get_mut::<Tasks>()
104                .unwrap()
105                .priority
106                .push(Task::RenderAll);
107            return true;
108        }
109        _ => {}
110    }
111
112    let textfield = framework
113        .data
114        .global
115        .get_mut::<Status>()
116        .unwrap()
117        .command_capture
118        .as_mut()
119        .unwrap();
120    // a textfield must have a width set, because we don't know the width yet just set it to the
121    // maximum value possible, so that the cursor will not be moved because of the small value
122    textfield.set_width(u16::MAX);
123
124    match key.code {
125        KeyCode::Char(c) => textfield.push(c).is_ok(),
126        KeyCode::Up => textfield.first().is_ok(),
127        KeyCode::Down => textfield.last().is_ok(),
128        KeyCode::Left => textfield.left().is_ok(),
129        KeyCode::Right => textfield.right().is_ok(),
130        KeyCode::Backspace => textfield.pop().is_ok(),
131        KeyCode::Enter => {
132            framework
133                .data
134                .state
135                .get_mut::<Tasks>()
136                .unwrap()
137                .priority
138                .push(Task::Command(apply_envs(textfield.content.clone())));
139            framework
140                .data
141                .global
142                .get_mut::<Status>()
143                .unwrap()
144                .command_capture = None;
145            true
146        }
147        _ => false,
148    }
149}