datafusion_tui/
lib.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
18pub mod app;
19pub mod cli;
20pub mod events;
21pub mod utils;
22
23use std::io;
24use std::time::Duration;
25
26use app::core::{App, AppReturn};
27use crossterm::{
28    event::{DisableMouseCapture, EnableMouseCapture},
29    execute,
30    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
31};
32use tui::{backend::CrosstermBackend, Terminal};
33
34use crate::app::error::Result;
35use crate::app::ui;
36
37use crate::events::{Event, Events};
38
39pub async fn run_app(app: &mut App) -> Result<()> {
40    enable_raw_mode().unwrap();
41    let mut stdout = io::stdout();
42    execute!(stdout, EnterAlternateScreen, EnableMouseCapture).unwrap();
43    let backend = CrosstermBackend::new(stdout);
44    let mut terminal = Terminal::new(backend).unwrap();
45
46    let tick_rate = Duration::from_millis(200);
47    let events = Events::new(tick_rate);
48
49    loop {
50        terminal.draw(|f| ui::draw_ui(f, app))?;
51
52        let event = events.next().unwrap();
53
54        let result = match event {
55            Event::KeyInput(key) => app.key_handler(key).await,
56            Event::Tick => app.update_on_tick(),
57        };
58
59        if result == AppReturn::Exit {
60            break;
61        }
62    }
63
64    disable_raw_mode()?;
65    execute!(
66        terminal.backend_mut(),
67        LeaveAlternateScreen,
68        DisableMouseCapture
69    )?;
70    terminal.show_cursor()?;
71    Ok(())
72}