mirror of https://github.com/TaKO8Ki/gobang
refactor components event
parent
72fb787038
commit
6a166d8e34
@ -1,46 +0,0 @@
|
||||
use crate::app::{App, Focus};
|
||||
use crate::components::Component as _;
|
||||
use crate::event::Key;
|
||||
use crate::utils::{get_databases, get_tables};
|
||||
use database_tree::Database;
|
||||
use sqlx::mysql::MySqlPool;
|
||||
use std::collections::BTreeSet;
|
||||
|
||||
pub async fn handler(key: Key, app: &mut App) -> anyhow::Result<()> {
|
||||
match key {
|
||||
Key::Enter => {
|
||||
app.record_table.reset();
|
||||
if let Some(conn) = app.connections.selected_connection() {
|
||||
if let Some(pool) = app.pool.as_ref() {
|
||||
pool.close().await;
|
||||
}
|
||||
let pool = MySqlPool::connect(conn.database_url().as_str()).await?;
|
||||
app.pool = Some(pool);
|
||||
app.focus = Focus::DabataseList;
|
||||
}
|
||||
if let Some(conn) = app.connections.selected_connection() {
|
||||
match &conn.database {
|
||||
Some(database) => app
|
||||
.databases
|
||||
.update(
|
||||
&[Database::new(
|
||||
database.clone(),
|
||||
get_tables(database.clone(), app.pool.as_ref().unwrap()).await?,
|
||||
)],
|
||||
&BTreeSet::new(),
|
||||
)
|
||||
.unwrap(),
|
||||
None => app
|
||||
.databases
|
||||
.update(
|
||||
get_databases(app.pool.as_ref().unwrap()).await?.as_slice(),
|
||||
&BTreeSet::new(),
|
||||
)
|
||||
.unwrap(),
|
||||
}
|
||||
};
|
||||
}
|
||||
key => app.connections.event(key)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
@ -1,48 +0,0 @@
|
||||
use crate::app::{App, Focus};
|
||||
use crate::components::table::RECORDS_LIMIT_PER_PAGE;
|
||||
use crate::components::{Component as _, RecordTableComponent, TableComponent};
|
||||
use crate::event::Key;
|
||||
use crate::utils::{get_columns, get_records};
|
||||
use database_tree::Database;
|
||||
|
||||
pub async fn handler(key: Key, app: &mut App) -> anyhow::Result<()> {
|
||||
match key {
|
||||
Key::Char('c') if app.databases.tree_focused() => app.focus = Focus::ConnectionList,
|
||||
Key::Right if app.databases.tree_focused() => app.focus = Focus::Table,
|
||||
Key::Enter if app.databases.tree_focused() => {
|
||||
if let Some((table, database)) = app.databases.tree().selected_table() {
|
||||
app.focus = Focus::Table;
|
||||
let (headers, records) = get_records(
|
||||
&Database {
|
||||
name: database.clone(),
|
||||
tables: vec![],
|
||||
},
|
||||
&table,
|
||||
0,
|
||||
RECORDS_LIMIT_PER_PAGE,
|
||||
None,
|
||||
app.pool.as_ref().unwrap(),
|
||||
)
|
||||
.await?;
|
||||
app.record_table = RecordTableComponent::new(records, headers);
|
||||
app.record_table.set_table(table.name.to_string());
|
||||
|
||||
let (headers, records) = get_columns(
|
||||
&Database {
|
||||
name: database,
|
||||
tables: vec![],
|
||||
},
|
||||
&table,
|
||||
app.pool.as_ref().unwrap(),
|
||||
)
|
||||
.await?;
|
||||
app.structure_table = TableComponent::new(records, headers);
|
||||
|
||||
app.table_status
|
||||
.update(app.record_table.len() as u64, table);
|
||||
}
|
||||
}
|
||||
key => app.databases.event(key)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
@ -1,30 +0,0 @@
|
||||
pub mod connection_list;
|
||||
pub mod database_list;
|
||||
pub mod record_table;
|
||||
pub mod structure_table;
|
||||
pub mod table_filter;
|
||||
|
||||
use crate::app::{App, Focus};
|
||||
use crate::components::tab::Tab;
|
||||
use crate::components::Component as _;
|
||||
use crate::event::Key;
|
||||
|
||||
pub async fn handle_app(key: Key, app: &mut App) -> anyhow::Result<()> {
|
||||
match key {
|
||||
Key::Esc if app.error.error.is_some() => {
|
||||
app.error.error = None;
|
||||
return Ok(());
|
||||
}
|
||||
key => app.tab.event(key)?,
|
||||
}
|
||||
|
||||
match app.focus {
|
||||
Focus::ConnectionList => connection_list::handler(key, app).await?,
|
||||
Focus::DabataseList => database_list::handler(key, app).await?,
|
||||
Focus::Table => match app.tab.selected_tab {
|
||||
Tab::Records => record_table::handler(key, app).await?,
|
||||
Tab::Structure => structure_table::handler(key, app).await?,
|
||||
},
|
||||
}
|
||||
Ok(())
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
use crate::app::{App, Focus};
|
||||
use crate::components::table::RECORDS_LIMIT_PER_PAGE;
|
||||
use crate::components::Component as _;
|
||||
use crate::event::Key;
|
||||
use crate::utils::get_records;
|
||||
use database_tree::Database;
|
||||
|
||||
pub async fn handler(key: Key, app: &mut App) -> anyhow::Result<()> {
|
||||
match key {
|
||||
Key::Left => app.focus = Focus::DabataseList,
|
||||
Key::Char('c') => app.focus = Focus::ConnectionList,
|
||||
Key::Char('y') => {
|
||||
if let Some(text) = app.record_table.table.selected_cell() {
|
||||
app.clipboard.store(text)
|
||||
}
|
||||
}
|
||||
Key::Enter
|
||||
if matches!(
|
||||
app.record_table.focus,
|
||||
crate::components::record_table::Focus::Filter
|
||||
) =>
|
||||
{
|
||||
crate::handlers::table_filter::handler(key, app).await?
|
||||
}
|
||||
key => {
|
||||
app.record_table.event(key)?;
|
||||
if app.record_table.table.eod {
|
||||
return Ok(());
|
||||
}
|
||||
if let Some(index) = app.record_table.table.state.selected() {
|
||||
if index.saturating_add(1) % RECORDS_LIMIT_PER_PAGE as usize == 0 {
|
||||
if let Some((table, database)) = app.databases.tree().selected_table() {
|
||||
let (_, records) = get_records(
|
||||
&Database {
|
||||
name: database.clone(),
|
||||
tables: vec![],
|
||||
},
|
||||
&table,
|
||||
index as u16,
|
||||
RECORDS_LIMIT_PER_PAGE,
|
||||
if app.record_table.filter.input.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(app.record_table.filter.input.to_string())
|
||||
},
|
||||
app.pool.as_ref().unwrap(),
|
||||
)
|
||||
.await?;
|
||||
if !records.is_empty() {
|
||||
app.record_table.table.rows.extend(records);
|
||||
} else {
|
||||
app.record_table.table.end()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
use crate::app::{App, Focus};
|
||||
use crate::components::Component as _;
|
||||
use crate::event::Key;
|
||||
|
||||
pub async fn handler(key: Key, app: &mut App) -> anyhow::Result<()> {
|
||||
match key {
|
||||
Key::Left => app.focus = Focus::DabataseList,
|
||||
Key::Char('c') => app.focus = Focus::ConnectionList,
|
||||
Key::Char('y') => {
|
||||
if let Some(text) = app.structure_table.selected_cell() {
|
||||
app.clipboard.store(text)
|
||||
}
|
||||
}
|
||||
key => app.structure_table.event(key)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
use crate::app::App;
|
||||
use crate::components::table::RECORDS_LIMIT_PER_PAGE;
|
||||
use crate::event::Key;
|
||||
use crate::utils::get_records;
|
||||
use database_tree::Database;
|
||||
|
||||
pub async fn handler(key: Key, app: &mut App) -> anyhow::Result<()> {
|
||||
match key {
|
||||
Key::Enter => {
|
||||
app.record_table.focus = crate::components::record_table::Focus::Table;
|
||||
let filter_input = app.record_table.filter.input.to_string();
|
||||
|
||||
if app.record_table.filter.input.is_empty() {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if let Some((table, database)) = app.databases.tree().selected_table() {
|
||||
let (headers, records) = get_records(
|
||||
&Database {
|
||||
name: database.clone(),
|
||||
tables: vec![],
|
||||
},
|
||||
&table,
|
||||
0,
|
||||
RECORDS_LIMIT_PER_PAGE,
|
||||
Some(filter_input),
|
||||
app.pool.as_ref().unwrap(),
|
||||
)
|
||||
.await?;
|
||||
app.record_table.update(records, headers);
|
||||
}
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue