Skip to content

Commit

Permalink
Merge pull request #90 from kyoto7250/fix/issue-89
Browse files Browse the repository at this point in the history
Fix out of bounds when no search results are found
  • Loading branch information
kyoto7250 authored Jul 3, 2024
2 parents ec8bf76 + f862b93 commit 8aac395
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
36 changes: 17 additions & 19 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,9 @@ impl App {
Ok(())
}

async fn update_record_table(
&mut self,
orders: Option<String>,
header_icons: Option<Vec<String>>,
hold_cursor_position: bool,
) -> anyhow::Result<()> {
async fn update_record_table(&mut self, hold_cursor_position: bool) -> anyhow::Result<()> {
if let Some((database, table)) = self.databases.tree().selected_table() {
let order_query = self.record_table.table.generate_order_query();
let (headers, records) = self
.pool
.as_ref()
Expand All @@ -202,7 +198,7 @@ impl App {
} else {
Some(self.record_table.filter.input_str())
},
orders,
order_query,
)
.await?;
let total_row_count = self
Expand All @@ -219,10 +215,12 @@ impl App {
},
)
.await?;

let header_icons = self.record_table.table.generate_header_icons(headers.len());
self.record_table.update(
records,
Some(total_row_count),
self.concat_headers(headers, header_icons),
self.concat_headers(headers, Some(header_icons)),
database.clone(),
table.clone(),
hold_cursor_position,
Expand Down Expand Up @@ -311,10 +309,7 @@ impl App {
&& !self.record_table.table.headers.is_empty()
{
self.record_table.table.add_order();
let order_query = self.record_table.table.generate_order_query();
let header_icons = self.record_table.table.generate_header_icons();
self.update_record_table(order_query, Some(header_icons), true)
.await?;
self.update_record_table(true).await?;
return Ok(EventState::Consumed);
};

Expand All @@ -327,10 +322,7 @@ impl App {
if key == self.config.key_config.enter && self.record_table.filter_focused()
{
self.record_table.focus = crate::components::record_table::Focus::Table;
let order_query = self.record_table.table.generate_order_query();
let header_icons = self.record_table.table.generate_header_icons();
self.update_record_table(order_query, Some(header_icons), false)
.await?;
self.update_record_table(false).await?;
}

if self.record_table.table.eod {
Expand Down Expand Up @@ -415,9 +407,15 @@ impl App {
if let Some(header_icons) = &header_icons {
let mut new_headers = vec![String::new(); headers.len()];
for (index, header) in headers.iter().enumerate() {
new_headers[index] = format!("{} {}", header, header_icons[index])
.trim()
.to_string();
// It does not support increasing or decreasing table columns using filter.
// Rewrite when implementing column deletion
new_headers[index] = format!(
"{} {}",
header,
header_icons.get(index).unwrap_or(&String::from(""))
)
.trim()
.to_string();
}
return new_headers;
}
Expand Down
14 changes: 9 additions & 5 deletions src/components/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ use unicode_width::UnicodeWidthStr;

#[derive(Debug, PartialEq)]
struct Order {
// NOTE:
// In order to handle increases and decreases in columns,
// it is better to set the column name as a String instead of column_number.
pub column_number: usize,
pub is_asc: bool,
}
Expand Down Expand Up @@ -69,11 +72,12 @@ impl OrderManager {

fn generate_header_icons(&mut self, header_length: usize) -> Vec<String> {
let mut header_icons = vec![String::new(); header_length];

for (index, order) in self.orders.iter().enumerate() {
let arrow = if order.is_asc { "↑" } else { "↓" };
header_icons[order.column_number - 1] =
format!("{arrow}{number}", arrow = arrow, number = index + 1);
if header_icons.len() > order.column_number - 1 {
header_icons[order.column_number - 1] =
format!("{arrow}{number}", arrow = arrow, number = index + 1);
}
}

header_icons
Expand Down Expand Up @@ -190,8 +194,8 @@ impl TableComponent {
self.orders.generate_order_query()
}

pub fn generate_header_icons(&mut self) -> Vec<String> {
self.orders.generate_header_icons(self.headers.len())
pub fn generate_header_icons(&mut self, header_length: usize) -> Vec<String> {
self.orders.generate_header_icons(header_length)
}

pub fn end(&mut self) {
Expand Down

0 comments on commit 8aac395

Please sign in to comment.