mirror of https://github.com/TaKO8Ki/gobang
Filter records (#24)
* filter records * refactor components event * fix scrollbar style * remove unneeded doc * fix event order and table filter cursor * fix and enhance databases filterpull/27/head
parent
4472b63754
commit
98ca815c68
@ -1,83 +0,0 @@
|
||||
use super::{Component, DrawableComponent};
|
||||
use crate::event::Key;
|
||||
use anyhow::Result;
|
||||
use tui::{
|
||||
backend::Backend,
|
||||
layout::Rect,
|
||||
style::{Color, Style},
|
||||
widgets::{Block, Borders, Paragraph},
|
||||
Frame,
|
||||
};
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
pub struct QueryComponent {
|
||||
pub input: String,
|
||||
pub input_cursor_x: u16,
|
||||
}
|
||||
|
||||
impl Default for QueryComponent {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
input: String::new(),
|
||||
input_cursor_x: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl QueryComponent {
|
||||
pub fn increment_input_cursor_x(&mut self) {
|
||||
if self.input_cursor_x > 0 {
|
||||
self.input_cursor_x -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn decrement_input_cursor_x(&mut self) {
|
||||
if self.input_cursor_x < self.input.width() as u16 {
|
||||
self.input_cursor_x += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DrawableComponent for QueryComponent {
|
||||
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, area: Rect, focused: bool) -> Result<()> {
|
||||
let query = Paragraph::new(self.input.as_ref())
|
||||
.style(if focused {
|
||||
Style::default()
|
||||
} else {
|
||||
Style::default().fg(Color::DarkGray)
|
||||
})
|
||||
.block(Block::default().borders(Borders::ALL).title("Query"));
|
||||
f.render_widget(query, area);
|
||||
if focused {
|
||||
f.set_cursor(
|
||||
area.x + self.input.width() as u16 + 1 - self.input_cursor_x,
|
||||
area.y + 1,
|
||||
)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Component for QueryComponent {
|
||||
fn event(&mut self, key: Key) -> Result<()> {
|
||||
match key {
|
||||
Key::Char(c) => self.input.push(c),
|
||||
Key::Delete | Key::Backspace => {
|
||||
if self.input.width() > 0 {
|
||||
if self.input_cursor_x == 0 {
|
||||
self.input.pop();
|
||||
return Ok(());
|
||||
}
|
||||
if self.input.width() - self.input_cursor_x as usize > 0 {
|
||||
self.input
|
||||
.remove(self.input.width() - self.input_cursor_x as usize);
|
||||
}
|
||||
}
|
||||
}
|
||||
Key::Left => self.decrement_input_cursor_x(),
|
||||
Key::Right => self.increment_input_cursor_x(),
|
||||
_ => (),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
@ -0,0 +1,100 @@
|
||||
use super::{Component, DrawableComponent, EventState};
|
||||
use crate::components::{TableComponent, TableFilterComponent};
|
||||
use crate::event::Key;
|
||||
use anyhow::Result;
|
||||
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,
|
||||
}
|
||||
|
||||
impl Default for RecordTableComponent {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
filter: TableFilterComponent::default(),
|
||||
table: TableComponent::default(),
|
||||
focus: Focus::Table,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RecordTableComponent {
|
||||
pub fn new(rows: Vec<Vec<String>>, headers: Vec<String>) -> Self {
|
||||
Self {
|
||||
table: TableComponent::new(rows, headers),
|
||||
..Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn update(&mut self, rows: Vec<Vec<String>>, headers: Vec<String>) {
|
||||
self.table.rows = rows;
|
||||
self.table.headers = headers;
|
||||
if !self.table.rows.is_empty() {
|
||||
self.table.state.select(None);
|
||||
self.table.state.select(Some(0));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn reset(&mut self) {
|
||||
self.table = TableComponent::default();
|
||||
if !self.table.rows.is_empty() {
|
||||
self.table.state.select(None);
|
||||
self.table.state.select(Some(0))
|
||||
}
|
||||
self.filter = TableFilterComponent::default();
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.table.rows.len()
|
||||
}
|
||||
|
||||
pub fn set_table(&mut self, table: String) {
|
||||
self.filter.table = Some(table)
|
||||
}
|
||||
|
||||
pub fn filter_focused(&self) -> bool {
|
||||
matches!(self.focus, Focus::Filter)
|
||||
}
|
||||
}
|
||||
|
||||
impl DrawableComponent for RecordTableComponent {
|
||||
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.filter
|
||||
.draw(f, layout[0], focused && matches!(self.focus, Focus::Filter))?;
|
||||
|
||||
self.table
|
||||
.draw(f, layout[1], focused && matches!(self.focus, Focus::Table))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Component for RecordTableComponent {
|
||||
fn event(&mut self, key: Key) -> Result<EventState> {
|
||||
match key {
|
||||
Key::Char('/') => {
|
||||
self.focus = Focus::Filter;
|
||||
return Ok(EventState::Consumed);
|
||||
}
|
||||
key if matches!(self.focus, Focus::Filter) => return Ok(self.filter.event(key)?),
|
||||
key if matches!(self.focus, Focus::Table) => return Ok(self.table.event(key)?),
|
||||
_ => (),
|
||||
}
|
||||
Ok(EventState::NotConsumed)
|
||||
}
|
||||
}
|
@ -0,0 +1,136 @@
|
||||
use super::{compute_character_width, Component, DrawableComponent, EventState};
|
||||
use crate::event::Key;
|
||||
use anyhow::Result;
|
||||
use tui::{
|
||||
backend::Backend,
|
||||
layout::Rect,
|
||||
style::{Color, Style},
|
||||
text::{Span, Spans},
|
||||
widgets::{Block, Borders, Paragraph},
|
||||
Frame,
|
||||
};
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
pub struct TableFilterComponent {
|
||||
pub table: Option<String>,
|
||||
pub input: Vec<char>,
|
||||
pub input_idx: usize,
|
||||
pub input_cursor_position: u16,
|
||||
}
|
||||
|
||||
impl Default for TableFilterComponent {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
table: None,
|
||||
input: Vec::new(),
|
||||
input_idx: 0,
|
||||
input_cursor_position: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TableFilterComponent {
|
||||
pub fn input_str(&self) -> String {
|
||||
self.input.iter().collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl DrawableComponent for TableFilterComponent {
|
||||
fn draw<B: Backend>(&mut self, f: &mut Frame<B>, area: Rect, focused: bool) -> Result<()> {
|
||||
let query = Paragraph::new(Spans::from(vec![
|
||||
Span::styled(
|
||||
self.table
|
||||
.as_ref()
|
||||
.map_or("-".to_string(), |table| table.to_string()),
|
||||
Style::default().fg(Color::Blue),
|
||||
),
|
||||
Span::from(format!(
|
||||
" {}",
|
||||
if focused || !self.input.is_empty() {
|
||||
self.input.iter().collect::<String>()
|
||||
} else {
|
||||
"Enter a SQL expression in WHERE clause".to_string()
|
||||
}
|
||||
)),
|
||||
]))
|
||||
.style(if focused {
|
||||
Style::default()
|
||||
} else {
|
||||
Style::default().fg(Color::DarkGray)
|
||||
})
|
||||
.block(Block::default().borders(Borders::ALL));
|
||||
f.render_widget(query, area);
|
||||
if focused {
|
||||
f.set_cursor(
|
||||
(area.x
|
||||
+ (1 + self
|
||||
.table
|
||||
.as_ref()
|
||||
.map_or(String::new(), |table| table.to_string())
|
||||
.width()
|
||||
+ 1) as u16)
|
||||
.saturating_add(self.input_cursor_position),
|
||||
area.y + 1,
|
||||
)
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Component for TableFilterComponent {
|
||||
fn event(&mut self, key: Key) -> Result<EventState> {
|
||||
let input_str: String = self.input.iter().collect();
|
||||
match key {
|
||||
Key::Char(c) => {
|
||||
self.input.insert(self.input_idx, c);
|
||||
self.input_idx += 1;
|
||||
self.input_cursor_position += compute_character_width(c);
|
||||
|
||||
return Ok(EventState::Consumed);
|
||||
}
|
||||
Key::Delete | Key::Backspace => {
|
||||
if input_str.width() > 0 {
|
||||
if !self.input.is_empty() && self.input_idx > 0 {
|
||||
let last_c = self.input.remove(self.input_idx - 1);
|
||||
self.input_idx -= 1;
|
||||
self.input_cursor_position -= compute_character_width(last_c);
|
||||
}
|
||||
}
|
||||
return Ok(EventState::Consumed);
|
||||
}
|
||||
Key::Left => {
|
||||
if !self.input.is_empty() && self.input_idx > 0 {
|
||||
self.input_idx -= 1;
|
||||
self.input_cursor_position = self
|
||||
.input_cursor_position
|
||||
.saturating_sub(compute_character_width(self.input[self.input_idx]));
|
||||
}
|
||||
return Ok(EventState::Consumed);
|
||||
}
|
||||
Key::Ctrl('a') => {
|
||||
if !self.input.is_empty() && self.input_idx > 0 {
|
||||
self.input_idx = 0;
|
||||
self.input_cursor_position = 0
|
||||
}
|
||||
return Ok(EventState::Consumed);
|
||||
}
|
||||
Key::Right => {
|
||||
if self.input_idx < self.input.len() {
|
||||
let next_c = self.input[self.input_idx];
|
||||
self.input_idx += 1;
|
||||
self.input_cursor_position += compute_character_width(next_c);
|
||||
}
|
||||
return Ok(EventState::Consumed);
|
||||
}
|
||||
Key::Ctrl('e') => {
|
||||
if self.input_idx < self.input.len() {
|
||||
self.input_idx = self.input.len();
|
||||
self.input_cursor_position = self.input_str().width() as u16;
|
||||
}
|
||||
return Ok(EventState::Consumed);
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
Ok(EventState::NotConsumed)
|
||||
}
|
||||
}
|
@ -1,47 +0,0 @@
|
||||
use crate::app::{App, FocusBlock};
|
||||
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(vec![], vec![]);
|
||||
app.record_table.state.select(Some(0));
|
||||
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_block = FocusBlock::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, FocusBlock};
|
||||
use crate::components::table::RECORDS_LIMIT_PER_PAGE;
|
||||
use crate::components::Component as _;
|
||||
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_block = FocusBlock::ConnectionList
|
||||
}
|
||||
Key::Right if app.databases.tree_focused() => app.focus_block = FocusBlock::Table,
|
||||
Key::Enter if app.databases.tree_focused() => {
|
||||
if let Some((table, database)) = app.databases.tree().selected_table() {
|
||||
app.focus_block = FocusBlock::Table;
|
||||
let (headers, records) = get_records(
|
||||
&Database {
|
||||
name: database.clone(),
|
||||
tables: vec![],
|
||||
},
|
||||
&table,
|
||||
0,
|
||||
RECORDS_LIMIT_PER_PAGE,
|
||||
app.pool.as_ref().unwrap(),
|
||||
)
|
||||
.await?;
|
||||
app.record_table.reset(headers, records);
|
||||
|
||||
let (headers, records) = get_columns(
|
||||
&Database {
|
||||
name: database,
|
||||
tables: vec![],
|
||||
},
|
||||
&table,
|
||||
app.pool.as_ref().unwrap(),
|
||||
)
|
||||
.await?;
|
||||
app.structure_table.reset(headers, records);
|
||||
|
||||
app.table_status
|
||||
.update(app.record_table.rows.len() as u64, table);
|
||||
}
|
||||
}
|
||||
key => app.databases.event(key)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
pub mod connection_list;
|
||||
pub mod database_list;
|
||||
pub mod query;
|
||||
pub mod record_table;
|
||||
pub mod structure_table;
|
||||
|
||||
use crate::app::{App, FocusBlock};
|
||||
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::Ctrl('e') => app.focus_block = FocusBlock::Query,
|
||||
Key::Esc if app.error.error.is_some() => {
|
||||
app.error.error = None;
|
||||
return Ok(());
|
||||
}
|
||||
key => app.tab.event(key)?,
|
||||
}
|
||||
|
||||
match app.focus_block {
|
||||
FocusBlock::ConnectionList => connection_list::handler(key, app).await?,
|
||||
FocusBlock::DabataseList => database_list::handler(key, app).await?,
|
||||
FocusBlock::Table => match app.tab.selected_tab {
|
||||
Tab::Records => record_table::handler(key, app).await?,
|
||||
Tab::Structure => structure_table::handler(key, app).await?,
|
||||
},
|
||||
FocusBlock::Query => query::handler(key, app).await?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
@ -1,46 +0,0 @@
|
||||
use crate::app::{App, FocusBlock};
|
||||
use crate::components::Component as _;
|
||||
use crate::event::Key;
|
||||
use crate::utils::convert_column_value_to_string;
|
||||
use futures::TryStreamExt;
|
||||
use regex::Regex;
|
||||
use sqlx::Row;
|
||||
|
||||
pub async fn handler(key: Key, app: &mut App) -> anyhow::Result<()> {
|
||||
match key {
|
||||
Key::Enter => {
|
||||
let re = Regex::new(r"select .+ from ([^ ]+).*").unwrap();
|
||||
match re.captures(app.query.input.as_str()) {
|
||||
Some(caps) => {
|
||||
let mut rows =
|
||||
sqlx::query(app.query.input.as_str()).fetch(app.pool.as_ref().unwrap());
|
||||
let headers =
|
||||
sqlx::query(format!("desc `{}`", caps.get(1).unwrap().as_str()).as_str())
|
||||
.fetch_all(app.pool.as_ref().unwrap())
|
||||
.await?
|
||||
.iter()
|
||||
.map(|table| table.get(0))
|
||||
.collect::<Vec<String>>();
|
||||
let mut records = vec![];
|
||||
while let Some(row) = rows.try_next().await? {
|
||||
records.push(
|
||||
row.columns()
|
||||
.iter()
|
||||
.map(|col| convert_column_value_to_string(&row, col))
|
||||
.collect::<Vec<String>>(),
|
||||
)
|
||||
}
|
||||
app.record_table.reset(headers, records);
|
||||
}
|
||||
None => {
|
||||
sqlx::query(app.query.input.as_str())
|
||||
.execute(app.pool.as_ref().unwrap())
|
||||
.await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Key::Esc => app.focus_block = FocusBlock::Table,
|
||||
key => app.query.event(key)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
use crate::app::{App, FocusBlock};
|
||||
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_block = FocusBlock::DabataseList,
|
||||
Key::Char('c') => app.focus_block = FocusBlock::ConnectionList,
|
||||
Key::Char('y') => {
|
||||
if let Some(text) = app.record_table.selected_cell() {
|
||||
app.clipboard.store(text)
|
||||
}
|
||||
}
|
||||
key => {
|
||||
app.record_table.event(key)?;
|
||||
if let Some(index) = app.record_table.state.selected() {
|
||||
if index == app.record_table.rows.len().saturating_sub(1) {
|
||||
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,
|
||||
app.pool.as_ref().unwrap(),
|
||||
)
|
||||
.await?;
|
||||
if !records.is_empty() {
|
||||
app.record_table.rows.extend(records);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
use crate::app::{App, FocusBlock};
|
||||
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_block = FocusBlock::DabataseList,
|
||||
Key::Char('c') => app.focus_block = FocusBlock::ConnectionList,
|
||||
Key::Char('y') => {
|
||||
if let Some(text) = app.structure_table.selected_cell() {
|
||||
app.clipboard.store(text)
|
||||
}
|
||||
}
|
||||
key => app.structure_table.event(key)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
Loading…
Reference in New Issue