datafusion_tui/app/handlers/
normal.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::app::core::{App, AppReturn, InputMode, TabItem};
19use crate::app::error::Result;
20use crate::app::handlers::execute_query;
21use crate::events::Key;
22use log::debug;
23
24pub enum NormalModeAction {
25    Continue,
26    Exit,
27}
28
29pub async fn normal_mode_handler(app: &mut App, key: Key) -> Result<AppReturn> {
30    if app.tab_item == TabItem::Editor {
31        match key {
32            Key::Enter => execute_query(app).await,
33            Key::Char('c') => {
34                app.editor.input.clear()?;
35                app.input_mode = InputMode::Editing;
36                Ok(AppReturn::Continue)
37            }
38            Key::Char('e') => {
39                app.input_mode = InputMode::Editing;
40                Ok(AppReturn::Continue)
41            }
42            Key::Char('q') => Ok(AppReturn::Exit),
43            Key::Char('r') => {
44                app.input_mode = InputMode::Rc;
45                Ok(AppReturn::Continue)
46            }
47            Key::Char(c) => {
48                if c.is_ascii_digit() {
49                    change_tab(c, app)
50                } else {
51                    Ok(AppReturn::Continue)
52                }
53            }
54            Key::Down => {
55                if let Some(results) = &mut app.query_results {
56                    results.scroll.x += 1
57                }
58                Ok(AppReturn::Continue)
59            }
60            Key::PageDown => {
61                if let Some(results) = &mut app.query_results {
62                    results.scroll.x += 10
63                }
64                Ok(AppReturn::Continue)
65            }
66            Key::Up => {
67                if let Some(results) = &mut app.query_results {
68                    let new_x = match results.scroll.x {
69                        0 => 0,
70                        n => n - 1,
71                    };
72                    results.scroll.x = new_x
73                }
74                Ok(AppReturn::Continue)
75            }
76            Key::PageUp => {
77                if let Some(results) = &mut app.query_results {
78                    let new_x = match results.scroll.x {
79                        0 => 0,
80                        n => n - 10,
81                    };
82                    results.scroll.x = new_x
83                }
84                Ok(AppReturn::Continue)
85            }
86
87            Key::Right => {
88                if let Some(results) = &mut app.query_results {
89                    results.scroll.y += 3
90                }
91                Ok(AppReturn::Continue)
92            }
93            Key::Left => {
94                if let Some(results) = &mut app.query_results {
95                    let new_y = match results.scroll.y {
96                        0 | 1 | 2 => 0,
97                        n => n - 3,
98                    };
99                    results.scroll.y = new_y
100                }
101                Ok(AppReturn::Continue)
102            }
103            _ => Ok(AppReturn::Continue),
104        }
105    } else {
106        match key {
107            Key::Char('q') => Ok(AppReturn::Exit),
108            Key::Char(c) if c.is_ascii_digit() => change_tab(c, app),
109            _ => Ok(AppReturn::Continue),
110        }
111    }
112}
113
114fn change_tab(c: char, app: &mut App) -> Result<AppReturn> {
115    match TabItem::try_from(c) {
116        Ok(tab_item) => {
117            app.tab_item = tab_item;
118        }
119        Err(e) => {
120            debug!("{}", e);
121        }
122    };
123    Ok(AppReturn::Continue)
124}