Implement table infinite scrolling (#22)

* implement infinite scrolling

* add windows as runs-on OS
input-text-overflow-scroll
Takayuki Maeda 3 years ago committed by GitHub
parent 4486fc707d
commit 4472b63754
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -61,7 +61,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
needs: check
steps:

@ -9,3 +9,9 @@ user = "root"
host = "localhost"
port = 3306
database = "world"
[[conn]]
user = "root"
host = "localhost"
port = 3306
database = "employees"

@ -11,6 +11,9 @@ use tui::{
widgets::{Block, Borders, Cell, Row, Table, TableState},
Frame,
};
use unicode_width::UnicodeWidthStr;
pub const RECORDS_LIMIT_PER_PAGE: u8 = 200;
pub struct TableComponent {
pub state: TableState,
@ -158,16 +161,7 @@ impl TableComponent {
.iter()
.map(|row| row[self.column_page..].to_vec())
.collect::<Vec<Vec<String>>>();
let mut new_rows = match self.state.selected() {
Some(index) => {
if index + 100 <= self.rows.len() {
rows[..index + 100].to_vec()
} else {
rows
}
}
None => rows,
};
let mut new_rows = rows;
for (index, row) in new_rows.iter_mut().enumerate() {
row.insert(0, (index + 1).to_string())
}

@ -1,4 +1,5 @@
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};
@ -19,6 +20,8 @@ pub async fn handler(key: Key, app: &mut App) -> anyhow::Result<()> {
tables: vec![],
},
&table,
0,
RECORDS_LIMIT_PER_PAGE,
app.pool.as_ref().unwrap(),
)
.await?;

@ -1,6 +1,9 @@
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 {
@ -11,7 +14,29 @@ pub async fn handler(key: Key, app: &mut App) -> anyhow::Result<()> {
app.clipboard.store(text)
}
}
key => app.record_table.event(key)?,
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(())
}

@ -32,9 +32,17 @@ pub async fn get_tables(database: String, pool: &MySqlPool) -> anyhow::Result<Ve
pub async fn get_records(
database: &Database,
table: &Table,
page: u16,
limit: u8,
pool: &MySqlPool,
) -> anyhow::Result<(Vec<String>, Vec<Vec<String>>)> {
let query = format!("SELECT * FROM `{}`.`{}`", database.name, table.name);
let query = format!(
"SELECT * FROM `{}`.`{}` limit {page}, {limit}",
database.name,
table.name,
page = page,
limit = limit
);
let mut rows = sqlx::query(query.as_str()).fetch(pool);
let headers =
sqlx::query(format!("SHOW COLUMNS FROM `{}`.`{}`", database.name, table.name).as_str())

Loading…
Cancel
Save