src/global/functions/command_capture.rs

205 lines · 7358 bytes

  1use crate::global::{functions::*, structs::*, traits::CollectionNoId};
  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        // warning: really shitty code, it works, dont touch
110        Some(KeyAction::PreviousEntry) => {
111            let status = framework.data.global.get::<Status>().unwrap();
112            let history = framework.data.global.get::<CommandHistory>().unwrap();
113            if status.command_history_index != Some(0) && !history.0.is_empty() {
114                let new_index = status.command_history_index.unwrap_or(history.0.len()) - 1;
115                let new_content = history.0[new_index].clone();
116
117                let status = framework.data.global.get_mut::<Status>().unwrap();
118                let text_field = status.command_capture.as_mut().unwrap();
119                if status.command_history_index.is_none() {
120                    status.command_editing_cache = text_field.content.clone();
121                }
122                text_field.content = new_content;
123                status.command_history_index = Some(new_index);
124                let _ = text_field.last();
125                return true;
126            }
127            return false;
128        }
129        Some(KeyAction::NextEntry) => {
130            let status = framework.data.global.get::<Status>().unwrap();
131            if status.command_history_index.is_none() {
132                return false;
133            } else {
134                let history = framework.data.global.get::<CommandHistory>().unwrap();
135                if status.command_history_index == Some(history.0.len() - 1) {
136                    let status = framework.data.global.get_mut::<Status>().unwrap();
137                    let text_field = status.command_capture.as_mut().unwrap();
138                    status.command_history_index = None;
139                    text_field.content = status.command_editing_cache.clone();
140                } else {
141                    let new_index = status.command_history_index.unwrap_or(history.0.len()) + 1;
142                    let new_content = history.0[new_index].clone();
143
144                    let status = framework.data.global.get_mut::<Status>().unwrap();
145                    let text_field = status.command_capture.as_mut().unwrap();
146                    text_field.content = new_content;
147                    status.command_history_index = Some(new_index);
148                    let _ = text_field.last();
149                }
150                return true;
151            }
152        }
153        _ => {}
154    }
155
156    let status = framework.data.global.get_mut::<Status>().unwrap();
157    let textfield = status.command_capture.as_mut().unwrap();
158    // a textfield must have a width set, because we don't know the width yet just set it to the
159    // maximum value possible, so that the cursor will not be moved because of the small value
160    textfield.set_width(u16::MAX);
161
162    match key.code {
163        KeyCode::Char(c) => textfield.push(c).is_ok(),
164        KeyCode::Up => textfield.first().is_ok(),
165        KeyCode::Down => textfield.last().is_ok(),
166        KeyCode::Left => textfield.left().is_ok(),
167        KeyCode::Right => textfield.right().is_ok(),
168        KeyCode::Backspace => textfield.pop().is_ok(),
169        KeyCode::Enter => {
170            let content = textfield.content.clone();
171            framework
172                .data
173                .global
174                .get_mut::<CommandHistory>()
175                .unwrap()
176                .push(content);
177            framework
178                .data
179                .state
180                .get_mut::<Tasks>()
181                .unwrap()
182                .priority
183                .push(Task::Command(apply_envs(
184                    framework
185                        .data
186                        .global
187                        .get_mut::<Status>()
188                        .unwrap()
189                        .command_capture
190                        .as_mut()
191                        .unwrap()
192                        .content
193                        .clone(),
194                )));
195            framework
196                .data
197                .global
198                .get_mut::<Status>()
199                .unwrap()
200                .command_capture = None;
201            true
202        }
203        _ => false,
204    }
205}