src/global/functions/command_capture.rs

84c4617332578aaf4e9ee68a0a2d7d6258e48243
youtube-tui/src/global/functions/command_capture.rs history · blame · raw

209 lines · 7576 bytes

1 symbol in this file
  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                    text_field.cursor = text_field.cursor.min(text_field.content.len());
141                    if !text_field.content.is_empty() {
142                        let _ = text_field.last();
143                    }
144                } else {
145                    let new_index = status.command_history_index.unwrap_or(history.0.len()) + 1;
146                    let new_content = history.0[new_index].clone();
147
148                    let status = framework.data.global.get_mut::<Status>().unwrap();
149                    let text_field = status.command_capture.as_mut().unwrap();
150                    text_field.content = new_content;
151                    status.command_history_index = Some(new_index);
152                    let _ = text_field.last();
153                }
154                return true;
155            }
156        }
157        _ => {}
158    }
159
160    let status = framework.data.global.get_mut::<Status>().unwrap();
161    let textfield = status.command_capture.as_mut().unwrap();
162    // a textfield must have a width set, because we don't know the width yet just set it to the
163    // maximum value possible, so that the cursor will not be moved because of the small value
164    textfield.set_width(u16::MAX);
165
166    match key.code {
167        KeyCode::Char(c) => textfield.push(c).is_ok(),
168        KeyCode::Up => textfield.first().is_ok(),
169        KeyCode::Down => textfield.last().is_ok(),
170        KeyCode::Left => textfield.left().is_ok(),
171        KeyCode::Right => textfield.right().is_ok(),
172        KeyCode::Backspace => textfield.pop().is_ok(),
173        KeyCode::Enter => {
174            let content = textfield.content.clone();
175            framework
176                .data
177                .global
178                .get_mut::<CommandHistory>()
179                .unwrap()
180                .push(content);
181            framework
182                .data
183                .state
184                .get_mut::<Tasks>()
185                .unwrap()
186                .priority
187                .push(Task::Command(apply_envs(
188                    framework
189                        .data
190                        .global
191                        .get_mut::<Status>()
192                        .unwrap()
193                        .command_capture
194                        .as_mut()
195                        .unwrap()
196                        .content
197                        .clone(),
198                )));
199            framework
200                .data
201                .global
202                .get_mut::<Status>()
203                .unwrap()
204                .command_capture = None;
205            true
206        }
207        _ => false,
208    }
209}