use database/table as a table component name

pull/33/head
Takayuki Maeda 3 years ago
parent 37e39711d9
commit 8d6309c08e

@ -1,8 +1,8 @@
use crate::Table;
use crate::{
databasetreeitems::DatabaseTreeItems, error::Result, item::DatabaseTreeItemKind,
tree_iter::TreeIterator,
};
use crate::{Database, Table};
use std::{collections::BTreeSet, usize};
///
@ -76,13 +76,13 @@ impl DatabaseTree {
.and_then(|index| self.items.tree_items.get(index))
}
pub fn selected_table(&self) -> Option<(Table, String)> {
pub fn selected_table(&self) -> Option<(Database, Table)> {
self.selection.and_then(|index| {
let item = &self.items.tree_items[index];
match item.kind() {
DatabaseTreeItemKind::Database { .. } => None,
DatabaseTreeItemKind::Table { table, database } => {
Some((table.clone(), database.clone()))
Some((database.clone(), table.clone()))
}
}
})

@ -34,7 +34,7 @@ impl TreeItemInfo {
#[derive(PartialEq, Debug, Clone)]
pub enum DatabaseTreeItemKind {
Database { name: String, collapsed: bool },
Table { database: String, table: Table },
Table { database: Database, table: Table },
}
impl DatabaseTreeItemKind {
@ -63,7 +63,7 @@ impl DatabaseTreeItemKind {
pub fn database_name(&self) -> Option<String> {
match self {
Self::Database { .. } => None,
Self::Table { database, .. } => Some(database.clone()),
Self::Table { database, .. } => Some(database.name.clone()),
}
}
}
@ -82,7 +82,7 @@ impl DatabaseTreeItem {
Ok(Self {
info: TreeItemInfo::new(indent, false),
kind: DatabaseTreeItemKind::Table {
database: database.name.clone(),
database: database.clone(),
table: table.clone(),
},
})

@ -11,7 +11,7 @@ pub use crate::{
item::{DatabaseTreeItem, TreeItemInfo},
};
#[derive(Clone)]
#[derive(Clone, PartialEq, Debug)]
pub struct Database {
pub name: String,
pub tables: Vec<Table>,

@ -170,7 +170,8 @@ impl App {
None => self.pool.as_ref().unwrap().get_databases().await?,
};
self.databases.update(databases.as_slice()).unwrap();
self.focus = Focus::DabataseList
self.focus = Focus::DabataseList;
self.record_table.reset();
}
return Ok(EventState::Consumed);
}
@ -181,24 +182,29 @@ impl App {
}
if key == self.config.key_config.enter && self.databases.tree_focused() {
if let Some((table, database)) = self.databases.tree().selected_table() {
if let Some((database, table)) = self.databases.tree().selected_table() {
self.focus = Focus::Table;
let (headers, records) = self
.pool
.as_ref()
.unwrap()
.get_records(&database, &table.name, 0, None)
.get_records(&database.name, &table.name, 0, None)
.await?;
self.record_table.update(records, headers);
self.record_table.set_table(table.name.to_string());
self.record_table
.update(records, headers, database.clone(), table.clone());
let (headers, records) = self
.pool
.as_ref()
.unwrap()
.get_columns(&database, &table.name)
.get_columns(&database.name, &table.name)
.await?;
self.structure_table.update(records, headers);
self.structure_table.update(
records,
headers,
database.clone(),
table.clone(),
);
self.table_status
.update(self.record_table.len() as u64, table);
}
@ -221,14 +227,14 @@ impl App {
if key == self.config.key_config.enter && self.record_table.filter_focused()
{
self.record_table.focus = crate::components::record_table::Focus::Table;
if let Some((table, database)) = self.databases.tree().selected_table()
if let Some((database, table)) = self.databases.tree().selected_table()
{
let (headers, records) = self
.pool
.as_ref()
.unwrap()
.get_records(
&database.clone(),
&database.name.clone(),
&table.name,
0,
if self.record_table.filter.input.is_empty() {
@ -238,7 +244,7 @@ impl App {
},
)
.await?;
self.record_table.update(records, headers);
self.record_table.update(records, headers, database, table);
}
}
@ -251,7 +257,7 @@ impl App {
% crate::utils::RECORDS_LIMIT_PER_PAGE as usize
== 0
{
if let Some((table, database)) =
if let Some((database, table)) =
self.databases.tree().selected_table()
{
let (_, records) = self
@ -259,7 +265,7 @@ impl App {
.as_ref()
.unwrap()
.get_records(
&database.clone(),
&database.name.clone(),
&table.name,
index as u16,
if self.record_table.filter.input.is_empty() {

@ -4,6 +4,7 @@ use crate::components::{TableComponent, TableFilterComponent};
use crate::config::KeyConfig;
use crate::event::Key;
use anyhow::Result;
use database_tree::{Database, Table as DTable};
use tui::{
backend::Backend,
layout::{Constraint, Direction, Layout, Rect},
@ -32,16 +33,24 @@ impl RecordTableComponent {
}
}
pub fn update(&mut self, rows: Vec<Vec<String>>, headers: Vec<String>) {
self.table.update(rows, headers)
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);
}
pub fn len(&self) -> usize {
self.table.rows.len()
pub fn reset(&mut self) {
self.table.reset();
self.filter.reset();
}
pub fn set_table(&mut self, table: String) {
self.filter.table = Some(table)
pub fn len(&self) -> usize {
self.table.rows.len()
}
pub fn filter_focused(&self) -> bool {

@ -6,6 +6,7 @@ use crate::components::command::{self, CommandInfo};
use crate::config::KeyConfig;
use crate::event::Key;
use anyhow::Result;
use database_tree::{Database, Table as DTable};
use std::convert::From;
use tui::{
backend::Backend,
@ -21,6 +22,7 @@ pub struct TableComponent {
pub rows: Vec<Vec<String>>,
pub eod: bool,
pub selected_row: TableState,
table: Option<(Database, DTable)>,
selected_column: usize,
selection_area_corner: Option<(usize, usize)>,
column_page_start: std::cell::Cell<usize>,
@ -34,6 +36,7 @@ impl TableComponent {
selected_row: TableState::default(),
headers: vec![],
rows: vec![],
table: None,
selected_column: 0,
selection_area_corner: None,
column_page_start: std::cell::Cell::new(0),
@ -43,7 +46,19 @@ impl TableComponent {
}
}
pub fn update(&mut self, rows: Vec<Vec<String>>, headers: Vec<String>) {
fn title(&self) -> String {
self.table.as_ref().map_or(" - ".to_string(), |table| {
format!("{}/{}", table.0.name, table.1.name)
})
}
pub fn update(
&mut self,
rows: Vec<Vec<String>>,
headers: Vec<String>,
database: Database,
table: DTable,
) {
if !rows.is_empty() {
self.selected_row.select(None);
self.selected_row.select(Some(0))
@ -55,9 +70,22 @@ impl TableComponent {
self.column_page_start = std::cell::Cell::new(0);
self.scroll = VerticalScroll::new();
self.eod = false;
self.table = Some((database, table));
}
pub fn reset(&mut self) {
self.selected_row.select(None);
self.headers = Vec::new();
self.rows = Vec::new();
self.selected_column = 0;
self.selection_area_corner = None;
self.column_page_start = std::cell::Cell::new(0);
self.scroll = VerticalScroll::new();
self.eod = false;
self.table = None;
}
fn reset(&mut self) {
fn reset_selection(&mut self) {
self.selection_area_corner = None;
}
@ -76,7 +104,7 @@ impl TableComponent {
}
None => None,
};
self.reset();
self.reset_selection();
self.selected_row.select(i);
}
@ -91,7 +119,7 @@ impl TableComponent {
}
None => None,
};
self.reset();
self.reset_selection();
self.selected_row.select(i);
}
@ -99,7 +127,7 @@ impl TableComponent {
if self.rows.is_empty() {
return;
}
self.reset();
self.reset_selection();
self.selected_row.select(Some(0));
}
@ -107,7 +135,7 @@ impl TableComponent {
if self.rows.is_empty() {
return;
}
self.reset();
self.reset_selection();
self.selected_row.select(Some(self.rows.len() - 1));
}
@ -118,7 +146,7 @@ impl TableComponent {
if self.selected_column >= self.headers.len().saturating_sub(1) {
return;
}
self.reset();
self.reset_selection();
self.selected_column += 1;
}
@ -129,7 +157,7 @@ impl TableComponent {
if self.selected_column == 0 {
return;
}
self.reset();
self.reset_selection();
self.selected_column -= 1;
}
@ -390,7 +418,7 @@ impl DrawableComponent for TableComponent {
TableValueComponent::new(self.selected_cells().unwrap_or_default())
.draw(f, layout[0], focused)?;
let block = Block::default().borders(Borders::ALL).title("Records");
let block = Block::default().borders(Borders::ALL).title(self.title());
let (selected_column_index, headers, rows, constraints) =
self.calculate_cell_widths(block.inner(layout[1]).width);
let header_cells = headers.iter().enumerate().map(|(column_index, h)| {

@ -2,6 +2,7 @@ use super::{compute_character_width, Component, DrawableComponent, EventState};
use crate::components::command::CommandInfo;
use crate::event::Key;
use anyhow::Result;
use database_tree::Table;
use tui::{
backend::Backend,
layout::Rect,
@ -13,7 +14,7 @@ use tui::{
use unicode_width::UnicodeWidthStr;
pub struct TableFilterComponent {
pub table: Option<String>,
pub table: Option<Table>,
pub input: Vec<char>,
input_idx: usize,
input_cursor_position: u16,
@ -34,6 +35,13 @@ impl TableFilterComponent {
pub fn input_str(&self) -> String {
self.input.iter().collect()
}
pub fn reset(&mut self) {
self.table = None;
self.input = Vec::new();
self.input_idx = 0;
self.input_cursor_position = 0;
}
}
impl DrawableComponent for TableFilterComponent {
@ -42,7 +50,7 @@ impl DrawableComponent for TableFilterComponent {
Span::styled(
self.table
.as_ref()
.map_or("-".to_string(), |table| table.to_string()),
.map_or("-".to_string(), |table| table.name.to_string()),
Style::default().fg(Color::Blue),
),
Span::from(format!(
@ -67,7 +75,7 @@ impl DrawableComponent for TableFilterComponent {
+ (1 + self
.table
.as_ref()
.map_or(String::new(), |table| table.to_string())
.map_or(String::new(), |table| table.name.to_string())
.width()
+ 1) as u16)
.saturating_add(self.input_cursor_position),

Loading…
Cancel
Save