diff --git a/tui/src/key_checker/key_handler.rs b/tui/src/key_checker/key_handler.rs index 308dc35d..3c0aca62 100644 --- a/tui/src/key_checker/key_handler.rs +++ b/tui/src/key_checker/key_handler.rs @@ -14,8 +14,9 @@ use crate::page_handler::{ SummaryTab, TableData, TxTab, }; use crate::pages::{ - ChoicePopupState, ConfigChoices, DeletionChoices, InfoPopupState, MovementDirection, - NewPathChoices, PopupType, + ACTIVITY_TABLE_ID, ChoicePopupState, ConfigChoices, DeletionChoices, HOME_TABLE_ID, + InfoPopupState, MovementDirection, NewPathChoices, PopupType, SEARCH_TABLE_ID, + SUMMARY_TABLE_ID, }; use crate::theme::Theme; use crate::tx_handler::TxData; @@ -344,6 +345,8 @@ impl<'a> InputKeyHandler<'a> { LogType::Info, ); } + + self.lerp_state.clear_lerp(SEARCH_TABLE_ID); self.reload_activity_table()?; } Ok(()) @@ -567,7 +570,10 @@ impl<'a> InputKeyHandler<'a> { HomeTab::Table => {} }, CurrentUi::AddTx => self.add_tx_data.move_index_right(self.add_tx_tab), - CurrentUi::Search => self.search_data.move_index_right(self.search_tab), + CurrentUi::Search => { + self.search_data.move_index_right(self.search_tab); + self.lerp_state.clear_lerp(SEARCH_TABLE_ID); + } CurrentUi::Chart => { if !*self.chart_hidden_mode { match self.chart_tab { @@ -611,20 +617,26 @@ impl<'a> InputKeyHandler<'a> { } SummaryTab::Table => {} } + + self.lerp_state.clear_lerp(SUMMARY_TABLE_ID); } } - CurrentUi::Activity => match self.activity_tab { - ActivityTab::Years => { - self.activity_months.set_index_zero(); - self.activity_years.next_yearly(); - self.reload_activity_table()?; - } - ActivityTab::Months => { - self.activity_months.next(); - self.reload_activity_table()?; - } - ActivityTab::List => {} - }, + CurrentUi::Activity => { + match self.activity_tab { + ActivityTab::Years => { + self.activity_months.set_index_zero(); + self.activity_years.next_yearly(); + self.reload_activity_table()?; + } + ActivityTab::Months => { + self.activity_months.next(); + self.reload_activity_table()?; + } + ActivityTab::List => {} + }; + + self.lerp_state.clear_lerp(ACTIVITY_TABLE_ID); + } CurrentUi::Initial => {} } @@ -1899,6 +1911,8 @@ impl InputKeyHandler<'_> { *self.home_table = TableData::new(self.home_txs.tx_array()); + self.lerp_state.clear_lerp(HOME_TABLE_ID); + Ok(()) } diff --git a/tui/src/pages/activity_ui.rs b/tui/src/pages/activity_ui.rs index a321baf9..4af252c6 100644 --- a/tui/src/pages/activity_ui.rs +++ b/tui/src/pages/activity_ui.rs @@ -9,6 +9,8 @@ use crate::page_handler::{ActivityTab, IndexedData, TableData}; use crate::theme::Theme; use crate::utility::{LerpState, create_tab, main_block, styled_block, tab_highlight_style}; +pub const ACTIVITY_TABLE_ID: &str = "activity_table_row"; + pub fn activity_ui( f: &mut Frame, months: &IndexedData, @@ -82,7 +84,9 @@ pub fn activity_ui( let tx_count = activity_view.total_activity(); let lerp_id = "activity_tx_count"; - let lerp_tx_count = lerp_state.lerp(lerp_id, tx_count as f64) as i64; + let lerp_tx_count = lerp_state.lerp(lerp_id, tx_count as f64, None) as i64; + + let lerp_row = lerp_state.lerp(ACTIVITY_TABLE_ID, tx_count as f64, Some(0.50)) as usize; let table_name = format!("Transactions: {lerp_tx_count}"); @@ -104,7 +108,7 @@ pub fn activity_ui( .height(1) .bottom_margin(0); - let activity_rows = table_data.items.iter().map(|item| { + let activity_rows = table_data.items.iter().take(lerp_row).map(|item| { let height = 1; let cells = item.iter().map(|c| Cell::from(c.separate_with_commas())); Row::new(cells) diff --git a/tui/src/pages/add_tx_ui.rs b/tui/src/pages/add_tx_ui.rs index 13293fe2..c4cb4b99 100644 --- a/tui/src/pages/add_tx_ui.rs +++ b/tui/src/pages/add_tx_ui.rs @@ -130,7 +130,7 @@ pub fn add_tx_ui( index += 1; let lerp_id = format!("{row_type}:{index}"); - let to_show = lerp_state.lerp(&lerp_id, actual_data); + let to_show = lerp_state.lerp(&lerp_id, actual_data, None); // re-add the previously removed symbol if is the Changes row // Otherwise separate the number with commas diff --git a/tui/src/pages/chart_ui.rs b/tui/src/pages/chart_ui.rs index 5dde2f91..2026816d 100644 --- a/tui/src/pages/chart_ui.rs +++ b/tui/src/pages/chart_ui.rs @@ -135,7 +135,7 @@ pub fn chart_ui( let total_loop = final_date.signed_duration_since(checking_date).num_days() as f64; let lerp_id = "chart_loop_size"; - let mut to_loop = lerp_state.lerp(lerp_id, total_loop); + let mut to_loop = lerp_state.lerp(lerp_id, total_loop, None); // labels of the x axis date_labels.push(checking_date.to_string()); diff --git a/tui/src/pages/home_ui.rs b/tui/src/pages/home_ui.rs index 1e719683..eb2586e9 100644 --- a/tui/src/pages/home_ui.rs +++ b/tui/src/pages/home_ui.rs @@ -10,6 +10,8 @@ use crate::page_handler::{HomeRow, HomeTab, IndexedData, TableData}; use crate::theme::Theme; use crate::utility::{LerpState, create_tab, main_block, styled_block, tab_highlight_style}; +pub const HOME_TABLE_ID: &str = "home_table_row"; + pub const BALANCE_BOLD: [&str; 7] = [ "Balance", "Changes", @@ -58,7 +60,9 @@ pub fn home_ui( let tx_count = home_table.items.len(); let lerp_id = "home_tx_count"; - let lerp_tx_count = lerp_state.lerp(lerp_id, tx_count as f64) as i64; + let lerp_tx_count = lerp_state.lerp(lerp_id, tx_count as f64, None) as i64; + + let lerp_tx_row = lerp_state.lerp(HOME_TABLE_ID, tx_count as f64, Some(0.50)) as i64; let table_name = format!("Transactions: {lerp_tx_count}"); @@ -76,6 +80,7 @@ pub fn home_ui( let rows = home_table .items .iter() + .take(lerp_tx_row as usize) .enumerate() .map(|(row_index, item)| { let cells = item.iter().enumerate().map(|(index, c)| { @@ -84,7 +89,7 @@ pub fn home_ui( }; let lerp_id = format!("home_table:{index}:{row_index}"); - let new_c = lerp_state.lerp(&lerp_id, parsed_num); + let new_c = lerp_state.lerp(&lerp_id, parsed_num, None); Cell::from(format!("{new_c:.2}").separate_with_commas()) }); @@ -183,7 +188,7 @@ pub fn home_ui( }; let lerp_id = format!("{row_type}:{index}"); - let to_show = lerp_state.lerp(&lerp_id, actual_data); + let to_show = lerp_state.lerp(&lerp_id, actual_data, None); // re-add the previously removed symbol if is the Changes row // Otherwise separate the number with commas diff --git a/tui/src/pages/search_ui.rs b/tui/src/pages/search_ui.rs index 749ebeb6..2f56c839 100644 --- a/tui/src/pages/search_ui.rs +++ b/tui/src/pages/search_ui.rs @@ -12,6 +12,8 @@ use crate::theme::Theme; use crate::tx_handler::TxData; use crate::utility::{LerpState, main_block, styled_block}; +pub const SEARCH_TABLE_ID: &str = "search_table_row"; + pub fn search_ui( f: &mut Frame, search_data: &TxData, @@ -43,13 +45,16 @@ pub fn search_ui( let tx_count = search_table.items.len(); let lerp_id = "home_tx_count"; - let lerp_tx_count = lerp_state.lerp(lerp_id, tx_count as f64) as i64; + let lerp_tx_count = lerp_state.lerp(lerp_id, tx_count as f64, None) as i64; + + let lerp_row = lerp_state.lerp(SEARCH_TABLE_ID, tx_count as f64, Some(0.50)) as usize; let table_name = format!("Transactions: {lerp_tx_count}"); let rows = search_table .items .iter() + .take(lerp_row) .enumerate() .map(|(row_index, item)| { let height = 1; @@ -62,7 +67,7 @@ pub fn search_ui( }; let lerp_id = format!("search_table:{index}:{row_index}"); - let new_c = lerp_state.lerp(&lerp_id, parsed_num); + let new_c = lerp_state.lerp(&lerp_id, parsed_num, None); Cell::from(format!("{new_c:.2}").separate_with_commas()) }); diff --git a/tui/src/pages/summary_ui.rs b/tui/src/pages/summary_ui.rs index 2a6249b8..7ff61356 100644 --- a/tui/src/pages/summary_ui.rs +++ b/tui/src/pages/summary_ui.rs @@ -13,6 +13,8 @@ use crate::utility::{ tab_highlight_style, }; +pub const SUMMARY_TABLE_ID: &str = "summary_table_row"; + /// The function draws the Summary page of the interface. pub fn summary_ui( f: &mut Frame, @@ -30,6 +32,12 @@ pub fn summary_ui( ) { let size = f.area(); + let tags_count = table_data.items.len(); + let lerp_id = "summary_tags_count"; + let lerp_tags_count = lerp_state.lerp(lerp_id, tags_count as f64, None) as i64; + + let lerp_row = lerp_state.lerp(SUMMARY_TABLE_ID, tags_count as f64, Some(0.50)) as usize; + let tag_header = if let SortingType::Tags = summary_sort { "Tags ↑" } else { @@ -202,6 +210,7 @@ pub fn summary_ui( let rows = table_data .items .iter() + .take(lerp_row) .enumerate() .map(|(row_index, item)| { let cells = item.iter().enumerate().map(|(index, c)| { @@ -213,7 +222,7 @@ pub fn summary_ui( let Ok(parsed_num) = c.parse::() else { if c == "∞" { - let new_c = lerp_state.lerp(&lerp_id, 0.0); + let new_c = lerp_state.lerp(&lerp_id, 0.0, None); return Cell::from(format!("{new_c:.2}").separate_with_commas()); } @@ -226,7 +235,7 @@ pub fn summary_ui( if let Some(sym) = symbol { let c = c.replace(sym, ""); if let Ok(parsed_num) = c.parse::() { - let new_c = lerp_state.lerp(&lerp_id, parsed_num); + let new_c = lerp_state.lerp(&lerp_id, parsed_num, None); return Cell::from(format!("{sym}{new_c:.2}").separate_with_commas()); } @@ -234,7 +243,7 @@ pub fn summary_ui( return Cell::from(c.separate_with_commas()); }; - let new_c = lerp_state.lerp(&lerp_id, parsed_num); + let new_c = lerp_state.lerp(&lerp_id, parsed_num, None); Cell::from(format!("{new_c:.2}").separate_with_commas()) }); @@ -268,9 +277,11 @@ pub fn summary_ui( ] }; + let tags_text = format!("Tags: {lerp_tags_count}"); + let mut table_area = Table::new(rows, table_width) .header(tag_table_header) - .block(styled_block("Tags", theme)) + .block(styled_block(&tags_text, theme)) .style(Style::default().fg(theme.border())); let summary_rows_largest = @@ -282,7 +293,7 @@ pub fn summary_ui( let cells = item.into_iter().enumerate().map(|(index, c)| { let mut cell = if let Ok(parsed_num) = c.parse::() { let lerp_id = format!("summary_table_largest:{index}:{row_index}"); - let new_c = lerp_state.lerp(&lerp_id, parsed_num); + let new_c = lerp_state.lerp(&lerp_id, parsed_num, None); Cell::from(format!("{new_c:.2}").separate_with_commas()) } else { @@ -326,7 +337,7 @@ pub fn summary_ui( let cells = item.into_iter().enumerate().map(|(index, c)| { let mut cell = if let Ok(parsed_num) = c.parse::() { let lerp_id = format!("summary_table_peak:{index}:{row_index}"); - let new_c = lerp_state.lerp(&lerp_id, parsed_num); + let new_c = lerp_state.lerp(&lerp_id, parsed_num, None); let text = if index == 1 && row_index == 2 { // Month checked value. No need float for this @@ -376,7 +387,7 @@ pub fn summary_ui( let cells = item.into_iter().enumerate().map(|(index, c)| { let mut cell = if let Ok(parsed_num) = c.parse::() { let lerp_id = format!("method_table:{index}:{row_index}"); - let new_c = lerp_state.lerp(&lerp_id, parsed_num); + let new_c = lerp_state.lerp(&lerp_id, parsed_num, None); Cell::from(format!("{new_c:.2}").separate_with_commas()) } else { @@ -390,7 +401,7 @@ pub fn summary_ui( let c = c.replace(sym, ""); if let Ok(parsed_num) = c.parse::() { let lerp_id = format!("method_table:{index}:{row_index}"); - let new_c = lerp_state.lerp(&lerp_id, parsed_num); + let new_c = lerp_state.lerp(&lerp_id, parsed_num, None); return Cell::from( format!("{sym}{new_c:.2}").separate_with_commas(), @@ -400,7 +411,7 @@ pub fn summary_ui( if c == "∞" { let lerp_id = format!("method_table:{index}:{row_index}"); - lerp_state.lerp(&lerp_id, 0.0); + lerp_state.lerp(&lerp_id, 0.0, None); } Cell::from(c.separate_with_commas()) }; @@ -468,7 +479,7 @@ pub fn summary_ui( let lerp_id = format!("lend_borrow_table:{index}:{row_index}"); if let Ok(parsed_num) = c.parse::() { - let new_c = lerp_state.lerp(&lerp_id, parsed_num); + let new_c = lerp_state.lerp(&lerp_id, parsed_num, None); Cell::from(format!("{new_c:.2}").separate_with_commas()) } else { @@ -481,14 +492,14 @@ pub fn summary_ui( if let Some(sym) = symbol { let c = c.replace(sym, ""); if let Ok(parsed_num) = c.parse::() { - let new_c = lerp_state.lerp(&lerp_id, parsed_num); + let new_c = lerp_state.lerp(&lerp_id, parsed_num, None); return Cell::from(format!("{sym}{new_c:.2}").separate_with_commas()); } } if c == "∞" { - lerp_state.lerp(&lerp_id, 0.0); + lerp_state.lerp(&lerp_id, 0.0, None); } Cell::from(c.separate_with_commas()) } @@ -523,13 +534,13 @@ pub fn summary_ui( let cells = item.iter().enumerate().map(|(index, c)| { let mut cell = if let Ok(parsed_num) = c.parse::() { let lerp_id = format!("summary_rows_net:{index}:{row_index}"); - let new_c = lerp_state.lerp(&lerp_id, parsed_num); + let new_c = lerp_state.lerp(&lerp_id, parsed_num, None); Cell::from(format!("{new_c:.2}").separate_with_commas()) } else { if c == "∞" { let lerp_id = format!("summary_rows_net:{index}:{row_index}"); - lerp_state.lerp(&lerp_id, 0.0); + lerp_state.lerp(&lerp_id, 0.0, None); } let symbol = if c.contains('↑') || c.contains('↓') { @@ -542,7 +553,7 @@ pub fn summary_ui( let c = c.replace(sym, ""); if let Ok(parsed_num) = c.parse::() { let lerp_id = format!("summary_net_rows:{index}:{row_index}"); - let new_c = lerp_state.lerp(&lerp_id, parsed_num); + let new_c = lerp_state.lerp(&lerp_id, parsed_num, None); return Cell::from(format!("{sym}{new_c:.2}").separate_with_commas()); } diff --git a/tui/src/utility/lerp.rs b/tui/src/utility/lerp.rs index 849a3311..4c07b2b9 100644 --- a/tui/src/utility/lerp.rs +++ b/tui/src/utility/lerp.rs @@ -26,7 +26,7 @@ impl LerpState { } } - pub fn lerp(&mut self, id: &str, new_target: f64) -> f64 { + pub fn lerp(&mut self, id: &str, new_target: f64, duration: Option) -> f64 { let now = Instant::now(); let entry = self.values.entry(id.to_string()).or_insert_with(|| { @@ -51,7 +51,7 @@ impl LerpState { entry.start_time = now; } - let t = (now - entry.start_time).as_secs_f64() / self.duration_secs; + let t = (now - entry.start_time).as_secs_f64() / duration.unwrap_or(self.duration_secs); let clamped_t = t.min(1.0); entry.current = entry.start + (entry.target - entry.start) * clamped_t; @@ -71,6 +71,11 @@ impl LerpState { self.active_lerp_count > 0 } + pub fn clear_lerp(&mut self, id: &str) { + self.values.remove(id); + self.active_lerp_count = self.active_lerp_count.saturating_sub(1); + } + pub fn clear(&mut self) { self.values.clear(); self.active_lerp_count = 0;