datafusion_dft/tui/ui/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

pub mod convert;
pub mod tabs;

use ratatui::{prelude::*, style::palette::tailwind};
use strum::{Display, EnumIter, FromRepr};

use crate::tui::App;

use self::tabs::{context, history, logs, sql};

#[derive(Clone, Copy, Debug, Display, FromRepr, EnumIter)]
pub enum SelectedTab {
    #[allow(clippy::upper_case_acronyms)]
    #[strum(to_string = "SQL")]
    SQL,
    #[cfg(feature = "flightsql")]
    #[strum(to_string = "FlightSQL")]
    FlightSQL,
    #[strum(to_string = "History")]
    History,
    #[strum(to_string = "Logs")]
    Logs,
    #[strum(to_string = "Context")]
    Context,
}

impl SelectedTab {
    pub fn title(self, _app: &App) -> Line<'static> {
        let padding = Span::from("  ");
        match self {
            SelectedTab::SQL => {
                let title = Span::from("SQL (1)").bold();
                Line::from_iter(vec![padding.clone(), title, padding.clone()])
                    .fg(tailwind::SLATE.c200)
                    .bg(self.bg())
            }
            #[cfg(feature = "flightsql")]
            Self::FlightSQL => {
                let status = _app.state.flightsql_tab.connection_status().tab_display();
                let title_text = format!("FlightSQL{status} (2)");
                let title = Span::from(title_text).bold();
                Line::from_iter(vec![padding.clone(), title, padding.clone()])
                    .fg(tailwind::SLATE.c200)
                    .bg(self.bg())
            }
            Self::Logs => {
                #[cfg(feature = "flightsql")]
                let title = Span::from("LOGS (4)").bold();

                #[cfg(not(feature = "flightsql"))]
                let title = Span::from("LOGS (3)").bold();

                Line::from_iter(vec![padding.clone(), title, padding.clone()])
                    .fg(tailwind::SLATE.c200)
                    .bg(self.bg())
            }
            Self::Context => {
                #[cfg(feature = "flightsql")]
                let title = Span::from("CONTEXT (5)").bold();

                #[cfg(not(feature = "flightsql"))]
                let title = Span::from("CONTEXT (4)").bold();

                Line::from_iter(vec![padding.clone(), title, padding.clone()])
                    .fg(tailwind::SLATE.c200)
                    .bg(self.bg())
            }
            Self::History => {
                #[cfg(feature = "flightsql")]
                let title = Span::from("HISTORY (3)").bold();
                #[cfg(not(feature = "flightsql"))]
                let title = Span::from("HISTORY (2)").bold();
                Line::from_iter(vec![padding.clone(), title, padding.clone()])
                    .fg(tailwind::SLATE.c200)
                    .bg(self.bg())
            }
        }
    }

    const fn bg(self) -> Color {
        match self {
            Self::SQL => tailwind::ORANGE.c700,
            Self::Logs => tailwind::ORANGE.c700,
            Self::Context => tailwind::ORANGE.c700,
            Self::History => tailwind::ORANGE.c700,
            #[cfg(feature = "flightsql")]
            Self::FlightSQL => tailwind::ORANGE.c700,
        }
    }

    /// Get the previous tab, if there is no previous tab return the current tab.
    pub fn previous(self) -> Self {
        let current_index: usize = self as usize;
        let previous_index = current_index.saturating_sub(1);
        Self::from_repr(previous_index).unwrap_or(self)
    }

    /// Get the next tab, if there is no next tab return the current tab.
    pub fn next(self) -> Self {
        let current_index = self as usize;
        let next_index = current_index.saturating_add(1);
        Self::from_repr(next_index).unwrap_or(self)
    }

    fn render_sql(self, area: Rect, buf: &mut Buffer, app: &App) {
        sql::render_sql(area, buf, app)
    }

    fn render_logs(self, area: Rect, buf: &mut Buffer, app: &App) {
        logs::render_logs(area, buf, app)
    }

    fn render_context(self, area: Rect, buf: &mut Buffer, app: &App) {
        context::render_context(area, buf, app)
    }

    fn render_history(self, area: Rect, buf: &mut Buffer, app: &App) {
        history::render_history(area, buf, app)
    }

    #[cfg(feature = "flightsql")]
    fn render_flightsql(self, area: Rect, buf: &mut Buffer, app: &App) {
        use self::tabs::flightsql;

        flightsql::render_sql(area, buf, app)
    }

    /// Render the tab with the provided state.
    ///
    /// This used to be an impl of `Widget` but we weren't able to pass state
    /// as a paramter to the render method so moved to impl on the SelectedTab.
    /// It's not clear if this will have future impact.
    pub fn render(self, area: Rect, buf: &mut Buffer, app: &App) {
        match self {
            Self::SQL => self.render_sql(area, buf, app),
            Self::Logs => self.render_logs(area, buf, app),
            Self::Context => self.render_context(area, buf, app),
            Self::History => self.render_history(area, buf, app),
            #[cfg(feature = "flightsql")]
            Self::FlightSQL => self.render_flightsql(area, buf, app),
        }
    }
}