2021-09-17 06:16:31 +00:00
|
|
|
use super::{Component, EventState, StatefulDrawableComponent};
|
2021-07-29 10:47:36 +00:00
|
|
|
use crate::components::command::CommandInfo;
|
2021-07-18 15:50:39 +00:00
|
|
|
use crate::components::{TableComponent, TableFilterComponent};
|
2021-07-31 16:03:39 +00:00
|
|
|
use crate::config::KeyConfig;
|
2021-07-18 15:50:39 +00:00
|
|
|
use crate::event::Key;
|
|
|
|
use anyhow::Result;
|
2021-07-31 16:03:39 +00:00
|
|
|
use database_tree::{Database, Table as DTable};
|
2021-07-18 15:50:39 +00:00
|
|
|
use tui::{
|
|
|
|
backend::Backend,
|
|
|
|
layout::{Constraint, Direction, Layout, Rect},
|
|
|
|
Frame,
|
|
|
|
};
|
|
|
|
|
|
|
|
pub enum Focus {
|
|
|
|
Table,
|
|
|
|
Filter,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RecordTableComponent {
|
|
|
|
pub filter: TableFilterComponent,
|
|
|
|
pub table: TableComponent,
|
|
|
|
pub focus: Focus,
|
2021-07-31 16:03:39 +00:00
|
|
|
key_config: KeyConfig,
|
2021-07-18 15:50:39 +00:00
|
|
|
}
|
|
|
|
|
2021-07-31 16:03:39 +00:00
|
|
|
impl RecordTableComponent {
|
|
|
|
pub fn new(key_config: KeyConfig) -> Self {
|
2021-07-18 15:50:39 +00:00
|
|
|
Self {
|
2021-09-12 06:50:59 +00:00
|
|
|
filter: TableFilterComponent::new(key_config.clone()),
|
2021-07-31 16:03:39 +00:00
|
|
|
table: TableComponent::new(key_config.clone()),
|
2021-07-18 15:50:39 +00:00
|
|
|
focus: Focus::Table,
|
2021-07-31 16:03:39 +00:00
|
|
|
key_config,
|
2021-07-18 15:50:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-31 16:03:39 +00:00
|
|
|
pub fn update(
|
|
|
|
&mut self,
|
|
|
|
rows: Vec<Vec<String>>,
|
|
|
|
headers: Vec<String>,
|
|
|
|
database: Database,
|
|
|
|
table: DTable,
|
|
|
|
) {
|
|
|
|
self.table.update(rows, headers, database, table.clone());
|
|
|
|
self.filter.table = Some(table);
|
2021-07-18 15:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn reset(&mut self) {
|
2021-07-31 16:03:39 +00:00
|
|
|
self.table.reset();
|
|
|
|
self.filter.reset();
|
2021-07-18 15:50:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn filter_focused(&self) -> bool {
|
|
|
|
matches!(self.focus, Focus::Filter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-17 06:16:31 +00:00
|
|
|
impl StatefulDrawableComponent for RecordTableComponent {
|
2021-07-18 15:50:39 +00:00
|
|
|
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, area: Rect, focused: bool) -> Result<()> {
|
|
|
|
let layout = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.constraints(vec![Constraint::Length(3), Constraint::Length(5)])
|
|
|
|
.split(area);
|
|
|
|
|
|
|
|
self.table
|
|
|
|
.draw(f, layout[1], focused && matches!(self.focus, Focus::Table))?;
|
2021-09-12 06:50:59 +00:00
|
|
|
|
|
|
|
self.filter
|
|
|
|
.draw(f, layout[0], focused && matches!(self.focus, Focus::Filter))?;
|
2021-07-18 15:50:39 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for RecordTableComponent {
|
2021-07-31 16:03:39 +00:00
|
|
|
fn commands(&self, out: &mut Vec<CommandInfo>) {
|
|
|
|
self.table.commands(out)
|
|
|
|
}
|
2021-07-29 10:47:36 +00:00
|
|
|
|
2021-07-18 15:50:39 +00:00
|
|
|
fn event(&mut self, key: Key) -> Result<EventState> {
|
2021-07-31 16:03:39 +00:00
|
|
|
if key == self.key_config.filter {
|
|
|
|
self.focus = Focus::Filter;
|
|
|
|
return Ok(EventState::Consumed);
|
|
|
|
}
|
2021-07-18 15:50:39 +00:00
|
|
|
match key {
|
2021-07-25 10:22:31 +00:00
|
|
|
key if matches!(self.focus, Focus::Filter) => return self.filter.event(key),
|
|
|
|
key if matches!(self.focus, Focus::Table) => return self.table.event(key),
|
2021-07-18 15:50:39 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
Ok(EventState::NotConsumed)
|
|
|
|
}
|
|
|
|
}
|